GFGUIPackage.cs 7.7 KB

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