Update AssetSystem
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal abstract class AssetProviderBase : IAssetProvider
|
||||
internal abstract class AssetProviderBase
|
||||
{
|
||||
public enum EStatus
|
||||
{
|
||||
@@ -13,19 +15,55 @@ namespace YooAsset
|
||||
Fail,
|
||||
}
|
||||
|
||||
protected bool IsWaitForAsyncComplete { private set; get; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// 资源路径
|
||||
/// </summary>
|
||||
public string AssetPath { private set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 资源对象的名称
|
||||
/// </summary>
|
||||
public string AssetName { private set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 资源对象的类型
|
||||
/// </summary>
|
||||
public System.Type AssetType { private set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取的资源对象
|
||||
/// </summary>
|
||||
public UnityEngine.Object AssetObject { protected set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取的资源对象集合
|
||||
/// </summary>
|
||||
public UnityEngine.Object[] AllAssets { protected set; get; }
|
||||
public IAssetInstance AssetInstance { protected set; get; }
|
||||
public EStatus Status { protected set; get; }
|
||||
public int RefCount { private set; get; }
|
||||
public AssetOperationHandle Handle { private set; get; }
|
||||
public System.Action<AssetOperationHandle> Callback { set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取的场景对象
|
||||
/// </summary>
|
||||
public UnityEngine.SceneManagement.Scene Scene { protected set; get; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 当前的加载状态
|
||||
/// </summary>
|
||||
public EStatus Status { protected set; get; } = EStatus.None;
|
||||
|
||||
/// <summary>
|
||||
/// 引用计数
|
||||
/// </summary>
|
||||
public int RefCount { private set; get; } = 0;
|
||||
|
||||
/// <summary>
|
||||
/// 是否已经销毁
|
||||
/// </summary>
|
||||
public bool IsDestroyed { private set; get; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// 是否完毕(成功或失败)
|
||||
/// </summary>
|
||||
public bool IsDone
|
||||
{
|
||||
get
|
||||
@@ -33,13 +71,10 @@ namespace YooAsset
|
||||
return Status == EStatus.Success || Status == EStatus.Fail;
|
||||
}
|
||||
}
|
||||
public bool IsValid
|
||||
{
|
||||
get
|
||||
{
|
||||
return IsDestroyed == false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载进度
|
||||
/// </summary>
|
||||
public virtual float Progress
|
||||
{
|
||||
get
|
||||
@@ -47,34 +82,69 @@ namespace YooAsset
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected bool IsWaitForAsyncComplete { private set; get; } = false;
|
||||
private readonly List<OperationHandleBase> _handles = new List<OperationHandleBase>();
|
||||
|
||||
|
||||
public AssetProviderBase(string assetPath, System.Type assetType)
|
||||
{
|
||||
AssetPath = assetPath;
|
||||
AssetName = System.IO.Path.GetFileName(assetPath);
|
||||
AssetType = assetType;
|
||||
Status = EStatus.None;
|
||||
Handle = new AssetOperationHandle(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 轮询更新方法
|
||||
/// </summary>
|
||||
public abstract void Update();
|
||||
|
||||
/// <summary>
|
||||
/// 销毁资源对象
|
||||
/// </summary>
|
||||
public virtual void Destory()
|
||||
{
|
||||
IsDestroyed = true;
|
||||
}
|
||||
|
||||
public void Reference()
|
||||
/// <summary>
|
||||
/// 创建操作句柄
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public OperationHandleBase CreateHandle()
|
||||
{
|
||||
// 引用计数增加
|
||||
RefCount++;
|
||||
|
||||
OperationHandleBase handle;
|
||||
if (IsSceneProvider())
|
||||
handle = new SceneOperationHandle(this);
|
||||
else
|
||||
handle = new AssetOperationHandle(this);
|
||||
|
||||
_handles.Add(handle);
|
||||
return handle;
|
||||
}
|
||||
public void Release()
|
||||
|
||||
/// <summary>
|
||||
/// 释放操作句柄
|
||||
/// </summary>
|
||||
public void ReleaseHandle(OperationHandleBase handle)
|
||||
{
|
||||
if (RefCount <= 0)
|
||||
YooLogger.Warning("Asset provider reference count is already zero. There may be resource leaks !");
|
||||
|
||||
if (_handles.Remove(handle) == false)
|
||||
throw new System.Exception("Should never get here !");
|
||||
|
||||
// 引用计数减少
|
||||
RefCount--;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否可以销毁
|
||||
/// </summary>
|
||||
public bool CanDestroy()
|
||||
{
|
||||
if (IsDone == false)
|
||||
@@ -97,7 +167,7 @@ namespace YooAsset
|
||||
/// <summary>
|
||||
/// 等待异步执行完毕
|
||||
/// </summary>
|
||||
public virtual void WaitForAsyncComplete()
|
||||
public void WaitForAsyncComplete()
|
||||
{
|
||||
IsWaitForAsyncComplete = true;
|
||||
|
||||
@@ -114,7 +184,7 @@ namespace YooAsset
|
||||
/// <summary>
|
||||
/// 异步操作任务
|
||||
/// </summary>
|
||||
System.Threading.Tasks.Task<object> IAssetProvider.Task
|
||||
public System.Threading.Tasks.Task<object> Task
|
||||
{
|
||||
get
|
||||
{
|
||||
@@ -141,7 +211,10 @@ namespace YooAsset
|
||||
}
|
||||
protected void InvokeCompletion()
|
||||
{
|
||||
Callback?.Invoke(Handle);
|
||||
foreach (var handle in _handles)
|
||||
{
|
||||
handle.InvokeCallback();
|
||||
}
|
||||
_waitHandle?.Set();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,8 @@ namespace YooAsset
|
||||
{
|
||||
internal sealed class BundledSceneProvider : BundledProvider
|
||||
{
|
||||
private SceneInstanceParam _param;
|
||||
private readonly LoadSceneMode _sceneMode;
|
||||
private readonly bool _activateOnLoad;
|
||||
private AsyncOperation _asyncOp;
|
||||
public override float Progress
|
||||
{
|
||||
@@ -19,10 +20,11 @@ namespace YooAsset
|
||||
}
|
||||
}
|
||||
|
||||
public BundledSceneProvider(string scenePath, SceneInstanceParam param)
|
||||
public BundledSceneProvider(string scenePath, LoadSceneMode sceneMode, bool activateOnLoad)
|
||||
: base(scenePath, null)
|
||||
{
|
||||
_param = param;
|
||||
_sceneMode = sceneMode;
|
||||
_activateOnLoad = activateOnLoad;
|
||||
}
|
||||
public override void Update()
|
||||
{
|
||||
@@ -56,7 +58,7 @@ namespace YooAsset
|
||||
// 2. 加载场景
|
||||
if (Status == EStatus.Loading)
|
||||
{
|
||||
_asyncOp = SceneManager.LoadSceneAsync(AssetName, _param.LoadMode);
|
||||
_asyncOp = SceneManager.LoadSceneAsync(AssetName, _sceneMode);
|
||||
if (_asyncOp != null)
|
||||
{
|
||||
_asyncOp.allowSceneActivation = true;
|
||||
@@ -75,13 +77,11 @@ namespace YooAsset
|
||||
{
|
||||
if (_asyncOp.isDone)
|
||||
{
|
||||
SceneInstance instance = new SceneInstance(_asyncOp);
|
||||
instance.Scene = SceneManager.GetSceneByName(AssetName);
|
||||
AssetInstance = instance;
|
||||
if (_param.ActivateOnLoad)
|
||||
instance.Activate();
|
||||
Scene = SceneManager.GetSceneByName(AssetName);
|
||||
if (_activateOnLoad)
|
||||
SceneManager.SetActiveScene(Scene);
|
||||
|
||||
Status = instance.Scene.IsValid() ? EStatus.Success : EStatus.Fail;
|
||||
Status = Scene.IsValid() ? EStatus.Success : EStatus.Fail;
|
||||
InvokeCompletion();
|
||||
}
|
||||
}
|
||||
@@ -91,19 +91,11 @@ namespace YooAsset
|
||||
base.Destory();
|
||||
|
||||
// 卸载附加场景(异步方式卸载)
|
||||
if (_param.LoadMode == LoadSceneMode.Additive)
|
||||
if (_sceneMode == LoadSceneMode.Additive)
|
||||
{
|
||||
var instance = AssetInstance as SceneInstance;
|
||||
if (instance != null && instance.Scene != null)
|
||||
{
|
||||
if (instance.Scene.IsValid() && instance.Scene.isLoaded)
|
||||
SceneManager.UnloadSceneAsync(instance.Scene);
|
||||
}
|
||||
if (Scene.IsValid() && Scene.isLoaded)
|
||||
SceneManager.UnloadSceneAsync(Scene);
|
||||
}
|
||||
}
|
||||
public override void WaitForAsyncComplete()
|
||||
{
|
||||
throw new System.Exception($"Unity scene is not support {nameof(WaitForAsyncComplete)}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,8 @@ namespace YooAsset
|
||||
{
|
||||
internal sealed class DatabaseSceneProvider : AssetProviderBase
|
||||
{
|
||||
private SceneInstanceParam _param;
|
||||
private readonly LoadSceneMode _sceneMode;
|
||||
private readonly bool _activateOnLoad;
|
||||
private AsyncOperation _asyncOp;
|
||||
public override float Progress
|
||||
{
|
||||
@@ -17,10 +18,11 @@ namespace YooAsset
|
||||
}
|
||||
}
|
||||
|
||||
public DatabaseSceneProvider(string scenePath, SceneInstanceParam param)
|
||||
public DatabaseSceneProvider(string scenePath, LoadSceneMode sceneMode, bool activateOnLoad)
|
||||
: base(scenePath, null)
|
||||
{
|
||||
_param = param;
|
||||
_sceneMode = sceneMode;
|
||||
_activateOnLoad = activateOnLoad;
|
||||
}
|
||||
public override void Update()
|
||||
{
|
||||
@@ -37,7 +39,7 @@ namespace YooAsset
|
||||
if (Status == EStatus.Loading)
|
||||
{
|
||||
LoadSceneParameters loadSceneParameters = new LoadSceneParameters();
|
||||
loadSceneParameters.loadSceneMode = _param.LoadMode;
|
||||
loadSceneParameters.loadSceneMode = _sceneMode;
|
||||
_asyncOp = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(AssetPath, loadSceneParameters);
|
||||
if (_asyncOp != null)
|
||||
{
|
||||
@@ -57,13 +59,11 @@ namespace YooAsset
|
||||
{
|
||||
if (_asyncOp.isDone)
|
||||
{
|
||||
SceneInstance instance = new SceneInstance(_asyncOp);
|
||||
instance.Scene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
|
||||
AssetInstance = instance;
|
||||
if(_param.ActivateOnLoad)
|
||||
instance.Activate();
|
||||
Scene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
|
||||
if (_activateOnLoad)
|
||||
SceneManager.SetActiveScene(Scene);
|
||||
|
||||
Status = instance.Scene.IsValid() ? EStatus.Success : EStatus.Fail;
|
||||
Status = Scene.IsValid() ? EStatus.Success : EStatus.Fail;
|
||||
InvokeCompletion();
|
||||
}
|
||||
}
|
||||
@@ -75,20 +75,12 @@ namespace YooAsset
|
||||
base.Destory();
|
||||
|
||||
// 卸载附加场景(异步方式卸载)
|
||||
if (_param.LoadMode == LoadSceneMode.Additive)
|
||||
if (_sceneMode == LoadSceneMode.Additive)
|
||||
{
|
||||
var instance = AssetInstance as SceneInstance;
|
||||
if(instance != null && instance.Scene != null)
|
||||
{
|
||||
if (instance.Scene.IsValid() && instance.Scene.isLoaded)
|
||||
SceneManager.UnloadSceneAsync(instance.Scene);
|
||||
}
|
||||
if (Scene.IsValid() && Scene.isLoaded)
|
||||
SceneManager.UnloadSceneAsync(Scene);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
public override void WaitForAsyncComplete()
|
||||
{
|
||||
throw new System.Exception($"Unity scene is not support {nameof(WaitForAsyncComplete)}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,7 +40,7 @@ namespace YooAsset
|
||||
else
|
||||
{
|
||||
Status = EStatus.Loading;
|
||||
}
|
||||
}
|
||||
|
||||
// 注意:模拟异步加载效果提前返回
|
||||
if (IsWaitForAsyncComplete == false)
|
||||
@@ -50,14 +50,21 @@ namespace YooAsset
|
||||
// 1. 加载资源对象
|
||||
if (Status == EStatus.Loading)
|
||||
{
|
||||
var findAssets = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(AssetPath);
|
||||
List<UnityEngine.Object> result = new List<Object>(findAssets.Length);
|
||||
foreach (var findObj in findAssets)
|
||||
if (AssetType == null)
|
||||
{
|
||||
if (findObj.GetType() == AssetType)
|
||||
result.Add(findObj);
|
||||
AllAssets = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
UnityEngine.Object[] findAssets = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetPath);
|
||||
List<UnityEngine.Object> result = new List<Object>(findAssets.Length);
|
||||
foreach (var findAsset in findAssets)
|
||||
{
|
||||
if (findAsset.GetType() == AssetType)
|
||||
result.Add(findAsset);
|
||||
}
|
||||
AllAssets = result.ToArray();
|
||||
}
|
||||
AllAssets = result.ToArray();
|
||||
Status = EStatus.Checking;
|
||||
}
|
||||
|
||||
@@ -66,7 +73,7 @@ namespace YooAsset
|
||||
{
|
||||
Status = AllAssets == null ? EStatus.Fail : EStatus.Success;
|
||||
if (Status == EStatus.Fail)
|
||||
YooLogger.Warning($"Failed to load all asset object : {AssetPath}");
|
||||
YooLogger.Warning($"Failed to load sub assets : {AssetName}");
|
||||
InvokeCompletion();
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,114 +0,0 @@
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 资源提供者
|
||||
/// </summary>
|
||||
internal interface IAssetProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// 资源路径
|
||||
/// </summary>
|
||||
string AssetPath { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 资源对象的名称
|
||||
/// </summary>
|
||||
string AssetName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 资源对象的类型
|
||||
/// </summary>
|
||||
System.Type AssetType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取的资源对象
|
||||
/// </summary>
|
||||
UnityEngine.Object AssetObject { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取的资源对象集合
|
||||
/// </summary>
|
||||
UnityEngine.Object[] AllAssets { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 扩展的实例对象
|
||||
/// </summary>
|
||||
IAssetInstance AssetInstance { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前的加载状态
|
||||
/// </summary>
|
||||
AssetProviderBase.EStatus Status { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 引用计数
|
||||
/// </summary>
|
||||
int RefCount { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 资源操作句柄
|
||||
/// </summary>
|
||||
AssetOperationHandle Handle { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 用户请求的回调
|
||||
/// </summary>
|
||||
System.Action<AssetOperationHandle> Callback { set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否已经销毁
|
||||
/// </summary>
|
||||
bool IsDestroyed { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否完毕(成功或失败)
|
||||
/// </summary>
|
||||
bool IsDone { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否有效(AssetFileLoader销毁会导致Provider无效)
|
||||
/// </summary>
|
||||
bool IsValid { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 加载进度
|
||||
/// </summary>
|
||||
float Progress { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 轮询更新方法
|
||||
/// </summary>
|
||||
void Update();
|
||||
|
||||
/// <summary>
|
||||
/// 销毁资源对象
|
||||
/// </summary>
|
||||
void Destory();
|
||||
|
||||
/// <summary>
|
||||
/// 引用计数递加
|
||||
/// </summary>
|
||||
void Reference();
|
||||
|
||||
/// <summary>
|
||||
/// 引用计数递减
|
||||
/// </summary>
|
||||
void Release();
|
||||
|
||||
/// <summary>
|
||||
/// 是否可以销毁
|
||||
/// </summary>
|
||||
bool CanDestroy();
|
||||
|
||||
/// <summary>
|
||||
/// 等待异步执行完毕
|
||||
/// </summary>
|
||||
void WaitForAsyncComplete();
|
||||
|
||||
/// <summary>
|
||||
/// 异步操作任务
|
||||
/// </summary>
|
||||
System.Threading.Tasks.Task<object> Task { get; }
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 54f7b01043e68994189cc954e55813aa
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user