GFGUIPackage.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using FairyGUI;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using YooAsset;
  6. using System.Threading.Tasks;
  7. namespace GFGGame
  8. {
  9. public class GFGUIPackage
  10. {
  11. private static Dictionary<string, UIPackage> _packages = new Dictionary<string, UIPackage>();
  12. private static Dictionary<string, List<AssetOperationHandle>> assetHandleCacheDic =
  13. new Dictionary<string, List<AssetOperationHandle>>();
  14. private static Dictionary<string, int> _assetCount = new Dictionary<string, int>();
  15. // public static void AddPackage(string descFilePath)
  16. // {
  17. // _assetCount.TryGetValue(descFilePath, out var count);
  18. // if (count > 0)
  19. // {
  20. // _assetCount[descFilePath] = count + 1;
  21. // return;
  22. // }
  23. // _assetCount.Add(descFilePath, count + 1);
  24. // var handle = YooAssets.LoadAssetSync<TextAsset>($"{descFilePath}_fui.bytes");
  25. // TextAsset textAsset = handle.AssetObject as TextAsset;
  26. // CacheAssetHandle(descFilePath, handle);
  27. // //同步
  28. // var uiPackage = UIPackage.AddPackage(textAsset.bytes, descFilePath, (string name, string extension, System.Type type, out DestroyMethod destroyMethod) =>
  29. // {
  30. // string location = name + extension;
  31. // destroyMethod = DestroyMethod.None; //注意:这里一定要设置为None
  32. // if (!YooAssets.CheckResExist(location))
  33. // {
  34. // return null;
  35. // }
  36. // var handle = YooAssets.LoadAssetSync(location, type);
  37. // CacheAssetHandle(descFilePath, handle);
  38. // return handle.AssetObject;
  39. // });
  40. // _packages.Add(descFilePath, uiPackage);
  41. // }
  42. public static void AddPackage(string descFilePath, Action onComplete = null, Action<string> onError = null)
  43. {
  44. // 打印开始加载信息
  45. Debug.Log($"开始加载UI包: {descFilePath}");
  46. _assetCount.TryGetValue(descFilePath, out var count);
  47. if (count > 0)
  48. {
  49. _assetCount[descFilePath] = count + 1;
  50. Debug.Log($"{descFilePath} 已引用,引用计数+1 (当前: {count + 1})");
  51. onComplete?.Invoke();
  52. return;
  53. }
  54. _assetCount.Add(descFilePath, count + 1);
  55. // 加载主资源
  56. Debug.Log($"正在加载描述文件: {descFilePath}_fui.bytes");
  57. var handle = YooAssets.LoadAssetAsync<TextAsset>($"{descFilePath}_fui.bytes");
  58. handle.Completed += (mainHandle) =>
  59. {
  60. if (mainHandle.Status != EOperationStatus.Succeed)
  61. {
  62. string error = $"描述文件加载失败: {descFilePath}_fui.bytes, 错误: {mainHandle.LastError}";
  63. Debug.LogError(error);
  64. onError?.Invoke(error);
  65. return;
  66. }
  67. Debug.Log($"描述文件加载成功: {descFilePath}_fui.bytes");
  68. TextAsset textAsset = mainHandle.AssetObject as TextAsset;
  69. CacheAssetHandle(descFilePath, mainHandle);
  70. // 添加UI包
  71. Debug.Log($"正在添加UI包: {descFilePath}");
  72. var uiPackage = UIPackage.AddPackage(textAsset.bytes, descFilePath,
  73. (string name, string extension, System.Type type, PackageItem item) =>
  74. {
  75. string location = name + extension;
  76. Debug.Log($"正在加载依赖资源: {location}");
  77. if (!YooAssets.CheckResExist(location))
  78. {
  79. Debug.LogWarning($"依赖资源不存在: {location}");
  80. return;
  81. }
  82. AssetOperationHandle depHandle = YooAssets.LoadAssetAsync(location, type);
  83. CacheAssetHandle(descFilePath, depHandle);
  84. depHandle.Completed += (depHandle) =>
  85. {
  86. if (depHandle.Status != EOperationStatus.Succeed)
  87. {
  88. Debug.LogError($"依赖资源加载失败: {location}");
  89. return;
  90. }
  91. if (depHandle.AssetObject == null)
  92. {
  93. Debug.LogWarning($"依赖资源为空: {location}");
  94. return;
  95. }
  96. Debug.Log($"依赖资源加载成功: {location}");
  97. Texture tex = depHandle.AssetObject as Texture;
  98. if (tex != null)
  99. {
  100. string alphaLocation = name + "!a" + extension;
  101. Debug.Log($"检测透明通道资源: {alphaLocation}");
  102. if (YooAssets.CheckResExist(alphaLocation))
  103. {
  104. AssetOperationHandle alphaHandle = YooAssets.LoadAssetAsync(alphaLocation, type);
  105. CacheAssetHandle(descFilePath, alphaHandle);
  106. alphaHandle.Completed += (alphaHandle) =>
  107. {
  108. if (alphaHandle.Status != EOperationStatus.Succeed)
  109. {
  110. Debug.LogError($"透明通道资源加载失败: {alphaLocation}");
  111. return;
  112. }
  113. if (alphaHandle.AssetObject == null)
  114. {
  115. Debug.LogWarning($"透明通道资源为空: {alphaLocation}");
  116. return;
  117. }
  118. Debug.Log($"透明通道资源加载成功: {alphaLocation}");
  119. Texture alphaTex = alphaHandle.AssetObject as Texture;
  120. item.owner.SetItemAsset(item, tex, alphaTex, DestroyMethod.None);
  121. };
  122. return;
  123. }
  124. }
  125. item.owner.SetItemAsset(item, depHandle.AssetObject, null, DestroyMethod.None);
  126. };
  127. });
  128. _packages.Add(descFilePath, uiPackage);
  129. Debug.Log($"UI包添加完成: {descFilePath}");
  130. CheckRemovePackage(descFilePath);
  131. onComplete?.Invoke();
  132. };
  133. }
  134. public static void ReleasePackage(string descFilePath)
  135. {
  136. _assetCount.TryGetValue(descFilePath, out var count);
  137. if (count > 0)
  138. {
  139. count--;
  140. _assetCount[descFilePath] = count;
  141. CheckRemovePackage(descFilePath);
  142. }
  143. }
  144. private static void CheckRemovePackage(string descFilePath)
  145. {
  146. _assetCount.TryGetValue(descFilePath, out var count);
  147. if (count > 0) return;
  148. _packages.TryGetValue(descFilePath, out var package);
  149. if (package == null) return;
  150. _assetCount.Remove(descFilePath);
  151. _packages.Remove(descFilePath);
  152. UIPackage.RemovePackage(package.name);
  153. if (assetHandleCacheDic.TryGetValue(descFilePath, out var list))
  154. {
  155. foreach (var asset in list)
  156. {
  157. asset.Release();
  158. }
  159. list.Clear();
  160. }
  161. }
  162. private static void CacheAssetHandle(string key, AssetOperationHandle handle)
  163. {
  164. List<AssetOperationHandle> list = null;
  165. if (!assetHandleCacheDic.TryGetValue(key, out list))
  166. {
  167. list = new List<AssetOperationHandle>();
  168. assetHandleCacheDic.Add(key, list);
  169. }
  170. list.Add(handle);
  171. }
  172. }
  173. }