GFGUIPackage.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using FairyGUI;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using YooAsset;
  5. namespace GFGGame
  6. {
  7. public class GFGUIPackage
  8. {
  9. private static Dictionary<string, UIPackage> _packages = new Dictionary<string, UIPackage>();
  10. private static Dictionary<string, List<AssetOperationHandle>> _assetDic = new Dictionary<string, List<AssetOperationHandle>>();
  11. private static Dictionary<string, int> _assetCount = new Dictionary<string, int>();
  12. public static UIPackage AddPackage(string descFilePath)
  13. {
  14. int count = _assetCount.ContainsKey(descFilePath) ? _assetCount[descFilePath] : 0;
  15. if (_packages.ContainsKey(descFilePath))
  16. {
  17. _assetCount[descFilePath] = count + 1;
  18. return _packages[descFilePath];
  19. }
  20. //var asset = Asset.Load($"{descFilePath}_fui.bytes", typeof(TextAsset));
  21. var handle = YooAssets.LoadAssetSync<TextAsset>($"{descFilePath}_fui.bytes");
  22. TextAsset textAsset = handle.AssetObject as TextAsset;
  23. AddAsset(descFilePath, handle);
  24. var uiPackage = UIPackage.AddPackage(textAsset.bytes, descFilePath, (string name, string extension, System.Type type, out DestroyMethod destroyMethod) =>
  25. {
  26. //destroyMethod = DestroyMethod.Unload;
  27. string path = name + extension;
  28. //if (Versions.Contains(path))
  29. //{
  30. // var asset = Asset.Load(path, type);
  31. // AddAsset(descFilePath, asset);
  32. // return asset.asset;
  33. //}
  34. //return null;
  35. destroyMethod = DestroyMethod.None; //注意:这里一定要设置为None
  36. string location = path;
  37. var handle = YooAssets.LoadAssetSync(location, type);
  38. AddAsset(descFilePath, handle);
  39. return handle.AssetObject;
  40. });
  41. _assetCount.Add(descFilePath, count + 1);
  42. _packages.Add(descFilePath, uiPackage);
  43. return uiPackage;
  44. }
  45. public static void RemovePackage(string descFilePath)
  46. {
  47. UIPackage package;
  48. if (_packages.TryGetValue(descFilePath, out package))
  49. {
  50. int count = _assetCount[descFilePath] - 1;
  51. _assetCount[descFilePath] = count;
  52. if (count > 0) return;
  53. _assetCount.Remove(descFilePath);
  54. _packages.Remove(descFilePath);
  55. UIPackage.RemovePackage(package.name);
  56. List<AssetOperationHandle> list = null;
  57. if (_assetDic.TryGetValue(descFilePath, out list))
  58. {
  59. foreach (var asset in list)
  60. {
  61. asset.Release();
  62. }
  63. list.Clear();
  64. }
  65. }
  66. }
  67. private static void AddAsset(string key, AssetOperationHandle asset)
  68. {
  69. List<AssetOperationHandle> list = null;
  70. if (!_assetDic.TryGetValue(key, out list))
  71. {
  72. list = new List<AssetOperationHandle>();
  73. _assetDic.Add(key, list);
  74. }
  75. list.Add(asset);
  76. }
  77. }
  78. }