|
|
@@ -9,9 +9,15 @@ 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, List<AssetOperationHandle>> assetHandleCacheDic =
|
|
|
+ new Dictionary<string, List<AssetOperationHandle>>();
|
|
|
+
|
|
|
private static Dictionary<string, int> _assetCount = new Dictionary<string, int>();
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 同步加载 FairyGUI 包
|
|
|
+ /// </summary>
|
|
|
public static void AddPackage(string descFilePath)
|
|
|
{
|
|
|
_assetCount.TryGetValue(descFilePath, out var count);
|
|
|
@@ -20,126 +26,133 @@ namespace GFGGame
|
|
|
_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;
|
|
|
- });
|
|
|
+ var uiPackage = UIPackage.AddPackage(textAsset.bytes, descFilePath, LoadResourceSync);
|
|
|
_packages.Add(descFilePath, uiPackage);
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 异步加载 FairyGUI 包(支持 await)
|
|
|
+ /// </summary>
|
|
|
public static async Task AddPackageAsync(string descFilePath)
|
|
|
{
|
|
|
- Debug.Log($"正在初始化 descFilePath:{descFilePath} 1");
|
|
|
_assetCount.TryGetValue(descFilePath, out var count);
|
|
|
if (count > 0)
|
|
|
{
|
|
|
_assetCount[descFilePath] = count + 1;
|
|
|
return;
|
|
|
}
|
|
|
+
|
|
|
_assetCount.Add(descFilePath, count + 1);
|
|
|
- Debug.Log($"正在初始化 descFilePath:{descFilePath} 2");
|
|
|
+
|
|
|
+ // 1. 异步加载描述文件
|
|
|
var handle = YooAssets.LoadAssetAsync<TextAsset>($"{descFilePath}_fui.bytes");
|
|
|
- Debug.Log($"正在初始化 descFilePath:{descFilePath} 3");
|
|
|
await handle.Task;
|
|
|
+ if (handle.Status != EOperationStatus.Succeed)
|
|
|
+ throw new System.Exception($"Failed to load package: {descFilePath}");
|
|
|
+
|
|
|
+ // 2. 添加包
|
|
|
TextAsset textAsset = handle.AssetObject as TextAsset;
|
|
|
CacheAssetHandle(descFilePath, handle);
|
|
|
- var uiPackage = UIPackage.AddPackage(textAsset.bytes, descFilePath, (string name, string extension, System.Type type, PackageItem item) =>
|
|
|
+ var uiPackage = UIPackage.AddPackage(textAsset.bytes, descFilePath, LoadResourceAsync);
|
|
|
+ _packages.Add(descFilePath, uiPackage);
|
|
|
+
|
|
|
+ CheckRemovePackage(descFilePath);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 同步加载资源回调
|
|
|
+ /// </summary>
|
|
|
+ private static object LoadResourceSync(string name, string extension, System.Type type,
|
|
|
+ out DestroyMethod destroyMethod)
|
|
|
+ {
|
|
|
+ destroyMethod = DestroyMethod.None;
|
|
|
+ string location = name + extension;
|
|
|
+ if (!YooAssets.CheckResExist(location))
|
|
|
+ return null;
|
|
|
+
|
|
|
+ var handle = YooAssets.LoadAssetSync(location, type);
|
|
|
+ CacheAssetHandle(name, handle);
|
|
|
+ return handle.AssetObject;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 异步加载资源回调
|
|
|
+ /// </summary>
|
|
|
+ private static void LoadResourceAsync(string name, string extension, System.Type type, PackageItem item)
|
|
|
+ {
|
|
|
+ string location = name + extension;
|
|
|
+ if (!YooAssets.CheckResExist(location))
|
|
|
+ return;
|
|
|
+
|
|
|
+ AssetOperationHandle handle = YooAssets.LoadAssetAsync(location, type);
|
|
|
+ CacheAssetHandle(name, handle);
|
|
|
+ handle.Completed += (op) =>
|
|
|
{
|
|
|
- Debug.Log($"正在初始化 descFilePath:{descFilePath} 4");
|
|
|
- string location = name + extension;
|
|
|
- if (!YooAssets.CheckResExist(location))
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
- AssetOperationHandle handle = YooAssets.LoadAssetAsync(location, type);
|
|
|
- CacheAssetHandle(descFilePath, handle);
|
|
|
- handle.Completed += (handle) =>
|
|
|
+ if (op.AssetObject == null) return;
|
|
|
+
|
|
|
+ // 处理带 Alpha 通道的纹理
|
|
|
+ if (op.AssetObject is Texture tex)
|
|
|
{
|
|
|
- Debug.Log($"正在初始化 descFilePath:{descFilePath} 5");
|
|
|
- if (handle.AssetObject == null) return;
|
|
|
- Texture tex = handle.AssetObject as Texture;
|
|
|
- if (tex != null)
|
|
|
+ string alphaLocation = name + "!a" + extension;
|
|
|
+ if (YooAssets.CheckResExist(alphaLocation))
|
|
|
{
|
|
|
- Debug.Log($"正在初始化 descFilePath:{descFilePath} 6");
|
|
|
- string location = name + "!a" + extension;
|
|
|
- if (YooAssets.CheckResExist(location))
|
|
|
+ var alphaHandle = YooAssets.LoadAssetAsync(alphaLocation, type);
|
|
|
+ CacheAssetHandle(name, alphaHandle);
|
|
|
+ alphaHandle.Completed += (alphaOp) =>
|
|
|
{
|
|
|
- Debug.Log($"正在初始化 descFilePath:{descFilePath} 7");
|
|
|
- AssetOperationHandle handleAlpha = YooAssets.LoadAssetAsync(location, type);
|
|
|
- CacheAssetHandle(descFilePath, handleAlpha);
|
|
|
- handleAlpha.Completed += (handle1) =>
|
|
|
- {
|
|
|
- Debug.Log($"正在初始化 descFilePath:{descFilePath} 8");
|
|
|
- if (handle1.AssetObject == null) return;
|
|
|
- Texture alphaTex = handle1.AssetObject as Texture;
|
|
|
- item.owner.SetItemAsset(item, tex, alphaTex, DestroyMethod.None);//注意:这里一定要设置为None
|
|
|
- Debug.Log($"正在初始化 descFilePath:{descFilePath} 9");
|
|
|
- };
|
|
|
- return;
|
|
|
- }
|
|
|
+ if (alphaOp.AssetObject is Texture alphaTex)
|
|
|
+ item.owner.SetItemAsset(item, tex, alphaTex, DestroyMethod.None);
|
|
|
+ };
|
|
|
+ return;
|
|
|
}
|
|
|
- Debug.Log($"正在初始化 descFilePath:{descFilePath} 10");
|
|
|
- item.owner.SetItemAsset(item, handle.AssetObject, null, DestroyMethod.None);//注意:这里一定要设置为None
|
|
|
- };
|
|
|
- });
|
|
|
- Debug.Log($"正在初始化 descFilePath:{descFilePath} 11");
|
|
|
- _packages.Add(descFilePath, uiPackage);
|
|
|
- CheckRemovePackage(descFilePath);
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
+ item.owner.SetItemAsset(item, op.AssetObject, null, DestroyMethod.None);
|
|
|
+ };
|
|
|
+ }
|
|
|
|
|
|
public static void ReleasePackage(string descFilePath)
|
|
|
{
|
|
|
- _assetCount.TryGetValue(descFilePath, out var count);
|
|
|
- if (count > 0)
|
|
|
+ if (_assetCount.TryGetValue(descFilePath, out var count) && count > 0)
|
|
|
{
|
|
|
- count--;
|
|
|
- _assetCount[descFilePath] = count;
|
|
|
+ _assetCount[descFilePath] = count - 1;
|
|
|
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))
|
|
|
+ if (_assetCount.TryGetValue(descFilePath, out var count) && count <= 0)
|
|
|
{
|
|
|
- foreach (var asset in list)
|
|
|
+ if (_packages.TryGetValue(descFilePath, out var package))
|
|
|
{
|
|
|
- asset.Release();
|
|
|
+ UIPackage.RemovePackage(package.name);
|
|
|
+ _packages.Remove(descFilePath);
|
|
|
+ _assetCount.Remove(descFilePath);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (assetHandleCacheDic.TryGetValue(descFilePath, out var handles))
|
|
|
+ {
|
|
|
+ foreach (var handle in handles)
|
|
|
+ handle.Release();
|
|
|
+ handles.Clear();
|
|
|
}
|
|
|
- list.Clear();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private static void CacheAssetHandle(string key, AssetOperationHandle handle)
|
|
|
{
|
|
|
- List<AssetOperationHandle> list = null;
|
|
|
- if (!assetHandleCacheDic.TryGetValue(key, out list))
|
|
|
+ if (!assetHandleCacheDic.TryGetValue(key, out var list))
|
|
|
{
|
|
|
list = new List<AssetOperationHandle>();
|
|
|
assetHandleCacheDic.Add(key, list);
|
|
|
}
|
|
|
+
|
|
|
list.Add(handle);
|
|
|
}
|
|
|
}
|