12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- using FairyGUI;
- using System.Collections.Generic;
- using UnityEngine;
- using YooAsset;
- namespace GFGGame
- {
- public class GFGUIPackage
- {
- private static Dictionary<string, UIPackage> _packages = new Dictionary<string, UIPackage>();
- private static Dictionary<string, List<AssetOperationHandle>> _assetDic = new Dictionary<string, List<AssetOperationHandle>>();
- private static Dictionary<string, int> _assetCount = new Dictionary<string, int>();
- public static UIPackage AddPackage(string descFilePath)
- {
- int count = _assetCount.ContainsKey(descFilePath) ? _assetCount[descFilePath] : 0;
- if (_packages.ContainsKey(descFilePath))
- {
- _assetCount[descFilePath] = count + 1;
- return _packages[descFilePath];
- }
- //var asset = Asset.Load($"{descFilePath}_fui.bytes", typeof(TextAsset));
- var handle = YooAssets.LoadAssetSync<TextAsset>($"{descFilePath}_fui.bytes");
- TextAsset textAsset = handle.AssetObject as TextAsset;
- AddAsset(descFilePath, handle);
- var uiPackage = UIPackage.AddPackage(textAsset.bytes, descFilePath, (string name, string extension, System.Type type, out DestroyMethod destroyMethod) =>
- {
- //destroyMethod = DestroyMethod.Unload;
- string path = name + extension;
- //if (Versions.Contains(path))
- //{
- // var asset = Asset.Load(path, type);
- // AddAsset(descFilePath, asset);
- // return asset.asset;
- //}
- //return null;
- destroyMethod = DestroyMethod.None; //注意:这里一定要设置为None
- string location = path;
- var handle = YooAssets.LoadAssetSync(location, type);
- AddAsset(descFilePath, handle);
- return handle.AssetObject;
- });
- _assetCount.Add(descFilePath, count + 1);
- _packages.Add(descFilePath, uiPackage);
- return uiPackage;
- }
- public static void RemovePackage(string descFilePath)
- {
- UIPackage package;
- if (_packages.TryGetValue(descFilePath, out package))
- {
- int count = _assetCount[descFilePath] - 1;
- _assetCount[descFilePath] = count;
- if (count > 0) return;
- _assetCount.Remove(descFilePath);
- _packages.Remove(descFilePath);
- UIPackage.RemovePackage(package.name);
- List<AssetOperationHandle> list = null;
- if (_assetDic.TryGetValue(descFilePath, out list))
- {
- foreach (var asset in list)
- {
- asset.Release();
- }
- list.Clear();
- }
- }
- }
- private static void AddAsset(string key, AssetOperationHandle asset)
- {
- List<AssetOperationHandle> list = null;
- if (!_assetDic.TryGetValue(key, out list))
- {
- list = new List<AssetOperationHandle>();
- _assetDic.Add(key, list);
- }
- list.Add(asset);
- }
- }
- }
|