GFGUIPackage.cs 5.5 KB

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