Files
YooAsset/Assets/YooAsset/Runtime/AssetSystem/Provider/ProviderBase.cs

234 lines
4.7 KiB
C#
Raw Normal View History

2022-03-21 23:43:11 +08:00
using System.Collections;
using System.Collections.Generic;
2022-03-01 10:44:12 +08:00
namespace YooAsset
{
2022-03-22 20:47:22 +08:00
internal abstract class ProviderBase
2022-03-01 10:44:12 +08:00
{
2022-03-09 23:57:04 +08:00
public enum EStatus
{
None = 0,
CheckBundle,
Loading,
Checking,
Success,
Fail,
}
2022-03-21 23:43:11 +08:00
/// <summary>
/// 资源路径
/// </summary>
2022-03-01 10:44:12 +08:00
public string AssetPath { private set; get; }
2022-03-21 23:43:11 +08:00
/// <summary>
/// 资源对象的名称
/// </summary>
2022-03-01 10:44:12 +08:00
public string AssetName { private set; get; }
2022-03-21 23:43:11 +08:00
/// <summary>
/// 资源对象的类型
/// </summary>
2022-03-01 10:44:12 +08:00
public System.Type AssetType { private set; get; }
2022-03-21 23:43:11 +08:00
/// <summary>
/// 获取的资源对象
/// </summary>
2022-03-01 10:44:12 +08:00
public UnityEngine.Object AssetObject { protected set; get; }
2022-03-21 23:43:11 +08:00
/// <summary>
/// 获取的资源对象集合
/// </summary>
2022-03-01 10:44:12 +08:00
public UnityEngine.Object[] AllAssets { protected set; get; }
2022-03-21 23:43:11 +08:00
/// <summary>
/// 获取的场景对象
/// </summary>
2022-03-22 20:15:59 +08:00
public UnityEngine.SceneManagement.Scene SceneObject { protected set; get; }
2022-03-21 23:43:11 +08:00
/// <summary>
/// 当前的加载状态
/// </summary>
public EStatus Status { protected set; get; } = EStatus.None;
/// <summary>
/// 引用计数
/// </summary>
public int RefCount { private set; get; } = 0;
/// <summary>
/// 是否已经销毁
/// </summary>
2022-03-01 10:44:12 +08:00
public bool IsDestroyed { private set; get; } = false;
2022-03-21 23:43:11 +08:00
/// <summary>
/// 是否完毕(成功或失败)
/// </summary>
2022-03-01 10:44:12 +08:00
public bool IsDone
{
get
{
2022-03-09 23:57:04 +08:00
return Status == EStatus.Success || Status == EStatus.Fail;
2022-03-01 10:44:12 +08:00
}
}
2022-03-21 23:43:11 +08:00
/// <summary>
/// 加载进度
/// </summary>
2022-03-01 10:44:12 +08:00
public virtual float Progress
{
get
{
return 0;
}
}
2022-03-21 23:43:11 +08:00
protected bool IsWaitForAsyncComplete { private set; get; } = false;
private readonly List<OperationHandleBase> _handles = new List<OperationHandleBase>();
2022-03-01 10:44:12 +08:00
2022-03-22 20:47:22 +08:00
public ProviderBase(string assetPath, System.Type assetType)
2022-03-01 10:44:12 +08:00
{
AssetPath = assetPath;
AssetName = System.IO.Path.GetFileName(assetPath);
AssetType = assetType;
}
2022-03-21 23:43:11 +08:00
/// <summary>
/// 轮询更新方法
/// </summary>
2022-03-01 10:44:12 +08:00
public abstract void Update();
2022-03-21 23:43:11 +08:00
/// <summary>
/// 销毁资源对象
/// </summary>
2022-03-01 10:44:12 +08:00
public virtual void Destory()
{
IsDestroyed = true;
}
2022-03-22 20:15:59 +08:00
/// <summary>
/// 是否可以销毁
/// </summary>
public bool CanDestroy()
{
if (IsDone == false)
return false;
return RefCount <= 0;
}
2022-03-21 23:43:11 +08:00
/// <summary>
/// 创建操作句柄
/// </summary>
/// <returns></returns>
public OperationHandleBase CreateHandle()
2022-03-01 10:44:12 +08:00
{
2022-03-21 23:43:11 +08:00
// 引用计数增加
2022-03-01 10:44:12 +08:00
RefCount++;
2022-03-21 23:43:11 +08:00
OperationHandleBase handle;
if (IsSceneProvider())
handle = new SceneOperationHandle(this);
2022-03-22 20:15:59 +08:00
else if (IsSubAssetsProvider())
handle = new SubAssetsOperationHandle(this);
2022-03-21 23:43:11 +08:00
else
handle = new AssetOperationHandle(this);
_handles.Add(handle);
return handle;
2022-03-01 10:44:12 +08:00
}
2022-03-21 23:43:11 +08:00
/// <summary>
/// 释放操作句柄
/// </summary>
public void ReleaseHandle(OperationHandleBase handle)
2022-03-01 10:44:12 +08:00
{
if (RefCount <= 0)
2022-03-09 21:53:01 +08:00
YooLogger.Warning("Asset provider reference count is already zero. There may be resource leaks !");
2022-03-01 10:44:12 +08:00
2022-03-21 23:43:11 +08:00
if (_handles.Remove(handle) == false)
throw new System.Exception("Should never get here !");
// 引用计数减少
2022-03-01 10:44:12 +08:00
RefCount--;
}
2022-03-21 23:43:11 +08:00
/// <summary>
2022-03-22 20:15:59 +08:00
/// 是否为场景提供者
2022-03-21 23:43:11 +08:00
/// </summary>
2022-03-22 20:15:59 +08:00
public bool IsSceneProvider()
2022-03-01 10:44:12 +08:00
{
2022-03-22 20:15:59 +08:00
if (this is BundledSceneProvider || this is DatabaseSceneProvider)
return true;
else
2022-03-01 10:44:12 +08:00
return false;
}
/// <summary>
2022-03-22 20:15:59 +08:00
/// 是否为子资源对象提供者
2022-03-01 10:44:12 +08:00
/// </summary>
2022-03-22 20:15:59 +08:00
public bool IsSubAssetsProvider()
2022-03-01 10:44:12 +08:00
{
2022-03-22 20:15:59 +08:00
if (this is BundledSubAssetsProvider || this is DatabaseSubAssetsProvider)
2022-03-01 10:44:12 +08:00
return true;
else
return false;
}
/// <summary>
/// 等待异步执行完毕
/// </summary>
2022-03-21 23:43:11 +08:00
public void WaitForAsyncComplete()
2022-03-01 10:44:12 +08:00
{
IsWaitForAsyncComplete = true;
// 注意:主动轮询更新完成同步加载
Update();
// 验证结果
if (IsDone == false)
{
2022-03-09 21:53:01 +08:00
YooLogger.Warning($"WaitForAsyncComplete failed to loading : {AssetPath}");
2022-03-01 10:44:12 +08:00
}
}
/// <summary>
/// 异步操作任务
/// </summary>
2022-03-21 23:43:11 +08:00
public System.Threading.Tasks.Task<object> Task
2022-03-01 10:44:12 +08:00
{
get
{
var handle = WaitHandle;
return System.Threading.Tasks.Task.Factory.StartNew(o =>
{
handle.WaitOne();
return AssetObject as object;
}, this);
}
}
// 异步操作相关
private System.Threading.EventWaitHandle _waitHandle;
private System.Threading.WaitHandle WaitHandle
{
get
{
if (_waitHandle == null)
_waitHandle = new System.Threading.EventWaitHandle(false, System.Threading.EventResetMode.ManualReset);
_waitHandle.Reset();
return _waitHandle;
}
}
protected void InvokeCompletion()
{
2022-03-21 23:43:11 +08:00
foreach (var handle in _handles)
{
handle.InvokeCallback();
}
2022-03-01 10:44:12 +08:00
_waitHandle?.Set();
}
}
}