using System;
using YooAsset;
namespace ET.Client
{
///
/// YIUI资源管理器 yooasset扩展
///
[FriendOf(typeof(YIUIYooAssetsLoadComponent))]
[EntitySystemOf(typeof(YIUIYooAssetsLoadComponent))]
public static partial class YIUIYooAssetsLoadComponentSystem
{
#region ObjectSystem
[EntitySystem]
private static void Awake(this YIUIYooAssetsLoadComponent self)
{
}
[EntitySystem]
private static void Destroy(this YIUIYooAssetsLoadComponent self)
{
self.ReleaseAllAction();
}
#endregion
public static async ETTask Initialize(this YIUIYooAssetsLoadComponent self, string packageName = "DefaultPackage")
{
self.m_Package = YooAssets.GetPackage(packageName);
if (self.m_Package == null)
{
Log.Error($"YooAsset 加载资源包失败: {packageName}");
return false;
}
//YIUI会用到的各种加载 需要自行实现 当前是YooAsset 根据自己项目的资源管理器实现下面的方法
#if !YIUIMACRO_SYNCLOAD_CLOSE
YIUILoadDI.LoadAssetFunc = self.LoadAssetFunc; //同步加载
#endif
YIUILoadDI.LoadAssetAsyncFunc = self.LoadAssetAsyncFunc; //异步加载
YIUILoadDI.ReleaseAction = self.ReleaseAction; //释放
YIUILoadDI.VerifyAssetValidityFunc = self.VerifyAssetValidityFunc; //检查
YIUILoadDI.ReleaseAllAction = self.ReleaseAllAction; //释放所有
await EventSystem.Instance.PublishAsync(self.Scene(), new YIUIEvent_YooAssetsLoad_Completed
{
YooAssetsLoadRef = self
});
return true;
}
///
/// 释放方法
///
/// 加载时所给到的唯一ID
private static void ReleaseAction(this YIUIYooAssetsLoadComponent self, int hashCode)
{
if (!self.m_AllHandle.TryGetValue(hashCode, out var value))
{
return;
}
value.Release();
self.m_AllHandle.Remove(hashCode);
}
///
/// 异步加载
///
/// 包名
/// 资源名
/// 类型
/// 返回值(obj资源对象,唯一ID)
private static async ETTask<(UnityEngine.Object, int)> LoadAssetAsyncFunc(this YIUIYooAssetsLoadComponent self,
string arg1,
string arg2,
Type arg3)
{
EntityRef selfRef = self;
var handle = self.m_Package.LoadAssetAsync(arg2, arg3);
await handle.Task;
self = selfRef;
return self.LoadAssetHandle(handle);
}
#if !YIUIMACRO_SYNCLOAD_CLOSE
///
/// 同步加载
///
/// 包名
/// 资源名
/// 类型
/// 返回值(obj资源对象,唯一ID)
private static (UnityEngine.Object, int) LoadAssetFunc(this YIUIYooAssetsLoadComponent self, string arg1, string arg2, Type arg3)
{
var handle = self.m_Package.LoadAssetSync(arg2, arg3);
return self.LoadAssetHandle(handle);
}
#endif
//Demo中对YooAsset加载后的一个简单返回封装
//只有成功加载才返回 否则直接释放
private static (UnityEngine.Object, int) LoadAssetHandle(this YIUIYooAssetsLoadComponent self, AssetHandle handle)
{
if (handle.AssetObject != null)
{
var hashCode = handle.GetHashCode();
self.m_AllHandle.Add(hashCode, handle);
return (handle.AssetObject, hashCode);
}
else
{
handle.Release();
return (null, 0);
}
}
//释放所有
private static void ReleaseAllAction(this YIUIYooAssetsLoadComponent self)
{
foreach (var handle in self.m_AllHandle.Values)
{
handle.Release();
}
self.m_AllHandle.Clear();
}
//检查合法
private static bool VerifyAssetValidityFunc(this YIUIYooAssetsLoadComponent self, string arg1, string arg2)
{
return self.m_Package.CheckLocationValid(arg2);
}
///
/// 获取当前的资源包
///
public static ResourcePackage GetPackage(this YIUIYooAssetsLoadComponent self)
{
return self.m_Package;
}
///
/// 获取资源信息
///
/// 资源的定位地址
/// 资源类型
public static AssetInfo GetAssetInfo(this YIUIYooAssetsLoadComponent self, string location, Type type = null)
{
return self.m_Package?.GetAssetInfo(location, type);
}
///
/// 获取资源信息
///
/// 资源GUID
/// 资源类型
public static AssetInfo GetAssetInfoByGUID(this YIUIYooAssetsLoadComponent self, string assetGUID, Type type = null)
{
return self.m_Package?.GetAssetInfoByGUID(assetGUID, type);
}
}
}