BuildHelper.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/编译Hotfix")]
  11. public static void BuildHotfix()
  12. {
  13. System.Diagnostics.Process process = new System.Diagnostics.Process();
  14. string unityDir = System.Environment.GetEnvironmentVariable("Unity");
  15. if (string.IsNullOrEmpty(unityDir))
  16. {
  17. Log.Error("没有设置Unity环境变量!");
  18. return;
  19. }
  20. process.StartInfo.FileName = $@"{unityDir}\Editor\Data\MonoBleedingEdge\bin\mono.exe";
  21. process.StartInfo.Arguments = $@"{unityDir}\Editor\Data\MonoBleedingEdge\lib\mono\xbuild\14.0\bin\xbuild.exe .\Hotfix\Unity.Hotfix.csproj";
  22. process.StartInfo.UseShellExecute = false;
  23. process.StartInfo.WorkingDirectory = @".\";
  24. process.StartInfo.RedirectStandardOutput = true;
  25. process.StartInfo.RedirectStandardError = true;
  26. process.Start();
  27. string info = process.StandardOutput.ReadToEnd();
  28. process.Close();
  29. Log.Info(info);
  30. }
  31. [MenuItem("Tools/web资源服务器")]
  32. public static void OpenFileServer()
  33. {
  34. #if !UNITY_EDITOR_OSX
  35. string currentDir = System.Environment.CurrentDirectory;
  36. string path = Path.Combine(currentDir, @"..\FileServer\");
  37. System.Diagnostics.Process process = new System.Diagnostics.Process();
  38. process.StartInfo.FileName = "FileServer.exe";
  39. process.StartInfo.WorkingDirectory = path;
  40. process.StartInfo.CreateNoWindow = true;
  41. process.Start();
  42. #else
  43. string path = System.Environment.CurrentDirectory + "/../FileServer/";
  44. ("cd " + path + " && go run FileServer.go").Bash(path, true);
  45. #endif
  46. }
  47. public static void Build(PlatformType type, BuildAssetBundleOptions buildAssetBundleOptions, BuildOptions buildOptions, bool isBuildExe, bool isContainAB)
  48. {
  49. BuildTarget buildTarget = BuildTarget.StandaloneWindows;
  50. string exeName = "ET";
  51. switch (type)
  52. {
  53. case PlatformType.PC:
  54. buildTarget = BuildTarget.StandaloneWindows64;
  55. exeName += ".exe";
  56. break;
  57. case PlatformType.Android:
  58. buildTarget = BuildTarget.Android;
  59. exeName += ".apk";
  60. break;
  61. case PlatformType.IOS:
  62. buildTarget = BuildTarget.iOS;
  63. break;
  64. case PlatformType.MacOS:
  65. buildTarget = BuildTarget.StandaloneOSX;
  66. break;
  67. }
  68. string fold = string.Format(BuildFolder, type);
  69. if (!Directory.Exists(fold))
  70. {
  71. Directory.CreateDirectory(fold);
  72. }
  73. Log.Info("开始资源打包");
  74. BuildPipeline.BuildAssetBundles(fold, buildAssetBundleOptions, buildTarget);
  75. GenerateVersionInfo(fold);
  76. Log.Info("完成资源打包");
  77. if (isContainAB)
  78. {
  79. FileHelper.CleanDirectory("Assets/StreamingAssets/");
  80. FileHelper.CopyDirectory(fold, "Assets/StreamingAssets/");
  81. }
  82. if (isBuildExe)
  83. {
  84. AssetDatabase.Refresh();
  85. string[] levels = {
  86. "Assets/Scenes/Init.unity",
  87. };
  88. Log.Info("开始EXE打包");
  89. BuildPipeline.BuildPlayer(levels, $"{relativeDirPrefix}/{exeName}", buildTarget, buildOptions);
  90. Log.Info("完成exe打包");
  91. }
  92. }
  93. private static void GenerateVersionInfo(string dir)
  94. {
  95. VersionConfig versionProto = new VersionConfig();
  96. GenerateVersionProto(dir, versionProto, "");
  97. using (FileStream fileStream = new FileStream($"{dir}/Version.txt", FileMode.Create))
  98. {
  99. byte[] bytes = JsonHelper.ToJson(versionProto).ToByteArray();
  100. fileStream.Write(bytes, 0, bytes.Length);
  101. }
  102. }
  103. private static void GenerateVersionProto(string dir, VersionConfig versionProto, string relativePath)
  104. {
  105. foreach (string file in Directory.GetFiles(dir))
  106. {
  107. string md5 = MD5Helper.FileMD5(file);
  108. FileInfo fi = new FileInfo(file);
  109. long size = fi.Length;
  110. string filePath = relativePath == "" ? fi.Name : $"{relativePath}/{fi.Name}";
  111. versionProto.FileInfoDict.Add(filePath, new FileVersionInfo
  112. {
  113. File = filePath,
  114. MD5 = md5,
  115. Size = size,
  116. });
  117. }
  118. foreach (string directory in Directory.GetDirectories(dir))
  119. {
  120. DirectoryInfo dinfo = new DirectoryInfo(directory);
  121. string rel = relativePath == "" ? dinfo.Name : $"{relativePath}/{dinfo.Name}";
  122. GenerateVersionProto($"{dir}/{dinfo.Name}", versionProto, rel);
  123. }
  124. }
  125. }
  126. }