AddressableInfosSoExt.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using XGame.Framework.Asset.Addressable;
  2. using XGame.Framework.Asset.Addressable.Data;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using UnityEditor;
  7. using UnityEngine;
  8. namespace XGame.Editor.Asset
  9. {
  10. public static class AddressableInfosSoExt
  11. {
  12. #region AddressableAssetInfoManifest
  13. /// <summary>
  14. /// 编辑器扩展方法
  15. /// 返回manifest内已有数据
  16. /// </summary>
  17. /// <param name="manifest"></param>
  18. /// <returns></returns>
  19. public static List<AssetInfo> GetAssetInfos(this AddressableInfosSo manifest)
  20. {
  21. if (manifest != null && manifest.assetInfos != null)
  22. {
  23. return manifest.assetInfos.Distinct().ToList();
  24. }
  25. //var fileInfo = manifest.GetType().GetField("assetInfos", BindingFlags.NonPublic | BindingFlags.Instance);
  26. //if (fileInfo != null)
  27. //{
  28. // var infos = (AssetInfo[])fileInfo.GetValue(manifest);
  29. // if (infos != null)
  30. // {
  31. // return infos.Distinct().ToList();
  32. // }
  33. //}
  34. return new List<AssetInfo>();
  35. }
  36. /// <summary>
  37. /// 返回以GUID做key的字典
  38. /// </summary>
  39. /// <param name="manifest"></param>
  40. /// <returns></returns>
  41. public static Dictionary<string, AssetInfo> GetAssetInfoMapWithGUID(this AddressableInfosSo manifest)
  42. {
  43. var map = new Dictionary<string, AssetInfo>();
  44. if (manifest != null && manifest.assetInfos != null)
  45. {
  46. foreach (var info in manifest.assetInfos)
  47. {
  48. var guid = info.assetGUID;
  49. if (!map.ContainsKey(guid))
  50. {
  51. map.Add(guid, info);
  52. }
  53. else
  54. {
  55. Debug.LogError($"AssetInfo repeated. Last:{map[guid]} Next:{info}");
  56. }
  57. }
  58. }
  59. return map;
  60. }
  61. /// <summary>
  62. /// 返回以addressableName做key的字典
  63. /// </summary>
  64. /// <param name="manifest"></param>
  65. /// <returns></returns>
  66. public static Dictionary<string, AssetInfo> GetAssetInfoMapWithAddressableName(this AddressableInfosSo manifest)
  67. {
  68. var map = new Dictionary<string, AssetInfo>();
  69. if (manifest != null && manifest.assetInfos != null)
  70. {
  71. foreach (var info in manifest.assetInfos)
  72. {
  73. map.Add(info.addressableName, info);
  74. }
  75. }
  76. return map;
  77. }
  78. /// <summary>
  79. /// 编辑器扩展方法
  80. /// 设置AddressableAssetInfo数组
  81. /// </summary>
  82. /// <param name="manifest"></param>
  83. /// <param name="assetInfos"></param>
  84. public static void SetAssetInfos(this AddressableInfosSo manifest, AssetInfo[] assetInfos)
  85. {
  86. if (manifest == null) return;
  87. if (assetInfos != null)
  88. {
  89. assetInfos = assetInfos.Distinct().ToArray();
  90. Array.Sort(assetInfos);
  91. }
  92. manifest.assetInfos = assetInfos;
  93. EditorUtility.SetDirty(manifest);
  94. AssetDatabase.SaveAssets();
  95. AssetDatabase.Refresh();
  96. }
  97. /// <summary>
  98. /// 编辑器扩展方法
  99. /// 根据GUID查找addressableName
  100. /// </summary>
  101. /// <param name="manifest"></param>
  102. /// <param name="guid"></param>
  103. /// <param name="addressableName"></param>
  104. /// <returns></returns>
  105. public static bool TryGetAddressableNameByGUID(this AddressableInfosSo manifest, string guid, out string addressableName)
  106. {
  107. addressableName = string.Empty;
  108. if (manifest != null && manifest.assetInfos != null)
  109. {
  110. var index = Array.FindIndex(manifest.assetInfos, (info => info.assetGUID.Equals(guid)));
  111. if (index < 0)
  112. {
  113. return false;
  114. }
  115. addressableName = manifest.assetInfos[index].addressableName;
  116. }
  117. return true;
  118. }
  119. /// <summary>
  120. /// 编辑器扩展方法
  121. /// 修改某个资源的addressableName
  122. /// </summary>
  123. /// <param name="manifest"></param>
  124. /// <param name="guid"></param>
  125. /// <param name="addressableName"></param>
  126. /// <param name="assetInfo"></param>
  127. /// <returns></returns>
  128. public static bool TryApplyAddressableName(this AddressableInfosSo manifest, string guid,
  129. string addressableName, out AssetInfo assetInfo)
  130. {
  131. var assetInfos = manifest.assetInfos;
  132. //转成小写
  133. addressableName = addressableName.ToLower();
  134. //检查重名
  135. var idxForName = Array.FindIndex(assetInfos, (info => info.addressableName.Equals(addressableName)));
  136. var idxForGUID = Array.FindIndex(assetInfos, (info => info.assetGUID.Equals(guid)));
  137. if (idxForName >= 0)
  138. {
  139. if (idxForGUID != idxForName)
  140. {
  141. Debug.LogError($"AddressableName repeat. Please rename it. Name:{addressableName} Path:{AssetDatabase.GUIDToAssetPath(guid)}");
  142. }
  143. else
  144. {
  145. Debug.Log($"Asset info hasn't changed.");
  146. }
  147. assetInfo = default;
  148. return false;
  149. }
  150. if (idxForGUID >= 0)
  151. {
  152. assetInfo = assetInfos[idxForGUID];
  153. assetInfo.addressableName = addressableName;
  154. assetInfos[idxForGUID] = assetInfo;
  155. //保存
  156. manifest.SetAssetInfos(assetInfos);
  157. Debug.Log($"Apply AddressableName Success. Path:{assetInfo.GetAssetPath()} AddressableName:{addressableName}");
  158. return true;
  159. }
  160. Debug.LogError("Can't Find Asset.");
  161. assetInfo = default;
  162. return false;
  163. }
  164. /// <summary>
  165. /// 编辑器扩展方法
  166. /// 删除指定资源信息
  167. /// </summary>
  168. /// <param name="manifest"></param>
  169. /// <param name="guid"></param>
  170. /// <returns></returns>
  171. public static bool TryRemoveAssetByGUID(this AddressableInfosSo manifest, string guid)
  172. {
  173. if (manifest.assetInfos == null || manifest.assetInfos.Length == 0)
  174. {
  175. Debug.Log($"AddressableInfosSo is null. Name:{manifest.name}");
  176. return false;
  177. }
  178. var assetInfos = manifest.assetInfos.ToList();
  179. var idxForGUID = assetInfos.FindIndex((info => info.assetGUID.Equals(guid)));
  180. if (idxForGUID == -1)
  181. {
  182. Debug.Log($"Can't find assetInfo by GUID:{guid}");
  183. return false;
  184. }
  185. var assetInfo = assetInfos[idxForGUID];
  186. assetInfos.RemoveAt(idxForGUID);
  187. //保存
  188. manifest.SetAssetInfos(assetInfos.ToArray());
  189. //EditorUtility.SetDirty(manifest);
  190. //AssetDatabase.SaveAssets();
  191. //AssetDatabase.Refresh();
  192. Debug.Log($"RemoveAsset Success. {assetInfo}");
  193. return true;
  194. }
  195. public static bool RemoveAllInvalidAssets(this AddressableInfosSo manifest)
  196. {
  197. var assetInfos = manifest.assetInfos;
  198. if (assetInfos == null || assetInfos.Length == 0)
  199. {
  200. Debug.Log($"AddressableInfosSo is null. Name:{manifest.name}");
  201. return false;
  202. }
  203. var validAssetInfos = Array.FindAll(assetInfos, (info => AddressableHelper.IsValidPath(info.GetAssetPath())));
  204. if (assetInfos.Length == validAssetInfos.Length)
  205. {
  206. Debug.Log("No find invalid assets.");
  207. return false;
  208. }
  209. //保存
  210. manifest.SetAssetInfos(validAssetInfos);
  211. //EditorUtility.SetDirty(manifest);
  212. //AssetDatabase.SaveAssets();
  213. //AssetDatabase.Refresh();
  214. Debug.Log("RemoveAllInvalidAssets Success.");
  215. return true;
  216. }
  217. #endregion
  218. #region i18n 国际化/本地化/多语言
  219. public static void SetAddressableI18nInfos(this AddressableInfosSo manifest,
  220. AddressableI18nInfo[] i18nInfos, bool refresh = true)
  221. {
  222. if (manifest == null) return;
  223. manifest.i18nInfos = i18nInfos;
  224. if (refresh)
  225. {
  226. EditorUtility.SetDirty(manifest);
  227. AssetDatabase.SaveAssets();
  228. AssetDatabase.Refresh();
  229. }
  230. }
  231. #endregion
  232. #region LOD
  233. public static void SetAddressableLodInfos(this AddressableInfosSo manifest,
  234. AddressableLodInfo[] lodInfos, bool refresh = true)
  235. {
  236. if (manifest == null) return;
  237. manifest.lodInfos = lodInfos;
  238. if (refresh)
  239. {
  240. EditorUtility.SetDirty(manifest);
  241. AssetDatabase.SaveAssets();
  242. AssetDatabase.Refresh();
  243. }
  244. }
  245. #endregion
  246. }
  247. }