GFGUIPackage.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. _packages.Add(descFilePath, uiPackage);
  39. }
  40. public static async Task AddPackageAsync(string descFilePath)
  41. {
  42. _assetCount.TryGetValue(descFilePath, out var count);
  43. if (count > 0)
  44. {
  45. _assetCount[descFilePath] = count + 1;
  46. return;
  47. }
  48. _assetCount.Add(descFilePath, count + 1);
  49. var handle = YooAssets.LoadAssetAsync<TextAsset>($"{descFilePath}_fui.bytes");
  50. await handle.Task;
  51. TextAsset textAsset = handle.AssetObject as TextAsset;
  52. CacheAssetHandle(descFilePath, handle);
  53. var uiPackage = UIPackage.AddPackage(textAsset.bytes, descFilePath, (string name, string extension, System.Type type, PackageItem item) =>
  54. {
  55. string location = name + extension;
  56. if (!YooAssets.CheckResExist(location))
  57. {
  58. return;
  59. }
  60. AssetOperationHandle handle = YooAssets.LoadAssetAsync(location, type);
  61. CacheAssetHandle(descFilePath, handle);
  62. handle.Completed += (handle) =>
  63. {
  64. if (handle.AssetObject == null) return;
  65. Texture tex = handle.AssetObject as Texture;
  66. if (tex != null)
  67. {
  68. string location = name + "!a" + extension;
  69. if (YooAssets.CheckResExist(location))
  70. {
  71. AssetOperationHandle handleAlpha = YooAssets.LoadAssetAsync(location, type);
  72. CacheAssetHandle(descFilePath, handleAlpha);
  73. handleAlpha.Completed += (handle1) =>
  74. {
  75. if (handle1.AssetObject == null) return;
  76. Texture alphaTex = handle1.AssetObject as Texture;
  77. item.owner.SetItemAsset(item, tex, alphaTex, DestroyMethod.None);//注意:这里一定要设置为None
  78. };
  79. return;
  80. }
  81. }
  82. item.owner.SetItemAsset(item, handle.AssetObject, null, DestroyMethod.None);//注意:这里一定要设置为None
  83. };
  84. });
  85. _packages.Add(descFilePath, uiPackage);
  86. CheckRemovePackage(descFilePath);
  87. }
  88. public static void ReleasePackage(string descFilePath)
  89. {
  90. _assetCount.TryGetValue(descFilePath, out var count);
  91. if (count > 0)
  92. {
  93. count--;
  94. _assetCount[descFilePath] = count;
  95. CheckRemovePackage(descFilePath);
  96. }
  97. }
  98. private static void CheckRemovePackage(string descFilePath)
  99. {
  100. _assetCount.TryGetValue(descFilePath, out var count);
  101. if (count > 0) return;
  102. _packages.TryGetValue(descFilePath, out var package);
  103. if (package == null) return;
  104. _assetCount.Remove(descFilePath);
  105. _packages.Remove(descFilePath);
  106. UIPackage.RemovePackage(package.name);
  107. if (assetHandleCacheDic.TryGetValue(descFilePath, out var list))
  108. {
  109. foreach (var asset in list)
  110. {
  111. asset.Release();
  112. }
  113. list.Clear();
  114. }
  115. }
  116. private static void CacheAssetHandle(string key, AssetOperationHandle handle)
  117. {
  118. List<AssetOperationHandle> list = null;
  119. if (!assetHandleCacheDic.TryGetValue(key, out list))
  120. {
  121. list = new List<AssetOperationHandle>();
  122. assetHandleCacheDic.Add(key, list);
  123. }
  124. list.Add(handle);
  125. }
  126. }
  127. }