BuildHelper.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System.IO;
  2. using ETModel;
  3. using UnityEditor;
  4. namespace ETEditor
  5. {
  6. public static class BuildHelper
  7. {
  8. private const string relativeDirPrefix = "../Release";
  9. public static string BuildFolder = "../Release/{0}/StreamingAssets/";
  10. [MenuItem("Tools/web资源服务器")]
  11. public static void OpenFileServer()
  12. {
  13. ProcessHelper.Run("dotnet", "FileServer.dll", "../FileServer/");
  14. }
  15. public static void Build(PlatformType type, BuildAssetBundleOptions buildAssetBundleOptions, BuildOptions buildOptions, bool isBuildExe, bool isContainAB)
  16. {
  17. BuildTarget buildTarget = BuildTarget.StandaloneWindows;
  18. string exeName = "ET";
  19. switch (type)
  20. {
  21. case PlatformType.PC:
  22. buildTarget = BuildTarget.StandaloneWindows64;
  23. exeName += ".exe";
  24. break;
  25. case PlatformType.Android:
  26. buildTarget = BuildTarget.Android;
  27. exeName += ".apk";
  28. break;
  29. case PlatformType.IOS:
  30. buildTarget = BuildTarget.iOS;
  31. break;
  32. case PlatformType.MacOS:
  33. buildTarget = BuildTarget.StandaloneOSX;
  34. break;
  35. }
  36. string fold = string.Format(BuildFolder, type);
  37. if (!Directory.Exists(fold))
  38. {
  39. Directory.CreateDirectory(fold);
  40. }
  41. Log.Info("开始资源打包");
  42. BuildPipeline.BuildAssetBundles(fold, buildAssetBundleOptions, buildTarget);
  43. GenerateVersionInfo(fold);
  44. Log.Info("完成资源打包");
  45. if (isContainAB)
  46. {
  47. FileHelper.CleanDirectory("Assets/StreamingAssets/");
  48. FileHelper.CopyDirectory(fold, "Assets/StreamingAssets/");
  49. }
  50. if (isBuildExe)
  51. {
  52. AssetDatabase.Refresh();
  53. string[] levels = {
  54. "Assets/Scenes/Init.unity",
  55. };
  56. Log.Info("开始EXE打包");
  57. BuildPipeline.BuildPlayer(levels, $"{relativeDirPrefix}/{exeName}", buildTarget, buildOptions);
  58. Log.Info("完成exe打包");
  59. }
  60. }
  61. private static void GenerateVersionInfo(string dir)
  62. {
  63. VersionConfig versionProto = new VersionConfig();
  64. GenerateVersionProto(dir, versionProto, "");
  65. using (FileStream fileStream = new FileStream($"{dir}/Version.txt", FileMode.Create))
  66. {
  67. byte[] bytes = JsonHelper.ToJson(versionProto).ToByteArray();
  68. fileStream.Write(bytes, 0, bytes.Length);
  69. }
  70. }
  71. private static void GenerateVersionProto(string dir, VersionConfig versionProto, string relativePath)
  72. {
  73. foreach (string file in Directory.GetFiles(dir))
  74. {
  75. string md5 = MD5Helper.FileMD5(file);
  76. FileInfo fi = new FileInfo(file);
  77. long size = fi.Length;
  78. string filePath = relativePath == "" ? fi.Name : $"{relativePath}/{fi.Name}";
  79. versionProto.FileInfoDict.Add(filePath, new FileVersionInfo
  80. {
  81. File = filePath,
  82. MD5 = md5,
  83. Size = size,
  84. });
  85. }
  86. foreach (string directory in Directory.GetDirectories(dir))
  87. {
  88. DirectoryInfo dinfo = new DirectoryInfo(directory);
  89. string rel = relativePath == "" ? dinfo.Name : $"{relativePath}/{dinfo.Name}";
  90. GenerateVersionProto($"{dir}/{dinfo.Name}", versionProto, rel);
  91. }
  92. }
  93. }
  94. }