using System; using FairyGUI; using System.Collections.Generic; using UnityEngine; using YooAsset; using System.Threading.Tasks; namespace GFGGame { public class GFGUIPackage { private static Dictionary _packages = new Dictionary(); private static Dictionary> assetHandleCacheDic = new Dictionary>(); private static Dictionary _assetCount = new Dictionary(); // public static void AddPackage(string descFilePath) // { // _assetCount.TryGetValue(descFilePath, out var count); // if (count > 0) // { // _assetCount[descFilePath] = count + 1; // return; // } // _assetCount.Add(descFilePath, count + 1); // var handle = YooAssets.LoadAssetSync($"{descFilePath}_fui.bytes"); // TextAsset textAsset = handle.AssetObject as TextAsset; // CacheAssetHandle(descFilePath, handle); // //同步 // var uiPackage = UIPackage.AddPackage(textAsset.bytes, descFilePath, (string name, string extension, System.Type type, out DestroyMethod destroyMethod) => // { // string location = name + extension; // destroyMethod = DestroyMethod.None; //注意:这里一定要设置为None // if (!YooAssets.CheckResExist(location)) // { // return null; // } // var handle = YooAssets.LoadAssetSync(location, type); // CacheAssetHandle(descFilePath, handle); // return handle.AssetObject; // }); // _packages.Add(descFilePath, uiPackage); // } public static void AddPackage(string descFilePath, Action onComplete = null, Action onError = null) { // 打印开始加载信息 _assetCount.TryGetValue(descFilePath, out var count); if (count > 0) { _assetCount[descFilePath] = count + 1; Debug.Log($"{descFilePath} 已引用,引用计数+1 (当前: {count + 1})"); onComplete?.Invoke(); return; } _assetCount.Add(descFilePath, count + 1); // 加载主资源 var handle = YooAssets.LoadAssetAsync($"{descFilePath}_fui.bytes"); handle.Completed += (mainHandle) => { if (mainHandle.Status != EOperationStatus.Succeed) { string error = $"描述文件加载失败: {descFilePath}_fui.bytes, 错误: {mainHandle.LastError}"; Debug.LogError(error); onError?.Invoke(error); return; } TextAsset textAsset = mainHandle.AssetObject as TextAsset; CacheAssetHandle(descFilePath, mainHandle); // 添加UI包 var uiPackage = UIPackage.AddPackage(textAsset.bytes, descFilePath, (string name, string extension, System.Type type, PackageItem item) => { string location = name + extension; if (!YooAssets.CheckResExist(location)) { Debug.LogWarning($"依赖资源不存在: {location}"); return; } AssetOperationHandle depHandle = YooAssets.LoadAssetAsync(location, type); CacheAssetHandle(descFilePath, depHandle); depHandle.Completed += (depHandle) => { if (depHandle.Status != EOperationStatus.Succeed) { Debug.LogError($"依赖资源加载失败: {location}"); return; } if (depHandle.AssetObject == null) { Debug.LogWarning($"依赖资源为空: {location}"); return; } Texture tex = depHandle.AssetObject as Texture; if (tex != null) { string alphaLocation = name + "!a" + extension; if (YooAssets.CheckResExist(alphaLocation)) { AssetOperationHandle alphaHandle = YooAssets.LoadAssetAsync(alphaLocation, type); CacheAssetHandle(descFilePath, alphaHandle); alphaHandle.Completed += (alphaHandle) => { if (alphaHandle.Status != EOperationStatus.Succeed) { Debug.LogError($"透明通道资源加载失败: {alphaLocation}"); return; } if (alphaHandle.AssetObject == null) { Debug.LogWarning($"透明通道资源为空: {alphaLocation}"); return; } Texture alphaTex = alphaHandle.AssetObject as Texture; item.owner.SetItemAsset(item, tex, alphaTex, DestroyMethod.None); }; return; } } item.owner.SetItemAsset(item, depHandle.AssetObject, null, DestroyMethod.None); }; }); _packages.Add(descFilePath, uiPackage); CheckRemovePackage(descFilePath); onComplete?.Invoke(); }; } public static void ReleasePackage(string descFilePath) { _assetCount.TryGetValue(descFilePath, out var count); if (count > 0) { count--; _assetCount[descFilePath] = count; CheckRemovePackage(descFilePath); } } private static void CheckRemovePackage(string descFilePath) { _assetCount.TryGetValue(descFilePath, out var count); if (count > 0) return; _packages.TryGetValue(descFilePath, out var package); if (package == null) return; _assetCount.Remove(descFilePath); _packages.Remove(descFilePath); UIPackage.RemovePackage(package.name); if (assetHandleCacheDic.TryGetValue(descFilePath, out var list)) { foreach (var asset in list) { asset.Release(); } list.Clear(); } } private static void CacheAssetHandle(string key, AssetOperationHandle handle) { List list = null; if (!assetHandleCacheDic.TryGetValue(key, out list)) { list = new List(); assetHandleCacheDic.Add(key, list); } list.Add(handle); } } }