AddressableHelper.Manifest.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. 
  2. using XGame.Framework.Asset.Addressable;
  3. using XGame.Framework.Asset.Addressable.Data;
  4. using System.IO;
  5. using System.Linq;
  6. using UnityEditor;
  7. using UnityEngine;
  8. namespace XGame.Editor.Asset
  9. {
  10. public static partial class AddressableHelper
  11. {
  12. private static string GetAssetManifestPath(AddressableClassify classify)
  13. {
  14. switch (classify)
  15. {
  16. case AddressableClassify.BuiltIn:
  17. return PathDefine.BuiltInAssetManifestPath;
  18. case AddressableClassify.Tests:
  19. return PathDefine.TestAssetsManifestPath;
  20. case AddressableClassify.Backup:
  21. return PathDefine.AddressableBackupManifestPath;
  22. default:
  23. return PathDefine.ProductAssetManifestPath;
  24. }
  25. }
  26. /// <summary>
  27. /// 加载 AddressableAssetInfoManifest
  28. /// </summary>
  29. /// <returns></returns>
  30. public static AddressableInfosSo LoadAssetManifest(AddressableClassify classify = AddressableClassify.Product)
  31. {
  32. var manifest = AssetDatabase.LoadAssetAtPath<AddressableInfosSo>(GetAssetManifestPath(classify));
  33. //if (manifest != null)
  34. //{
  35. // manifest.Init();
  36. //}
  37. return manifest;
  38. }
  39. internal static AddressableInfosSo LoadOrCreateManifest(AddressableClassify classify = AddressableClassify.Product)
  40. {
  41. var manifestPath = GetAssetManifestPath(classify);
  42. var manifest = AssetDatabase.LoadAssetAtPath<AddressableInfosSo>(manifestPath);
  43. if (manifest == null)
  44. {
  45. if ((classify == AddressableClassify.BuiltIn && !Directory.Exists(PathDefine.BuiltInResourcesRelative))
  46. || (classify == AddressableClassify.Tests && !Directory.Exists(PathDefine.TestsToAddressable) && !Directory.Exists(PathDefine.BuiltInResTests)))
  47. {
  48. //文件夹不存在
  49. return manifest;
  50. }
  51. if (!Directory.Exists(PathDefine.ConfigsRelative))
  52. {
  53. Directory.CreateDirectory(PathDefine.ConfigsRelative);
  54. }
  55. if (!Directory.Exists(PathDefine.AddressableDataRelative))
  56. {
  57. Directory.CreateDirectory(PathDefine.AddressableDataRelative);
  58. }
  59. manifest = ScriptableObject.CreateInstance<AddressableInfosSo>();
  60. AssetDatabase.CreateAsset(manifest, manifestPath);
  61. }
  62. return manifest;
  63. }
  64. public static bool TryApplyAddressableName(AddressableInfosSo manifest, string guid,
  65. string addressableName)
  66. {
  67. if (manifest == null)
  68. {
  69. return false;
  70. }
  71. //待优化
  72. if (IsValidName(addressableName))
  73. {
  74. Debug.LogError($"AddressableName repeat. Please rename it. Name:{addressableName} Path:{AssetDatabase.GUIDToAssetPath(guid)}");
  75. return false;
  76. }
  77. if (manifest.TryApplyAddressableName(guid, addressableName, out var assetInfo))
  78. {
  79. var manifestPath = AssetDatabase.GetAssetPath(manifest);
  80. if (PathDefine.ProductAssetManifestPath.Equals(manifestPath) || PathDefine.BuiltInAssetManifestPath.Equals(manifestPath))
  81. {
  82. ApplyBackupAssetInfo(assetInfo);
  83. }
  84. return true;
  85. }
  86. return false;
  87. }
  88. public static bool TryRemoveAssetByGUID(AddressableInfosSo manifest, string guid)
  89. {
  90. if (manifest != null && manifest.TryRemoveAssetByGUID(guid))
  91. {
  92. var manifestPath = AssetDatabase.GetAssetPath(manifest);
  93. if (PathDefine.ProductAssetManifestPath.Equals(manifestPath) || PathDefine.BuiltInAssetManifestPath.Equals(manifestPath))
  94. { //删除备份
  95. var backup = LoadAssetManifest(AddressableClassify.Backup);
  96. backup?.TryRemoveAssetByGUID(guid);
  97. }
  98. return true;
  99. }
  100. return false;
  101. }
  102. public static bool RemoveAllInvalidAssets(AddressableInfosSo manifest)
  103. {
  104. if (manifest != null && manifest.RemoveAllInvalidAssets())
  105. {
  106. var manifestPath = AssetDatabase.GetAssetPath(manifest);
  107. if (PathDefine.ProductAssetManifestPath.Equals(manifestPath))
  108. {
  109. var backup = LoadAssetManifest(AddressableClassify.Backup);
  110. backup?.RemoveAllInvalidAssets();
  111. }
  112. return true;
  113. }
  114. return false;
  115. }
  116. private static void ApplyBackupAssetInfo(AssetInfo assetInfo)
  117. {
  118. var backup = LoadAssetManifest(AddressableClassify.Backup);
  119. if (backup != null)
  120. {
  121. var isNameDefault = IsDefaultAddressableName(assetInfo.addressableName, assetInfo.assetGUID);
  122. if (isNameDefault || !IsValidPath(assetInfo.GetAssetPath()))
  123. { //默认名字或者无效资源
  124. backup.TryRemoveAssetByGUID(assetInfo.assetGUID);
  125. }
  126. else
  127. {
  128. var assetInfos = backup.assetInfos.ToList();
  129. var index = assetInfos.FindIndex((info => info.assetGUID.Equals(assetInfo.assetGUID)));
  130. if (index >= 0)
  131. {
  132. assetInfos[index] = assetInfo;
  133. }
  134. else
  135. {
  136. assetInfos.Add(assetInfo);
  137. }
  138. backup.SetAssetInfos(assetInfos.ToArray());
  139. }
  140. }
  141. }
  142. /// <summary>
  143. /// 收集所有自定义的AddressableName
  144. /// </summary>
  145. private static void CollectCustomAddressableNames()
  146. {
  147. var backup = LoadAssetManifest(AddressableClassify.Backup);
  148. if (backup != null)
  149. {
  150. var assetInfos = backup.GetAssetInfos();
  151. assetInfos.Clear();
  152. AddressableClassify[] operations = { AddressableClassify.BuiltIn, AddressableClassify.Product };
  153. for (int i = 0; i < operations.Length; i++)
  154. {
  155. var manifest = LoadAssetManifest(operations[i]);
  156. if (manifest != null && manifest.assetInfos != null)
  157. {
  158. foreach (var assetInfo in manifest.assetInfos)
  159. {
  160. if (IsValidPath(assetInfo.GetAssetPath()) && !IsDefaultAddressableName(assetInfo.addressableName, assetInfo.assetGUID))
  161. { //有效资源并且不是默认名字
  162. assetInfos.Add(assetInfo);
  163. }
  164. }
  165. }
  166. }
  167. backup.SetAssetInfos(assetInfos.ToArray());
  168. }
  169. }
  170. internal static bool IsDefaultAddressableName(string addressableName, string guid, System.Func<string, bool> isContantFileName = null)
  171. {
  172. var assetPath = AssetDatabase.GUIDToAssetPath(guid);
  173. if (string.IsNullOrEmpty(addressableName) || string.IsNullOrEmpty(assetPath))
  174. {
  175. return false;
  176. }
  177. var fileName = GetFileNameIncludeI18n(assetPath);
  178. if (addressableName.Equals(fileName))
  179. {
  180. return true;
  181. }
  182. if (isContantFileName != null && isContantFileName.Invoke(fileName) == false)
  183. {
  184. //addressable配置里没有名字为fileName的配置
  185. return false;
  186. }
  187. var ext = Path.GetExtension(assetPath);
  188. if (addressableName.Equals(($"{fileName}{ext.Replace('.', '_')}").ToLower()))
  189. {
  190. return true;
  191. }
  192. if (addressableName.Equals(($"{fileName}_{guid.Substring(0, 8)}").ToLower()))
  193. {
  194. return true;
  195. }
  196. if (addressableName.Equals(($"{fileName}_{guid}").ToLower()))
  197. {
  198. return true;
  199. }
  200. return false;
  201. }
  202. }
  203. }