GFGUIPackage.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using FairyGUI;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using VEngine;
  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<Asset>> _assetDic = new Dictionary<string, List<Asset>>();
  12. private static Dictionary<string, int> _assetCount = new Dictionary<string, int>();
  13. public static UIPackage AddPackage(string descFilePath)
  14. {
  15. int count = _assetCount.ContainsKey(descFilePath) ? _assetCount[descFilePath] : 0;
  16. if (_packages.ContainsKey(descFilePath))
  17. {
  18. _assetCount[descFilePath] = count + 1;
  19. return _packages[descFilePath];
  20. }
  21. var asset = Asset.Load($"{descFilePath}_fui.bytes", typeof(TextAsset));
  22. AddAsset(descFilePath, asset);
  23. var uiPackage = UIPackage.AddPackage((asset.asset as TextAsset).bytes, descFilePath, (string name, string extension, System.Type type, out DestroyMethod destroyMethod) =>
  24. {
  25. destroyMethod = DestroyMethod.Unload;
  26. string path = name + extension;
  27. if (Versions.Contains(path))
  28. {
  29. var asset = Asset.Load(path, type);
  30. AddAsset(descFilePath, asset);
  31. return asset.asset;
  32. }
  33. return null;
  34. });
  35. _assetCount.Add(descFilePath, count + 1);
  36. _packages.Add(descFilePath, uiPackage);
  37. return uiPackage;
  38. }
  39. public static void RemovePackage(string descFilePath)
  40. {
  41. UIPackage package;
  42. if (_packages.TryGetValue(descFilePath, out package))
  43. {
  44. int count = _assetCount[descFilePath] - 1;
  45. _assetCount[descFilePath] = count;
  46. if (count > 0) return;
  47. _assetCount.Remove(descFilePath);
  48. _packages.Remove(descFilePath);
  49. UIPackage.RemovePackage(package.name);
  50. List<Asset> list = null;
  51. if (_assetDic.TryGetValue(descFilePath, out list))
  52. {
  53. foreach (var asset in list)
  54. {
  55. asset.Release();
  56. }
  57. }
  58. }
  59. }
  60. private static void AddAsset(string key, Asset asset)
  61. {
  62. List<Asset> list = null;
  63. if (!_assetDic.TryGetValue(key, out list))
  64. {
  65. list = new List<Asset>();
  66. _assetDic.Add(key, list);
  67. }
  68. list.Add(asset);
  69. }
  70. }
  71. }