Files
YooAsset/Assets/YooAsset/Runtime/AssetSystem/Handles/AssetOperationHandle.cs

99 lines
2.1 KiB
C#
Raw Normal View History

2022-03-21 23:43:11 +08:00
using UnityEngine;
2022-03-01 10:44:12 +08:00
namespace YooAsset
{
2022-03-21 23:43:11 +08:00
public class AssetOperationHandle : OperationHandleBase
2022-03-01 10:44:12 +08:00
{
2022-03-21 23:43:11 +08:00
private System.Action<AssetOperationHandle> _callback;
2022-03-01 10:44:12 +08:00
2022-03-22 20:47:22 +08:00
internal AssetOperationHandle(ProviderBase provider) : base(provider)
2022-03-01 10:44:12 +08:00
{
}
2022-03-21 23:43:11 +08:00
internal override void InvokeCallback()
2022-03-01 10:44:12 +08:00
{
2022-03-21 23:43:11 +08:00
if (IsValid)
2022-03-01 10:44:12 +08:00
{
2022-03-21 23:43:11 +08:00
_callback?.Invoke(this);
2022-03-01 10:44:12 +08:00
}
}
/// <summary>
/// 完成委托
/// </summary>
public event System.Action<AssetOperationHandle> Completed
{
add
{
if (IsValid == false)
throw new System.Exception($"{nameof(AssetOperationHandle)} is invalid");
if (_provider.IsDone)
value.Invoke(this);
else
2022-03-21 23:43:11 +08:00
_callback += value;
2022-03-01 10:44:12 +08:00
}
remove
{
if (IsValid == false)
throw new System.Exception($"{nameof(AssetOperationHandle)} is invalid");
2022-03-21 23:43:11 +08:00
_callback -= value;
2022-03-01 10:44:12 +08:00
}
}
/// <summary>
/// 资源对象
/// </summary>
public UnityEngine.Object AssetObject
{
get
{
if (IsValid == false)
return null;
return _provider.AssetObject;
}
}
/// <summary>
2022-03-23 12:44:56 +08:00
/// 同步初始化游戏对象
2022-03-01 10:44:12 +08:00
/// </summary>
2022-03-23 12:44:56 +08:00
public GameObject InstantiateSync(Vector3 position, Quaternion rotation, Transform parent = null)
2022-03-01 10:44:12 +08:00
{
2022-03-23 12:44:56 +08:00
if (IsValid == false)
return null;
if (_provider.AssetObject == null)
return null;
if (parent == null)
return UnityEngine.Object.Instantiate(_provider.AssetObject as GameObject, position, rotation);
else
return UnityEngine.Object.Instantiate(_provider.AssetObject as GameObject, position, rotation, parent);
}
/// <summary>
/// 异步初始化游戏对象
/// </summary>
public InstantiateOperation InstantiateAsync(Vector3 position, Quaternion rotation, Transform parent = null)
{
InstantiateOperation operation = new InstantiateOperation(this, position, rotation, parent);
OperationSystem.ProcessOperaiton(operation);
return operation;
2022-03-01 10:44:12 +08:00
}
/// <summary>
/// 等待异步执行完毕
/// </summary>
public void WaitForAsyncComplete()
{
if (IsValid == false)
return;
_provider.WaitForAsyncComplete();
}
2022-03-22 20:15:59 +08:00
/// <summary>
/// 释放资源句柄
/// </summary>
public void Release()
{
this.ReleaseInternal();
}
2022-03-01 10:44:12 +08:00
}
}