mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
增加泛用加载资源并绑定资源引用到GameObject上。
增加泛用加载资源并绑定资源引用到GameObject上。
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngineInternal;
|
||||
|
||||
@@ -168,11 +169,13 @@ namespace TEngine
|
||||
operation = null;
|
||||
return;
|
||||
}
|
||||
|
||||
image.sprite = operation.AssetObject as Sprite;
|
||||
if (isSetNativeSize)
|
||||
{
|
||||
image.SetNativeSize();
|
||||
}
|
||||
|
||||
image.gameObject.GetOrAddComponent<AssetReference>().Reference(operation, spriteName);
|
||||
}, customPackageName: customPackageName);
|
||||
}
|
||||
@@ -204,7 +207,7 @@ namespace TEngine
|
||||
{
|
||||
var operation = GameModule.Resource.LoadAssetGetOperation<Sprite>(spriteName, customPackageName: customPackageName);
|
||||
spriteRenderer.sprite = operation.AssetObject as Sprite;
|
||||
|
||||
|
||||
spriteRenderer.gameObject.GetOrAddComponent<AssetReference>().Reference(operation, spriteName);
|
||||
}
|
||||
else
|
||||
@@ -300,5 +303,62 @@ namespace TEngine
|
||||
|
||||
return gameObject.GetComponent<AssetReference>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载资源并绑定资源引用到GameObject上。
|
||||
/// </summary>
|
||||
/// <param name="gameObject">GameObject。</param>
|
||||
/// <param name="location">资源定位地址。</param>
|
||||
/// <param name="callBack">加载完成回调。</param>
|
||||
/// <param name="customPackageName">自定义包。</param>
|
||||
/// <typeparam name="T">资源实例类型。</typeparam>
|
||||
/// <returns>资源实例。</returns>
|
||||
public static T LoadAsset<T>(this GameObject gameObject, string location, Action<T> callBack = null,
|
||||
string customPackageName = "") where T : UnityEngine.Object
|
||||
{
|
||||
if (gameObject == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var operation = GameModule.Resource.LoadAssetGetOperation<T>(location, customPackageName: customPackageName);
|
||||
var asset = operation.AssetObject as T;
|
||||
gameObject.GetOrAddComponent<AssetReference>().Reference(operation, location);
|
||||
callBack?.Invoke(asset);
|
||||
return asset;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载资源并绑定资源引用到GameObject上。
|
||||
/// </summary>
|
||||
/// <param name="gameObject">GameObject。</param>
|
||||
/// <param name="location">资源定位地址。</param>
|
||||
/// <param name="callBack">加载完成回调。</param>
|
||||
/// <param name="customPackageName">自定义包。</param>
|
||||
/// <typeparam name="T">资源实例类型。</typeparam>
|
||||
/// <returns>资源实例。</returns>
|
||||
public static async UniTask<T> LoadAssetAsync<T>(this GameObject gameObject, string location, Action<T> callBack = null,
|
||||
string customPackageName = "") where T : UnityEngine.Object
|
||||
{
|
||||
if (gameObject == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var operation = GameModule.Resource.LoadAssetAsyncHandle<T>(location, customPackageName: customPackageName);
|
||||
|
||||
bool cancelOrFailed = await operation.ToUniTask().AttachExternalCancellation(gameObject.GetCancellationTokenOnDestroy())
|
||||
.SuppressCancellationThrow();
|
||||
|
||||
if (cancelOrFailed)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
gameObject.GetOrAddComponent<AssetReference>().Reference(operation, location);
|
||||
var asset = operation.AssetObject as T;
|
||||
callBack?.Invoke(asset);
|
||||
return asset;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user