GFGUIPackage.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using FairyGUI;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using YooAsset;
  5. using System.Threading.Tasks;
  6. namespace GFGGame
  7. {
  8. public class GFGUIPackage
  9. {
  10. private static Dictionary<string, UIPackage> _packages = new Dictionary<string, UIPackage>();
  11. private static Dictionary<string, List<AssetOperationHandle>> assetHandleCacheDic = new Dictionary<string, List<AssetOperationHandle>>();
  12. private static Dictionary<string, int> _assetCount = new Dictionary<string, int>();
  13. public static void AddPackage(string descFilePath)
  14. {
  15. _assetCount.TryGetValue(descFilePath, out var count);
  16. if (count > 0)
  17. {
  18. _assetCount[descFilePath] = count + 1;
  19. return;
  20. }
  21. _assetCount.Add(descFilePath, count + 1);
  22. var handle = YooAssets.LoadAssetSync<TextAsset>($"{descFilePath}_fui.bytes");
  23. TextAsset textAsset = handle.AssetObject as TextAsset;
  24. CacheAssetHandle(descFilePath, handle);
  25. //同步
  26. var uiPackage = UIPackage.AddPackage(textAsset.bytes, descFilePath, (string name, string extension, System.Type type, out DestroyMethod destroyMethod) =>
  27. {
  28. string location = name + extension;
  29. destroyMethod = DestroyMethod.None; //注意:这里一定要设置为None
  30. if (!YooAssets.CheckResExist(location))
  31. {
  32. return null;
  33. }
  34. var handle = YooAssets.LoadAssetSync(location, type);
  35. CacheAssetHandle(descFilePath, handle);
  36. return handle.AssetObject;
  37. });
  38. //异步
  39. //var uiPackage = UIPackage.AddPackage(textAsset.bytes, descFilePath, (string name, string extension, System.Type type, PackageItem item) =>
  40. //{
  41. // string location = name + extension;
  42. // if (!YooAssets.CheckResExist(location))
  43. // {
  44. // return;
  45. // }
  46. // AssetOperationHandle handle = YooAssets.LoadAssetAsync(location, type);
  47. // CacheAssetHandle(descFilePath, handle);
  48. // handle.Completed += (handle) =>
  49. // {
  50. // if (handle.AssetObject == null) return;
  51. // Texture tex = handle.AssetObject as Texture;
  52. // if (tex != null)
  53. // {
  54. // string location = name + "!a" + extension;
  55. // if (YooAssets.CheckResExist(location))
  56. // {
  57. // AssetOperationHandle handleAlpha = YooAssets.LoadAssetAsync(location, type);
  58. // CacheAssetHandle(descFilePath, handleAlpha);
  59. // handleAlpha.Completed += (handle1) =>
  60. // {
  61. // if (handle1.AssetObject == null) return;
  62. // Texture alphaTex = handle1.AssetObject as Texture;
  63. // item.owner.SetItemAsset(item, tex, alphaTex, DestroyMethod.None);//注意:这里一定要设置为None
  64. // };
  65. // return;
  66. // }
  67. // }
  68. // item.owner.SetItemAsset(item, handle.AssetObject, null, DestroyMethod.None);//注意:这里一定要设置为None
  69. // };
  70. //});
  71. _packages.Add(descFilePath, uiPackage);
  72. }
  73. public static async Task AddPackageAsync(string descFilePath)
  74. {
  75. _assetCount.TryGetValue(descFilePath, out var count);
  76. if (count > 0)
  77. {
  78. _assetCount[descFilePath] = count + 1;
  79. return;
  80. }
  81. _assetCount.Add(descFilePath, count + 1);
  82. var handle = YooAssets.LoadAssetAsync<TextAsset>($"{descFilePath}_fui.bytes");
  83. await handle.Task;
  84. TextAsset textAsset = handle.AssetObject as TextAsset;
  85. CacheAssetHandle(descFilePath, handle);
  86. var uiPackage = UIPackage.AddPackage(textAsset.bytes, descFilePath, (string name, string extension, System.Type type, PackageItem item) =>
  87. {
  88. string location = name + extension;
  89. if (!YooAssets.CheckResExist(location))
  90. {
  91. return;
  92. }
  93. AssetOperationHandle handle = YooAssets.LoadAssetAsync(location, type);
  94. CacheAssetHandle(descFilePath, handle);
  95. handle.Completed += (handle) =>
  96. {
  97. if (handle.AssetObject == null) return;
  98. Texture tex = handle.AssetObject as Texture;
  99. if (tex != null)
  100. {
  101. string location = name + "!a" + extension;
  102. if (YooAssets.CheckResExist(location))
  103. {
  104. AssetOperationHandle handleAlpha = YooAssets.LoadAssetAsync(location, type);
  105. CacheAssetHandle(descFilePath, handleAlpha);
  106. handleAlpha.Completed += (handle1) =>
  107. {
  108. if (handle1.AssetObject == null) return;
  109. Texture alphaTex = handle1.AssetObject as Texture;
  110. item.owner.SetItemAsset(item, tex, alphaTex, DestroyMethod.None);//注意:这里一定要设置为None
  111. };
  112. return;
  113. }
  114. }
  115. item.owner.SetItemAsset(item, handle.AssetObject, null, DestroyMethod.None);//注意:这里一定要设置为None
  116. };
  117. });
  118. _packages.Add(descFilePath, uiPackage);
  119. CheckRemovePackage(descFilePath);
  120. }
  121. public static void ReleasePackage(string descFilePath)
  122. {
  123. _assetCount.TryGetValue(descFilePath, out var count);
  124. if (count > 0)
  125. {
  126. count--;
  127. _assetCount[descFilePath] = count;
  128. CheckRemovePackage(descFilePath);
  129. }
  130. }
  131. private static void CheckRemovePackage(string descFilePath)
  132. {
  133. _assetCount.TryGetValue(descFilePath, out var count);
  134. if (count > 0) return;
  135. _packages.TryGetValue(descFilePath, out var package);
  136. if (package == null) return;
  137. _assetCount.Remove(descFilePath);
  138. _packages.Remove(descFilePath);
  139. UIPackage.RemovePackage(package.name);
  140. if (assetHandleCacheDic.TryGetValue(descFilePath, out var list))
  141. {
  142. foreach (var asset in list)
  143. {
  144. asset.Release();
  145. }
  146. list.Clear();
  147. }
  148. }
  149. private static void CacheAssetHandle(string key, AssetOperationHandle handle)
  150. {
  151. List<AssetOperationHandle> list = null;
  152. if (!assetHandleCacheDic.TryGetValue(key, out list))
  153. {
  154. list = new List<AssetOperationHandle>();
  155. assetHandleCacheDic.Add(key, list);
  156. }
  157. list.Add(handle);
  158. }
  159. }
  160. }