GFGUIPackage.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 async Task AddPackageAsync(string descFilePath)
  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. return;
  52. }
  53. _assetCount.Add(descFilePath, count + 1);
  54. try
  55. {
  56. // 加载主资源
  57. Debug.Log($"正在加载描述文件: {descFilePath}_fui.bytes");
  58. var handle = YooAssets.LoadAssetAsync<TextAsset>($"{descFilePath}_fui.bytes");
  59. await handle.Task;
  60. if (handle.Status == EOperationStatus.Succeed)
  61. {
  62. Debug.Log($"描述文件加载成功: {descFilePath}_fui.bytes");
  63. TextAsset textAsset = handle.AssetObject as TextAsset;
  64. CacheAssetHandle(descFilePath, handle);
  65. // 添加UI包
  66. Debug.Log($"正在添加UI包: {descFilePath}");
  67. var uiPackage = UIPackage.AddPackage(textAsset.bytes, descFilePath,
  68. (string name, string extension, System.Type type, PackageItem item) =>
  69. {
  70. string location = name + extension;
  71. Debug.Log($"正在加载依赖资源: {location}");
  72. if (!YooAssets.CheckResExist(location))
  73. {
  74. Debug.LogWarning($"依赖资源不存在: {location}");
  75. return;
  76. }
  77. AssetOperationHandle handle = YooAssets.LoadAssetAsync(location, type);
  78. CacheAssetHandle(descFilePath, handle);
  79. handle.Completed += (handle) =>
  80. {
  81. if (handle.Status != EOperationStatus.Succeed)
  82. {
  83. Debug.LogError($"依赖资源加载失败: {location}");
  84. return;
  85. }
  86. if (handle.AssetObject == null)
  87. {
  88. Debug.LogWarning($"依赖资源为空: {location}");
  89. return;
  90. }
  91. Debug.Log($"依赖资源加载成功: {location}");
  92. Texture tex = handle.AssetObject as Texture;
  93. if (tex != null)
  94. {
  95. string location = name + "!a" + extension;
  96. Debug.Log($"检测透明通道资源: {location}");
  97. if (YooAssets.CheckResExist(location))
  98. {
  99. AssetOperationHandle handleAlpha = YooAssets.LoadAssetAsync(location, type);
  100. CacheAssetHandle(descFilePath, handleAlpha);
  101. handleAlpha.Completed += (handle1) =>
  102. {
  103. if (handle1.Status != EOperationStatus.Succeed)
  104. {
  105. Debug.LogError($"透明通道资源加载失败: {location}");
  106. return;
  107. }
  108. if (handle1.AssetObject == null)
  109. {
  110. Debug.LogWarning($"透明通道资源为空: {location}");
  111. return;
  112. }
  113. Debug.Log($"透明通道资源加载成功: {location}");
  114. Texture alphaTex = handle1.AssetObject as Texture;
  115. item.owner.SetItemAsset(item, tex, alphaTex, DestroyMethod.None);
  116. };
  117. return;
  118. }
  119. }
  120. item.owner.SetItemAsset(item, handle.AssetObject, null, DestroyMethod.None);
  121. };
  122. });
  123. _packages.Add(descFilePath, uiPackage);
  124. Debug.Log($"UI包添加完成: {descFilePath}");
  125. CheckRemovePackage(descFilePath);
  126. }
  127. else
  128. {
  129. Debug.LogError($"描述文件加载失败: {descFilePath}_fui.bytes, 错误: {handle.LastError}");
  130. }
  131. }
  132. catch (Exception ex)
  133. {
  134. Debug.LogError($"加载UI包异常: {descFilePath}, 错误: {ex.Message}");
  135. throw;
  136. }
  137. }
  138. public static void ReleasePackage(string descFilePath)
  139. {
  140. _assetCount.TryGetValue(descFilePath, out var count);
  141. if (count > 0)
  142. {
  143. count--;
  144. _assetCount[descFilePath] = count;
  145. CheckRemovePackage(descFilePath);
  146. }
  147. }
  148. private static void CheckRemovePackage(string descFilePath)
  149. {
  150. _assetCount.TryGetValue(descFilePath, out var count);
  151. if (count > 0) return;
  152. _packages.TryGetValue(descFilePath, out var package);
  153. if (package == null) return;
  154. _assetCount.Remove(descFilePath);
  155. _packages.Remove(descFilePath);
  156. UIPackage.RemovePackage(package.name);
  157. if (assetHandleCacheDic.TryGetValue(descFilePath, out var list))
  158. {
  159. foreach (var asset in list)
  160. {
  161. asset.Release();
  162. }
  163. list.Clear();
  164. }
  165. }
  166. private static void CacheAssetHandle(string key, AssetOperationHandle handle)
  167. {
  168. List<AssetOperationHandle> list = null;
  169. if (!assetHandleCacheDic.TryGetValue(key, out list))
  170. {
  171. list = new List<AssetOperationHandle>();
  172. assetHandleCacheDic.Add(key, list);
  173. }
  174. list.Add(handle);
  175. }
  176. }
  177. }