BuildAssetsCommand.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 UnityEngine;
  10. namespace HybridCLR.Editor
  11. {
  12. public static class BuildAssetsCommand
  13. {
  14. public static string HybridCLRBuildCacheDir => Application.dataPath + "/HybridCLRBuildCache";
  15. public static string AssetBundleOutputDir => $"{HybridCLRBuildCacheDir}/AssetBundleOutput";
  16. public static string AssetBundleSourceDataTempDir => $"{HybridCLRBuildCacheDir}/AssetBundleSourceData";
  17. public static string GetAssetBundleOutputDirByTarget(BuildTarget target)
  18. {
  19. return $"{AssetBundleOutputDir}/{target}";
  20. }
  21. public static string GetAssetBundleTempDirByTarget(BuildTarget target)
  22. {
  23. return $"{AssetBundleSourceDataTempDir}/{target}";
  24. }
  25. public static string ToRelativeAssetPath(string s)
  26. {
  27. return s.Substring(s.IndexOf("Assets/"));
  28. }
  29. /// <summary>
  30. /// 将HotFix.dll和HotUpdatePrefab.prefab打入common包.
  31. /// 将HotUpdateScene.unity打入scene包.
  32. /// </summary>
  33. /// <param name="tempDir"></param>
  34. /// <param name="outputDir"></param>
  35. /// <param name="target"></param>
  36. private static void BuildAssetBundles(string tempDir, string outputDir, BuildTarget target)
  37. {
  38. Directory.CreateDirectory(tempDir);
  39. Directory.CreateDirectory(outputDir);
  40. List<AssetBundleBuild> abs = new List<AssetBundleBuild>();
  41. {
  42. var prefabAssets = new List<string>();
  43. string testPrefab = $"{Application.dataPath}/Prefabs/Cube.prefab";
  44. prefabAssets.Add(testPrefab);
  45. AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
  46. abs.Add(new AssetBundleBuild
  47. {
  48. assetBundleName = "prefabs",
  49. assetNames = prefabAssets.Select(s => ToRelativeAssetPath(s)).ToArray(),
  50. });
  51. }
  52. BuildPipeline.BuildAssetBundles(outputDir, abs.ToArray(), BuildAssetBundleOptions.None, target);
  53. AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
  54. }
  55. public static void BuildAssetBundleByTarget(BuildTarget target)
  56. {
  57. BuildAssetBundles(GetAssetBundleTempDirByTarget(target), GetAssetBundleOutputDirByTarget(target), target);
  58. }
  59. [MenuItem("Build/BuildAssetsAndCopyToStreamingAssets")]
  60. public static void BuildAndCopyABAOTHotUpdateDlls()
  61. {
  62. BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
  63. BuildAssetBundleByTarget(target);
  64. CompileDllCommand.CompileDll(target);
  65. CopyABAOTHotUpdateDlls(target);
  66. AssetDatabase.Refresh();
  67. }
  68. public static void CopyABAOTHotUpdateDlls(BuildTarget target)
  69. {
  70. CopyAssetBundlesToStreamingAssets(target);
  71. CopyAOTAssembliesToStreamingAssets();
  72. CopyHotUpdateAssembliesToStreamingAssets();
  73. }
  74. //[MenuItem("HybridCLR/Build/BuildAssetbundle")]
  75. public static void BuildSceneAssetBundleActiveBuildTargetExcludeAOT()
  76. {
  77. BuildAssetBundleByTarget(EditorUserBuildSettings.activeBuildTarget);
  78. }
  79. public static void CopyAOTAssembliesToStreamingAssets()
  80. {
  81. var target = EditorUserBuildSettings.activeBuildTarget;
  82. string aotAssembliesSrcDir = SettingsUtil.GetAssembliesPostIl2CppStripDir(target);
  83. string aotAssembliesDstDir = Application.streamingAssetsPath;
  84. foreach (var dll in SettingsUtil.AOTAssemblyNames)
  85. {
  86. string srcDllPath = $"{aotAssembliesSrcDir}/{dll}.dll";
  87. if (!File.Exists(srcDllPath))
  88. {
  89. Debug.LogError($"ab中添加AOT补充元数据dll:{srcDllPath} 时发生错误,文件不存在。裁剪后的AOT dll在BuildPlayer时才能生成,因此需要你先构建一次游戏App后再打包。");
  90. continue;
  91. }
  92. string dllBytesPath = $"{aotAssembliesDstDir}/{dll}.dll.bytes";
  93. File.Copy(srcDllPath, dllBytesPath, true);
  94. Debug.Log($"[CopyAOTAssembliesToStreamingAssets] copy AOT dll {srcDllPath} -> {dllBytesPath}");
  95. }
  96. }
  97. public static void CopyHotUpdateAssembliesToStreamingAssets()
  98. {
  99. var target = EditorUserBuildSettings.activeBuildTarget;
  100. string hotfixDllSrcDir = SettingsUtil.GetHotUpdateDllsOutputDirByTarget(target);
  101. string hotfixAssembliesDstDir = Application.streamingAssetsPath;
  102. foreach (var dll in SettingsUtil.HotUpdateAssemblyFilesExcludePreserved)
  103. {
  104. string dllPath = $"{hotfixDllSrcDir}/{dll}";
  105. string dllBytesPath = $"{hotfixAssembliesDstDir}/{dll}.bytes";
  106. File.Copy(dllPath, dllBytesPath, true);
  107. Debug.Log($"[CopyHotUpdateAssembliesToStreamingAssets] copy hotfix dll {dllPath} -> {dllBytesPath}");
  108. }
  109. }
  110. public static void CopyAssetBundlesToStreamingAssets(BuildTarget target)
  111. {
  112. string streamingAssetPathDst = Application.streamingAssetsPath;
  113. Directory.CreateDirectory(streamingAssetPathDst);
  114. string outputDir = GetAssetBundleOutputDirByTarget(target);
  115. var abs = new string[] { "prefabs" };
  116. foreach (var ab in abs)
  117. {
  118. string srcAb = ToRelativeAssetPath($"{outputDir}/{ab}");
  119. string dstAb = ToRelativeAssetPath($"{streamingAssetPathDst}/{ab}");
  120. Debug.Log($"[CopyAssetBundlesToStreamingAssets] copy assetbundle {srcAb} -> {dstAb}");
  121. AssetDatabase.CopyAsset( srcAb, dstAb);
  122. }
  123. }
  124. }
  125. }