GFGUIPackage.cs 6.4 KB

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