mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
资源模块优化-使用资源缓存表以及资源淘汰算法,缓存常用资源,利于获取、淘汰不常用资源。
资源模块优化-使用资源缓存表以及资源淘汰算法,缓存常用资源,利于获取、淘汰不常用资源。
This commit is contained in:
@@ -73,11 +73,6 @@ namespace TEngine
|
||||
/// </summary>
|
||||
public AssetOperationHandle Handle { protected set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 资源引用数据。
|
||||
/// </summary>
|
||||
public AssetReference AssetReference { protected set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 资源是否准备完毕。
|
||||
/// </summary>
|
||||
@@ -316,13 +311,7 @@ namespace TEngine
|
||||
/// <returns>UIWidget实例。</returns>
|
||||
public T CreateWidgetByPath<T>(Transform parentTrans, string assetLocation, bool visible = true) where T : UIWidget, new()
|
||||
{
|
||||
if (AssetReference == null)
|
||||
{
|
||||
Log.Fatal($"CreateWidgetByPath Failed => {this}.AssetReference is null");
|
||||
return null;
|
||||
}
|
||||
|
||||
GameObject goInst = AssetReference.LoadAsset<GameObject>(assetLocation, parentTrans);
|
||||
GameObject goInst = GameModule.Resource.LoadAsset<GameObject>(assetLocation, parentTrans);
|
||||
return CreateWidget<T>(goInst, visible);
|
||||
}
|
||||
|
||||
@@ -336,13 +325,7 @@ namespace TEngine
|
||||
/// <returns>UIWidget实例。</returns>
|
||||
public async UniTask<T> CreateWidgetByPathAsync<T>(Transform parentTrans, string assetLocation, bool visible = true) where T : UIWidget, new()
|
||||
{
|
||||
if (AssetReference == null)
|
||||
{
|
||||
Log.Fatal($"CreateWidgetByPath Failed => {this}.AssetReference is null");
|
||||
return null;
|
||||
}
|
||||
|
||||
GameObject goInst = await AssetReference.LoadAssetAsync<GameObject>(assetLocation, gameObject.GetCancellationTokenOnDestroy());
|
||||
GameObject goInst = await GameModule.Resource.LoadAssetAsync<GameObject>(assetLocation, gameObject.GetCancellationTokenOnDestroy());
|
||||
goInst.transform.SetParent(parentTrans);
|
||||
return CreateWidget<T>(goInst, visible);
|
||||
}
|
||||
@@ -531,163 +514,6 @@ namespace TEngine
|
||||
|
||||
#endregion
|
||||
|
||||
#region AssetRefrence Methods
|
||||
|
||||
/// <summary>
|
||||
/// 引用资源数据到资源组内。
|
||||
/// </summary>
|
||||
/// <param name="handle">资源操作句柄。</param>
|
||||
/// <param name="assetTag">资源标识。</param>
|
||||
/// <returns>是否注册成功。</returns>
|
||||
public bool Reference(AssetOperationHandle handle, string assetTag = "")
|
||||
{
|
||||
if (AssetReference == null)
|
||||
{
|
||||
Log.Fatal($"Reference Failed => {this}.AssetReference is null");
|
||||
return false;
|
||||
}
|
||||
|
||||
return AssetReference.Reference(handle, assetTag);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 引用资源数据到资源组内。
|
||||
/// </summary>
|
||||
/// <param name="address">资源定位地址。</param>
|
||||
/// <param name="handle">资源操作句柄。</param>
|
||||
/// <param name="assetTag">资源标识。</param>
|
||||
/// <returns>是否注册成功。</returns>
|
||||
public bool Reference(string address, AssetOperationHandle handle, string assetTag = "")
|
||||
{
|
||||
if (AssetReference == null)
|
||||
{
|
||||
Log.Fatal($"Reference Failed => {this}.AssetReference is null");
|
||||
return false;
|
||||
}
|
||||
|
||||
return AssetReference.Reference(address, handle, assetTag);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从资源组内释放资源数据。
|
||||
/// </summary>
|
||||
/// <param name="assetTag"></param>
|
||||
/// <returns></returns>
|
||||
public bool ReleaseByTag(string assetTag)
|
||||
{
|
||||
if (AssetReference == null)
|
||||
{
|
||||
Log.Fatal($"Release Failed => {this}.AssetReference is null");
|
||||
return false;
|
||||
}
|
||||
|
||||
return AssetReference.ReleaseByTag(assetTag);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从资源组内释放资源数据。
|
||||
/// </summary>
|
||||
/// <param name="handle"></param>
|
||||
/// <returns></returns>
|
||||
public bool Release(AssetOperationHandle handle)
|
||||
{
|
||||
if (AssetReference == null)
|
||||
{
|
||||
Log.Fatal($"Release Failed => {this}.AssetReference is null");
|
||||
return false;
|
||||
}
|
||||
|
||||
return AssetReference.Release(handle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从资源组内释放资源数据。
|
||||
/// </summary>
|
||||
/// <param name="address">资源定位地址。</param>
|
||||
/// <returns>是否释放成功。</returns>
|
||||
public bool Release(string address)
|
||||
{
|
||||
if (AssetReference == null)
|
||||
{
|
||||
Log.Fatal($"Release Failed => {this}.AssetReference is null");
|
||||
return false;
|
||||
}
|
||||
|
||||
return AssetReference.Release(address);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 同步加载资源。
|
||||
/// </summary>
|
||||
/// <param name="assetName">要加载资源的名称。</param>
|
||||
/// <typeparam name="T">要加载资源的类型。</typeparam>
|
||||
/// <returns>资源实例。</returns>
|
||||
public T LoadAsset<T>(string assetName) where T : UnityEngine.Object
|
||||
{
|
||||
if (AssetReference == null)
|
||||
{
|
||||
Log.Fatal($"LoadAsset Failed => {this}.AssetReference is null");
|
||||
return default;
|
||||
}
|
||||
|
||||
return AssetReference.LoadAsset<T>(assetName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 同步加载资源。
|
||||
/// </summary>
|
||||
/// <param name="assetName">要加载资源的名称。</param>
|
||||
/// <param name="parentTrans">父节点位置。</param>
|
||||
/// <typeparam name="T">要加载资源的类型。</typeparam>
|
||||
/// <returns>资源实例。</returns>
|
||||
public T LoadAsset<T>(string assetName, Transform parentTrans) where T : UnityEngine.Object
|
||||
{
|
||||
if (AssetReference == null)
|
||||
{
|
||||
Log.Fatal($"LoadAsset Failed => {this}.AssetReference is null");
|
||||
return default;
|
||||
}
|
||||
|
||||
return AssetReference.LoadAsset<T>(assetName, parentTrans);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载资源实例。
|
||||
/// </summary>
|
||||
/// <param name="assetName">要加载的实例名称。</param>
|
||||
/// <param name="cancellationToken">取消操作Token。</param>
|
||||
/// <returns>资源实实例。</returns>
|
||||
public async UniTask<T> LoadAssetAsync<T>(string assetName, CancellationToken cancellationToken)
|
||||
where T : UnityEngine.Object
|
||||
{
|
||||
if (AssetReference == null)
|
||||
{
|
||||
Log.Fatal($"LoadAssetAsync Failed => {this}.AssetReference is null");
|
||||
return default;
|
||||
}
|
||||
|
||||
return await AssetReference.LoadAssetAsync<T>(assetName, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载游戏物体。
|
||||
/// </summary>
|
||||
/// <param name="assetName">要加载的游戏物体名称。</param>
|
||||
/// <param name="cancellationToken">取消操作Token。</param>
|
||||
/// <returns>异步游戏物体实例。</returns>
|
||||
public async UniTask<GameObject> LoadGameObjectAsync(string assetName, CancellationToken cancellationToken)
|
||||
{
|
||||
if (AssetReference == null)
|
||||
{
|
||||
Log.Fatal($"LoadAssetAsync Failed => {this}.AssetReference is null");
|
||||
return default;
|
||||
}
|
||||
|
||||
return await AssetReference.LoadGameObjectAsync(assetName, cancellationToken);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region UIElement
|
||||
|
||||
/// <summary>
|
||||
|
@@ -149,7 +149,7 @@ namespace TEngine
|
||||
/// <returns></returns>
|
||||
public bool CreateByPath(string resPath, UIBase parentUI, Transform parentTrans = null, bool visible = true)
|
||||
{
|
||||
GameObject goInst = parentUI.AssetReference.LoadAsset<GameObject>(resPath, parentTrans);
|
||||
GameObject goInst = GameModule.Resource.LoadAsset<GameObject>(resPath, parentTrans);
|
||||
if (goInst == null)
|
||||
{
|
||||
return false;
|
||||
@@ -191,11 +191,6 @@ namespace TEngine
|
||||
return false;
|
||||
}
|
||||
|
||||
if (AssetReference == null)
|
||||
{
|
||||
AssetReference = AssetReference.BindAssetReference(widgetRoot, parent: parentUI.AssetReference);
|
||||
}
|
||||
|
||||
RestChildCanvas(parentUI);
|
||||
parent = parentUI;
|
||||
Parent.ListChild.Add(this);
|
||||
@@ -203,6 +198,7 @@ namespace TEngine
|
||||
BindMemberProperty();
|
||||
RegisterEvent();
|
||||
OnCreate();
|
||||
OnRefresh();
|
||||
IsPrepare = true;
|
||||
|
||||
if (!visible)
|
||||
@@ -269,14 +265,6 @@ namespace TEngine
|
||||
uiChild.OnDestroyWidget();
|
||||
}
|
||||
|
||||
if (Handle != null)
|
||||
{
|
||||
if (AssetReference != null && AssetReference.Parent != null)
|
||||
{
|
||||
AssetReference.Parent.Release(Handle);
|
||||
}
|
||||
}
|
||||
|
||||
if (gameObject != null)
|
||||
{
|
||||
Object.Destroy(gameObject);
|
||||
|
@@ -245,17 +245,11 @@ namespace TEngine
|
||||
|
||||
internal void InternalLoad(string location, System.Action<UIWindow> prepareCallback, System.Object[] userDatas)
|
||||
{
|
||||
if (Handle != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_prepareCallback = prepareCallback;
|
||||
this.userDatas = userDatas;
|
||||
if (!FromResources)
|
||||
{
|
||||
Handle = YooAssets.LoadAssetAsync<GameObject>(location);
|
||||
Handle.Completed += Handle_Completed;
|
||||
GameModule.Resource.LoadAssetAsync<GameObject>(location, Handle_Completed);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -369,13 +363,6 @@ namespace TEngine
|
||||
// 注销回调函数
|
||||
_prepareCallback = null;
|
||||
|
||||
// 卸载面板资源
|
||||
if (Handle != null)
|
||||
{
|
||||
Handle.Release();
|
||||
Handle = null;
|
||||
}
|
||||
|
||||
OnDestroy();
|
||||
|
||||
// 销毁面板对象
|
||||
@@ -393,11 +380,16 @@ namespace TEngine
|
||||
/// <exception cref="Exception"></exception>
|
||||
private void Handle_Completed(AssetOperationHandle handle)
|
||||
{
|
||||
if (handle == null)
|
||||
{
|
||||
throw new GameFrameworkException("Load uiWindows failed because AssetOperationHandle is null");
|
||||
}
|
||||
if (handle.AssetObject == null)
|
||||
{
|
||||
return;
|
||||
throw new GameFrameworkException("Load uiWindows Failed because AssetObject is null");
|
||||
}
|
||||
|
||||
Handle = handle;
|
||||
|
||||
// 实例化对象
|
||||
var panel = handle.InstantiateSync(UIModule.UIRootStatic);
|
||||
Handle_Completed(panel);
|
||||
@@ -417,9 +409,6 @@ namespace TEngine
|
||||
_panel = panel;
|
||||
_panel.transform.localPosition = Vector3.zero;
|
||||
|
||||
// 绑定引用
|
||||
AssetReference = AssetReference.BindAssetReference(_panel);
|
||||
|
||||
// 获取组件
|
||||
_canvas = _panel.GetComponent<Canvas>();
|
||||
if (_canvas == null)
|
||||
|
Reference in New Issue
Block a user