using FairyGUI; using System.Collections.Generic; using UnityEngine; using YooAsset; namespace GFGGame { public class GFGUIPackage { private static Dictionary _packages = new Dictionary(); private static Dictionary> _assetDic = new Dictionary>(); private static Dictionary _assetCount = new Dictionary(); 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($"{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 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 list = null; if (!_assetDic.TryGetValue(key, out list)) { list = new List(); _assetDic.Add(key, list); } list.Add(asset); } } }