| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- using System;
- using FairyGUI;
- using System.Collections.Generic;
- using UnityEngine;
- using YooAsset;
- using System.Threading.Tasks;
- namespace GFGGame
- {
- public class GFGUIPackage
- {
- private static Dictionary<string, UIPackage> _packages = new Dictionary<string, UIPackage>();
- private static Dictionary<string, List<AssetOperationHandle>> assetHandleCacheDic =
- new Dictionary<string, List<AssetOperationHandle>>();
- private static Dictionary<string, int> _assetCount = new Dictionary<string, int>();
- // 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<TextAsset>($"{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 async Task AddPackageAsync(string descFilePath)
- {
- // 打印开始加载信息
- Debug.Log($"开始加载UI包: {descFilePath}");
- _assetCount.TryGetValue(descFilePath, out var count);
- if (count > 0)
- {
- _assetCount[descFilePath] = count + 1;
- Debug.Log($"{descFilePath} 已引用,引用计数+1 (当前: {count + 1})");
- return;
- }
- _assetCount.Add(descFilePath, count + 1);
- try
- {
- // 加载主资源
- Debug.Log($"正在加载描述文件: {descFilePath}_fui.bytes");
- var handle = YooAssets.LoadAssetAsync<TextAsset>($"{descFilePath}_fui.bytes");
- await handle.Task;
- if (handle.Status == EOperationStatus.Succeed)
- {
- Debug.Log($"描述文件加载成功: {descFilePath}_fui.bytes");
- TextAsset textAsset = handle.AssetObject as TextAsset;
- CacheAssetHandle(descFilePath, handle);
- // 添加UI包
- Debug.Log($"正在添加UI包: {descFilePath}");
- var uiPackage = UIPackage.AddPackage(textAsset.bytes, descFilePath,
- (string name, string extension, System.Type type, PackageItem item) =>
- {
- string location = name + extension;
- Debug.Log($"正在加载依赖资源: {location}");
- if (!YooAssets.CheckResExist(location))
- {
- Debug.LogWarning($"依赖资源不存在: {location}");
- return;
- }
- AssetOperationHandle handle = YooAssets.LoadAssetAsync(location, type);
- CacheAssetHandle(descFilePath, handle);
- handle.Completed += (handle) =>
- {
- if (handle.Status != EOperationStatus.Succeed)
- {
- Debug.LogError($"依赖资源加载失败: {location}");
- return;
- }
- if (handle.AssetObject == null)
- {
- Debug.LogWarning($"依赖资源为空: {location}");
- return;
- }
- Debug.Log($"依赖资源加载成功: {location}");
- Texture tex = handle.AssetObject as Texture;
- if (tex != null)
- {
- string location = name + "!a" + extension;
- Debug.Log($"检测透明通道资源: {location}");
- if (YooAssets.CheckResExist(location))
- {
- AssetOperationHandle handleAlpha = YooAssets.LoadAssetAsync(location, type);
- CacheAssetHandle(descFilePath, handleAlpha);
- handleAlpha.Completed += (handle1) =>
- {
- if (handle1.Status != EOperationStatus.Succeed)
- {
- Debug.LogError($"透明通道资源加载失败: {location}");
- return;
- }
- if (handle1.AssetObject == null)
- {
- Debug.LogWarning($"透明通道资源为空: {location}");
- return;
- }
- Debug.Log($"透明通道资源加载成功: {location}");
- Texture alphaTex = handle1.AssetObject as Texture;
- item.owner.SetItemAsset(item, tex, alphaTex, DestroyMethod.None);
- };
- return;
- }
- }
- item.owner.SetItemAsset(item, handle.AssetObject, null, DestroyMethod.None);
- };
- });
- _packages.Add(descFilePath, uiPackage);
- Debug.Log($"UI包添加完成: {descFilePath}");
- CheckRemovePackage(descFilePath);
- }
- else
- {
- Debug.LogError($"描述文件加载失败: {descFilePath}_fui.bytes, 错误: {handle.LastError}");
- }
- }
- catch (Exception ex)
- {
- Debug.LogError($"加载UI包异常: {descFilePath}, 错误: {ex.Message}");
- throw;
- }
- }
- 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<AssetOperationHandle> list = null;
- if (!assetHandleCacheDic.TryGetValue(key, out list))
- {
- list = new List<AssetOperationHandle>();
- assetHandleCacheDic.Add(key, list);
- }
- list.Add(handle);
- }
- }
- }
|