GFGUIPackage.cs 7.2 KB

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