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

157 lines
4.0 KiB
C#
Raw Normal View History

2022-03-21 23:43:11 +08:00
using UnityEngine;
using System.Collections.Generic;
2022-03-01 10:44:12 +08:00
namespace YooAsset
{
public sealed 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
{
_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");
2022-05-02 17:54:55 +08:00
if (Provider.IsDone)
2022-03-01 10:44:12 +08:00
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;
2022-05-02 17:54:55 +08:00
return Provider.AssetObject;
2022-03-01 10:44:12 +08:00
}
}
2022-06-30 12:12:44 +08:00
/// <summary>
/// 获取资源对象
/// </summary>
/// <typeparam name="TAsset">资源类型</typeparam>
2022-06-30 14:09:07 +08:00
public TAsset GetAssetObject<TAsset>() where TAsset : UnityEngine.Object
2022-06-30 12:12:44 +08:00
{
if (IsValid == false)
return null;
return Provider.AssetObject as TAsset;
}
2022-03-23 14:58:13 +08:00
/// <summary>
/// 等待异步执行完毕
/// </summary>
2022-06-30 12:12:44 +08:00
public void WaitForAsyncComplete()
2022-03-23 14:58:13 +08:00
{
if (IsValid == false)
2022-06-30 12:12:44 +08:00
return;
2022-05-02 17:54:55 +08:00
Provider.WaitForAsyncComplete();
2022-03-23 14:58:13 +08:00
}
2022-06-30 12:12:44 +08:00
/// <summary>
/// 释放资源句柄
/// </summary>
public void Release()
2022-03-23 14:58:13 +08:00
{
this.ReleaseInternal();
}
2022-03-01 10:44:12 +08:00
/// <summary>
2022-03-23 12:44:56 +08:00
/// 同步初始化游戏对象
2022-03-01 10:44:12 +08:00
/// </summary>
2022-03-23 14:58:13 +08:00
/// <param name="parent">父类对象</param>
/// <returns></returns>
public GameObject InstantiateSync(Transform parent = null)
{
return InstantiateSyncInternal(Vector3.zero, Quaternion.identity, parent, false);
2022-03-23 14:58:13 +08:00
}
/// <summary>
/// 同步初始化游戏对象
/// </summary>
/// <param name="position">坐标</param>
/// <param name="rotation">角度</param>
/// <param name="parent">父类对象</param>
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
{
return InstantiateSyncInternal(position, rotation, parent, true);
2022-03-23 12:44:56 +08:00
}
/// <summary>
/// 异步初始化游戏对象
/// </summary>
2022-03-23 14:58:13 +08:00
/// <param name="parent">父类对象</param>
public InstantiateOperation InstantiateAsync(Transform parent = null)
2022-03-23 12:44:56 +08:00
{
return InstantiateAsyncInternal(Vector3.zero, Quaternion.identity, parent, false);
2022-03-01 10:44:12 +08:00
}
/// <summary>
2022-03-23 14:58:13 +08:00
/// 异步初始化游戏对象
2022-03-22 20:15:59 +08:00
/// </summary>
2022-03-23 14:58:13 +08:00
/// <param name="position">坐标</param>
/// <param name="rotation">角度</param>
/// <param name="parent">父类对象</param>
public InstantiateOperation InstantiateAsync(Vector3 position, Quaternion rotation, Transform parent = null)
2022-03-22 20:15:59 +08:00
{
return InstantiateAsyncInternal(position, rotation, parent, true);
}
2022-06-30 12:12:44 +08:00
private GameObject InstantiateSyncInternal(Vector3 position, Quaternion rotation, Transform parent, bool setPositionRotation)
{
if (IsValid == false)
return null;
2022-05-02 17:54:55 +08:00
if (Provider.AssetObject == null)
return null;
GameObject result;
if (setPositionRotation)
{
if (parent == null)
result = UnityEngine.Object.Instantiate(Provider.AssetObject as GameObject, position, rotation);
else
result = UnityEngine.Object.Instantiate(Provider.AssetObject as GameObject, position, rotation, parent);
}
else
{
if (parent == null)
result = UnityEngine.Object.Instantiate(Provider.AssetObject as GameObject);
else
result = UnityEngine.Object.Instantiate(Provider.AssetObject as GameObject, parent);
}
return result;
}
private InstantiateOperation InstantiateAsyncInternal(Vector3 position, Quaternion rotation, Transform parent, bool setPositionRotation)
{
InstantiateOperation operation = new InstantiateOperation(this, position, rotation, parent, setPositionRotation);
OperationSystem.StartOperaiton(operation);
2022-03-23 14:58:13 +08:00
return operation;
2022-03-22 20:15:59 +08:00
}
2022-03-01 10:44:12 +08:00
}
}