AssetBundleBuildCommand.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using HybridCLR.Editor.Commands;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using UnityEditor;
  9. using UnityEditor.EditorTools;
  10. using UnityEngine;
  11. using EditorTools = YooAsset.Editor.EditorTools;
  12. namespace HybridCLR.Editor
  13. {
  14. public static class AssetBundleBuildCommand
  15. {
  16. public static string HybridCLRBuildCacheDir => Application.dataPath + "/HybridCLRBuildCache";
  17. public static string AssetBundleOutputDir => $"{HybridCLRBuildCacheDir}/AssetBundleOutput";
  18. public static string AssetBundleSourceDataTempDir => $"{HybridCLRBuildCacheDir}/AssetBundleSourceData";
  19. public static HotUpdateAssemblyManifest HotUpdateManifest => Resources.Load<HotUpdateAssemblyManifest>("HotUpdateAssemblyManifest");
  20. public static string GetAssetBundleOutputDirByTarget(BuildTarget target)
  21. {
  22. return $"{AssetBundleOutputDir}/{target}";
  23. }
  24. public static string YooAssetCodeDll = Application.dataPath + "/Res/CodeDll/";
  25. public static string GetAssetBundleTempDirByTarget(BuildTarget target)
  26. {
  27. return $"{AssetBundleSourceDataTempDir}/{target}";
  28. }
  29. public static string ToRelativeAssetPath(string s)
  30. {
  31. return s.Substring(s.IndexOf("Assets/"));
  32. }
  33. /// <summary>
  34. /// 将HotFix.dll和HotUpdatePrefab.prefab打入common包.
  35. /// 将HotUpdateScene.unity打入scene包.
  36. /// </summary>
  37. /// <param name="tempDir"></param>
  38. /// <param name="outputDir"></param>
  39. /// <param name="target"></param>
  40. private static void BuildAssetBundles(string tempDir, string outputDir, BuildTarget target)
  41. {
  42. Directory.CreateDirectory(tempDir);
  43. Directory.CreateDirectory(outputDir);
  44. if (Directory.Exists(YooAssetCodeDll))
  45. {
  46. EditorTools.DeleteDirectory(YooAssetCodeDll);
  47. }
  48. Directory.CreateDirectory(YooAssetCodeDll);
  49. //step 1
  50. CompileDllCommand.CompileDll(target);
  51. //step 2
  52. PrebuildCommand.GenerateAll();
  53. //step 3 copy
  54. ET.BuildEditor.BuildModelAndHotfix();
  55. //hotfix.dll不使用默认的打包,ET里面加了些骚操作
  56. // List<string> notSceneAssets = new List<string>();
  57. /*string hotfixDllSrcDir = SettingsUtil.GetHotUpdateDllsOutputDirByTarget(target);
  58. foreach (var dll in SettingsUtil.HotUpdateAssemblyFiles)
  59. {
  60. string dllPath = $"{hotfixDllSrcDir}/{dll}";
  61. string dllBytesPath = $"{YooAssetCodeDll}/{dll}.bytes";
  62. File.Copy(dllPath, dllBytesPath, true);
  63. // notSceneAssets.Add(dllBytesPath);
  64. Debug.Log($"[BuildAssetBundles] copy hotfix dll {dllPath} -> {dllBytesPath}");
  65. }*/
  66. //step 4
  67. string aotDllDir = SettingsUtil.GetAssembliesPostIl2CppStripDir(target);
  68. HotUpdateAssemblyManifest manifest = HotUpdateManifest;
  69. if (manifest == null)
  70. {
  71. throw new Exception($"resource asset:{nameof(HotUpdateAssemblyManifest)} 配置不存在,请在Resources目录下创建");
  72. }
  73. List<string> AOTMetaAssemblies= (manifest.AOTMetadataDlls ?? Array.Empty<string>()).ToList();
  74. foreach (var dll in AOTMetaAssemblies)
  75. {
  76. string dllPath = $"{aotDllDir}/{dll}";
  77. if (!File.Exists(dllPath))
  78. {
  79. Debug.LogError($"ab中添加AOT补充元数据dll:{dllPath} 时发生错误,文件不存在。裁剪后的AOT dll在BuildPlayer时才能生成,因此需要你先构建一次游戏App后再打包。");
  80. continue;
  81. }
  82. string dllBytesPath = $"{YooAssetCodeDll}/{dll}.bytes";
  83. File.Copy(dllPath, dllBytesPath, true);
  84. // notSceneAssets.Add(dllBytesPath);
  85. Debug.Log($"[BuildAssetBundles] copy AOT dll {dllPath} -> {dllBytesPath}");
  86. }
  87. // string testPrefab = $"{Application.dataPath}/Prefabs/HotUpdatePrefab.prefab";
  88. // notSceneAssets.Add(testPrefab);
  89. // AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
  90. //
  91. // List<AssetBundleBuild> abs = new List<AssetBundleBuild>();
  92. // AssetBundleBuild notSceneAb = new AssetBundleBuild
  93. // {
  94. // assetBundleName = "common",
  95. // assetNames = notSceneAssets.Select(s => ToRelativeAssetPath(s)).ToArray(),
  96. // };
  97. // abs.Add(notSceneAb);
  98. //
  99. // UnityEditor.BuildPipeline.BuildAssetBundles(outputDir, abs.ToArray(), BuildAssetBundleOptions.None, target);
  100. //
  101. // AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
  102. //
  103. // string streamingAssetPathDst = $"{Application.streamingAssetsPath}";
  104. // Directory.CreateDirectory(streamingAssetPathDst);
  105. //
  106. // foreach (var ab in abs)
  107. // {
  108. // AssetDatabase.CopyAsset(ToRelativeAssetPath($"{outputDir}/{ab.assetBundleName}"),
  109. // ToRelativeAssetPath($"{streamingAssetPathDst}/{ab.assetBundleName}"));
  110. // }
  111. }
  112. public static void BuildAssetBundleByTarget(BuildTarget target)
  113. {
  114. BuildAssetBundles(GetAssetBundleTempDirByTarget(target), GetAssetBundleOutputDirByTarget(target), target);
  115. }
  116. [MenuItem("HybridCLR/CollectorDll/ActiveBuildTarget")]
  117. public static void BuildSceneAssetBundleActiveBuildTarget()
  118. {
  119. BuildAssetBundleByTarget(EditorUserBuildSettings.activeBuildTarget);
  120. }
  121. // [MenuItem("HybridCLR/CollectorDll/Win64")]
  122. // public static void BuildSceneAssetBundleWin64()
  123. // {
  124. // var target = BuildTarget.StandaloneWindows64;
  125. // BuildAssetBundleByTarget(target);
  126. // }
  127. //
  128. // [MenuItem("HybridCLR/CollectorDll/Win32")]
  129. // public static void BuildSceneAssetBundleWin32()
  130. // {
  131. // var target = BuildTarget.StandaloneWindows;
  132. // BuildAssetBundleByTarget(target);
  133. // }
  134. //
  135. // [MenuItem("HybridCLR/CollectorDll/Android")]
  136. // public static void BuildSceneAssetBundleAndroid()
  137. // {
  138. // var target = BuildTarget.Android;
  139. // BuildAssetBundleByTarget(target);
  140. // }
  141. //
  142. // [MenuItem("HybridCLR/CollectorDll/IOS")]
  143. // public static void BuildSceneAssetBundleIOS()
  144. // {
  145. // var target = BuildTarget.iOS;
  146. // BuildAssetBundleByTarget(target);
  147. // }
  148. }
  149. }