Update AssetSystem

pull/4/head
hevinci 2022-03-21 23:43:11 +08:00
parent fa0009685d
commit 2bf36ebd8b
18 changed files with 383 additions and 446 deletions

View File

@ -2,6 +2,7 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.SceneManagement;
namespace YooAsset namespace YooAsset
{ {
@ -163,21 +164,18 @@ namespace YooAsset
/// 异步加载场景 /// 异步加载场景
/// </summary> /// </summary>
/// <param name="scenePath">场景名称</param> /// <param name="scenePath">场景名称</param>
public static AssetOperationHandle LoadSceneAsync(string scenePath, SceneInstanceParam instanceParam) public static SceneOperationHandle LoadSceneAsync(string scenePath, LoadSceneMode mode, bool activateOnLoad)
{ {
AssetProviderBase provider = TryGetAssetProvider(scenePath); AssetProviderBase provider = TryGetAssetProvider(scenePath);
if (provider == null) if (provider == null)
{ {
if (SimulationOnEditor) if (SimulationOnEditor)
provider = new DatabaseSceneProvider(scenePath, instanceParam); provider = new DatabaseSceneProvider(scenePath, mode, activateOnLoad);
else else
provider = new BundledSceneProvider(scenePath, instanceParam); provider = new BundledSceneProvider(scenePath, mode, activateOnLoad);
_providers.Add(provider); _providers.Add(provider);
} }
return provider.CreateHandle() as SceneOperationHandle;
// 引用计数增加
provider.Reference();
return provider.Handle;
} }
/// <summary> /// <summary>
@ -196,10 +194,7 @@ namespace YooAsset
provider = new BundledAssetProvider(assetPath, assetType); provider = new BundledAssetProvider(assetPath, assetType);
_providers.Add(provider); _providers.Add(provider);
} }
return provider.CreateHandle() as AssetOperationHandle;
// 引用计数增加
provider.Reference();
return provider.Handle;
} }
/// <summary> /// <summary>
@ -218,10 +213,7 @@ namespace YooAsset
provider = new BundledSubAssetsProvider(assetPath, assetType); provider = new BundledSubAssetsProvider(assetPath, assetType);
_providers.Add(provider); _providers.Add(provider);
} }
return provider.CreateHandle() as AssetOperationHandle;
// 引用计数增加
provider.Reference();
return provider.Handle;
} }

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: a9b18d6c47ddf9b4296619d3d50ff826
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,7 +0,0 @@

namespace YooAsset
{
public interface IAssetInstance
{
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: e1d0cfc6a2d5b1f4d9b9ac8f93682497
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,7 +0,0 @@

namespace YooAsset
{
public interface IAssetParam
{
}
}

View File

@ -1,63 +0,0 @@
using UnityEngine;
using UnityEngine.SceneManagement;
namespace YooAsset
{
/// <summary>
/// 扩展的场景实例对象
/// </summary>
public class SceneInstance : IAssetInstance
{
private readonly AsyncOperation _asyncOp;
public SceneInstance(AsyncOperation op)
{
_asyncOp = op;
}
/// <summary>
/// UnityEngine场景对象
/// </summary>
public UnityEngine.SceneManagement.Scene Scene { internal set; get; }
/// <summary>
/// 激活场景
/// </summary>
public bool Activate()
{
if (Scene == null)
return false;
if (Scene.IsValid() && Scene.isLoaded)
{
return SceneManager.SetActiveScene(Scene);
}
else
{
YooLogger.Warning($"Scene is invalid or not loaded : {Scene.name}");
return false;
}
}
}
/// <summary>
/// 加载场景实例对象需要提供的参数类
/// </summary>
public class SceneInstanceParam : IAssetParam
{
/// <summary>
/// 加载模式
/// </summary>
public LoadSceneMode LoadMode { set; get; }
/// <summary>
/// 物理模式
/// </summary>
//public LocalPhysicsMode PhysicsMode { set; get;}
/// <summary>
/// 加载完毕时是否主动激活
/// </summary>
public bool ActivateOnLoad { set; get; }
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 0ab6e7cef318cff4684ed733679bca0e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,69 +1,19 @@
using System.Collections; using UnityEngine;
using UnityEngine;
namespace YooAsset namespace YooAsset
{ {
public struct AssetOperationHandle : IEnumerator public class AssetOperationHandle : OperationHandleBase
{ {
private IAssetProvider _provider; private System.Action<AssetOperationHandle> _callback;
internal AssetOperationHandle(IAssetProvider provider) internal AssetOperationHandle(AssetProviderBase provider) : base(provider)
{ {
_provider = provider;
} }
internal override void InvokeCallback()
/// <summary>
/// 句柄是否有效AssetFileLoader销毁会导致所有句柄失效
/// </summary>
public bool IsValid
{ {
get if (IsValid)
{ {
return _provider != null && _provider.IsValid; _callback?.Invoke(this);
}
}
/// <summary>
/// 当前状态
/// </summary>
public EOperationStatus Status
{
get
{
if (IsValid == false)
return EOperationStatus.None;
if (_provider.Status == AssetProviderBase.EStatus.Fail)
return EOperationStatus.Failed;
else if (_provider.Status == AssetProviderBase.EStatus.Success)
return EOperationStatus.Succeed;
else
return EOperationStatus.None;
}
}
/// <summary>
/// 加载进度
/// </summary>
public float Progress
{
get
{
if (IsValid == false)
return 0;
return _provider.Progress;
}
}
/// <summary>
/// 是否加载完毕
/// </summary>
public bool IsDone
{
get
{
if (IsValid == false)
return false;
return _provider.IsDone;
} }
} }
@ -79,13 +29,13 @@ namespace YooAsset
if (_provider.IsDone) if (_provider.IsDone)
value.Invoke(this); value.Invoke(this);
else else
_provider.Callback += value; _callback += value;
} }
remove remove
{ {
if (IsValid == false) if (IsValid == false)
throw new System.Exception($"{nameof(AssetOperationHandle)} is invalid"); throw new System.Exception($"{nameof(AssetOperationHandle)} is invalid");
_provider.Callback -= value; _callback -= value;
} }
} }
@ -102,32 +52,6 @@ namespace YooAsset
} }
} }
/// <summary>
/// 资源对象集合
/// </summary>
public UnityEngine.Object[] AllAssets
{
get
{
if (IsValid == false)
return null;
return _provider.AllAssets;
}
}
/// <summary>
/// 扩展的实例对象
/// </summary>
public IAssetInstance AssetInstance
{
get
{
if (IsValid == false)
return null;
return _provider.AssetInstance;
}
}
/// <summary> /// <summary>
/// 初始化的游戏对象只限于请求的资源对象类型为GameObject /// 初始化的游戏对象只限于请求的资源对象类型为GameObject
/// </summary> /// </summary>
@ -143,17 +67,6 @@ namespace YooAsset
} }
} }
/// <summary>
/// 释放资源句柄
/// </summary>
public void Release()
{
if (IsValid == false)
return;
_provider.Release();
_provider = null;
}
/// <summary> /// <summary>
/// 等待异步执行完毕 /// 等待异步执行完毕
/// </summary> /// </summary>
@ -163,28 +76,5 @@ namespace YooAsset
return; return;
_provider.WaitForAsyncComplete(); _provider.WaitForAsyncComplete();
} }
#region 异步操作相关
/// <summary>
/// 异步操作任务
/// </summary>
public System.Threading.Tasks.Task<object> Task
{
get { return _provider.Task; }
}
// 协程相关
bool IEnumerator.MoveNext()
{
return !IsDone;
}
void IEnumerator.Reset()
{
}
object IEnumerator.Current
{
get { return AssetObject; }
}
#endregion
} }
} }

View File

@ -0,0 +1,104 @@
using System.Collections;
namespace YooAsset
{
public abstract class OperationHandleBase : IEnumerator
{
internal AssetProviderBase _provider { private set; get; }
internal OperationHandleBase(AssetProviderBase provider)
{
_provider = provider;
}
internal abstract void InvokeCallback();
/// <summary>
/// 当前状态
/// </summary>
public EOperationStatus Status
{
get
{
if (IsValid == false)
return EOperationStatus.None;
if (_provider.Status == AssetProviderBase.EStatus.Fail)
return EOperationStatus.Failed;
else if (_provider.Status == AssetProviderBase.EStatus.Success)
return EOperationStatus.Succeed;
else
return EOperationStatus.None;
}
}
/// <summary>
/// 加载进度
/// </summary>
public float Progress
{
get
{
if (IsValid == false)
return 0;
return _provider.Progress;
}
}
/// <summary>
/// 是否加载完毕
/// </summary>
public bool IsDone
{
get
{
if (IsValid == false)
return false;
return _provider.IsDone;
}
}
/// <summary>
/// 句柄是否有效
/// </summary>
public bool IsValid
{
get
{
return _provider != null && _provider.IsDestroyed == false;
}
}
/// <summary>
/// 释放句柄
/// </summary>
public void Release()
{
if (IsValid == false)
return;
_provider.ReleaseHandle(this);
_provider = null;
}
#region 异步操作相关
/// <summary>
/// 异步操作任务
/// </summary>
public System.Threading.Tasks.Task<object> Task
{
get { return _provider.Task; }
}
// 协程相关
bool IEnumerator.MoveNext()
{
return !IsDone;
}
void IEnumerator.Reset()
{
}
object IEnumerator.Current
{
get { return _provider; }
}
#endregion
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 54f7b01043e68994189cc954e55813aa guid: a9f5acda1a4584648af7b47e19a0d24f
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -0,0 +1,73 @@

namespace YooAsset
{
public class SceneOperationHandle : OperationHandleBase
{
private System.Action<SceneOperationHandle> _callback;
internal SceneOperationHandle(AssetProviderBase provider) : base(provider)
{
}
internal override void InvokeCallback()
{
if (IsValid)
{
_callback?.Invoke(this);
}
}
/// <summary>
/// 完成委托
/// </summary>
public event System.Action<SceneOperationHandle> Completed
{
add
{
if (IsValid == false)
throw new System.Exception($"{nameof(SceneOperationHandle)} is invalid");
if (_provider.IsDone)
value.Invoke(this);
else
_callback += value;
}
remove
{
if (IsValid == false)
throw new System.Exception($"{nameof(SceneOperationHandle)} is invalid");
_callback -= value;
}
}
/// <summary>
/// 场景对象
/// </summary>
public UnityEngine.SceneManagement.Scene Scene
{
get
{
if (IsValid == false)
return new UnityEngine.SceneManagement.Scene();
return _provider.Scene;
}
}
/// <summary>
/// 激活场景
/// </summary>
public bool ActivateScene()
{
if (IsValid == false)
return false;
if (Scene.IsValid() && Scene.isLoaded)
{
return UnityEngine.SceneManagement.SceneManager.SetActiveScene(Scene);
}
else
{
YooLogger.Warning($"Scene is invalid or not loaded : {Scene.name}");
return false;
}
}
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: a8995b1e1e511714b948f7fa6275028e guid: c5d788ba2dd3c5243ae2e0be8e780825
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -1,7 +1,9 @@
 using System.Collections;
using System.Collections.Generic;
namespace YooAsset namespace YooAsset
{ {
internal abstract class AssetProviderBase : IAssetProvider internal abstract class AssetProviderBase
{ {
public enum EStatus public enum EStatus
{ {
@ -13,19 +15,55 @@ namespace YooAsset
Fail, Fail,
} }
protected bool IsWaitForAsyncComplete { private set; get; } = false; /// <summary>
/// 资源路径
/// </summary>
public string AssetPath { private set; get; } public string AssetPath { private set; get; }
/// <summary>
/// 资源对象的名称
/// </summary>
public string AssetName { private set; get; } public string AssetName { private set; get; }
/// <summary>
/// 资源对象的类型
/// </summary>
public System.Type AssetType { private set; get; } public System.Type AssetType { private set; get; }
/// <summary>
/// 获取的资源对象
/// </summary>
public UnityEngine.Object AssetObject { protected set; get; } public UnityEngine.Object AssetObject { protected set; get; }
/// <summary>
/// 获取的资源对象集合
/// </summary>
public UnityEngine.Object[] AllAssets { protected set; get; } public UnityEngine.Object[] AllAssets { protected set; get; }
public IAssetInstance AssetInstance { protected set; get; }
public EStatus Status { protected set; get; } /// <summary>
public int RefCount { private set; get; } /// 获取的场景对象
public AssetOperationHandle Handle { private set; get; } /// </summary>
public System.Action<AssetOperationHandle> Callback { set; get; } 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; public bool IsDestroyed { private set; get; } = false;
/// <summary>
/// 是否完毕(成功或失败)
/// </summary>
public bool IsDone public bool IsDone
{ {
get get
@ -33,13 +71,10 @@ namespace YooAsset
return Status == EStatus.Success || Status == EStatus.Fail; return Status == EStatus.Success || Status == EStatus.Fail;
} }
} }
public bool IsValid
{ /// <summary>
get /// 加载进度
{ /// </summary>
return IsDestroyed == false;
}
}
public virtual float Progress public virtual float Progress
{ {
get get
@ -47,34 +82,69 @@ namespace YooAsset
return 0; return 0;
} }
} }
protected bool IsWaitForAsyncComplete { private set; get; } = false;
private readonly List<OperationHandleBase> _handles = new List<OperationHandleBase>();
public AssetProviderBase(string assetPath, System.Type assetType) public AssetProviderBase(string assetPath, System.Type assetType)
{ {
AssetPath = assetPath; AssetPath = assetPath;
AssetName = System.IO.Path.GetFileName(assetPath); AssetName = System.IO.Path.GetFileName(assetPath);
AssetType = assetType; AssetType = assetType;
Status = EStatus.None;
Handle = new AssetOperationHandle(this);
} }
/// <summary>
/// 轮询更新方法
/// </summary>
public abstract void Update(); public abstract void Update();
/// <summary>
/// 销毁资源对象
/// </summary>
public virtual void Destory() public virtual void Destory()
{ {
IsDestroyed = true; IsDestroyed = true;
} }
public void Reference() /// <summary>
/// 创建操作句柄
/// </summary>
/// <returns></returns>
public OperationHandleBase CreateHandle()
{ {
// 引用计数增加
RefCount++; 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) if (RefCount <= 0)
YooLogger.Warning("Asset provider reference count is already zero. There may be resource leaks !"); 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--; RefCount--;
} }
/// <summary>
/// 是否可以销毁
/// </summary>
public bool CanDestroy() public bool CanDestroy()
{ {
if (IsDone == false) if (IsDone == false)
@ -97,7 +167,7 @@ namespace YooAsset
/// <summary> /// <summary>
/// 等待异步执行完毕 /// 等待异步执行完毕
/// </summary> /// </summary>
public virtual void WaitForAsyncComplete() public void WaitForAsyncComplete()
{ {
IsWaitForAsyncComplete = true; IsWaitForAsyncComplete = true;
@ -114,7 +184,7 @@ namespace YooAsset
/// <summary> /// <summary>
/// 异步操作任务 /// 异步操作任务
/// </summary> /// </summary>
System.Threading.Tasks.Task<object> IAssetProvider.Task public System.Threading.Tasks.Task<object> Task
{ {
get get
{ {
@ -141,7 +211,10 @@ namespace YooAsset
} }
protected void InvokeCompletion() protected void InvokeCompletion()
{ {
Callback?.Invoke(Handle); foreach (var handle in _handles)
{
handle.InvokeCallback();
}
_waitHandle?.Set(); _waitHandle?.Set();
} }
} }

View File

@ -7,7 +7,8 @@ namespace YooAsset
{ {
internal sealed class BundledSceneProvider : BundledProvider internal sealed class BundledSceneProvider : BundledProvider
{ {
private SceneInstanceParam _param; private readonly LoadSceneMode _sceneMode;
private readonly bool _activateOnLoad;
private AsyncOperation _asyncOp; private AsyncOperation _asyncOp;
public override float Progress 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) : base(scenePath, null)
{ {
_param = param; _sceneMode = sceneMode;
_activateOnLoad = activateOnLoad;
} }
public override void Update() public override void Update()
{ {
@ -56,7 +58,7 @@ namespace YooAsset
// 2. 加载场景 // 2. 加载场景
if (Status == EStatus.Loading) if (Status == EStatus.Loading)
{ {
_asyncOp = SceneManager.LoadSceneAsync(AssetName, _param.LoadMode); _asyncOp = SceneManager.LoadSceneAsync(AssetName, _sceneMode);
if (_asyncOp != null) if (_asyncOp != null)
{ {
_asyncOp.allowSceneActivation = true; _asyncOp.allowSceneActivation = true;
@ -75,13 +77,11 @@ namespace YooAsset
{ {
if (_asyncOp.isDone) if (_asyncOp.isDone)
{ {
SceneInstance instance = new SceneInstance(_asyncOp); Scene = SceneManager.GetSceneByName(AssetName);
instance.Scene = SceneManager.GetSceneByName(AssetName); if (_activateOnLoad)
AssetInstance = instance; SceneManager.SetActiveScene(Scene);
if (_param.ActivateOnLoad)
instance.Activate();
Status = instance.Scene.IsValid() ? EStatus.Success : EStatus.Fail; Status = Scene.IsValid() ? EStatus.Success : EStatus.Fail;
InvokeCompletion(); InvokeCompletion();
} }
} }
@ -91,19 +91,11 @@ namespace YooAsset
base.Destory(); base.Destory();
// 卸载附加场景(异步方式卸载) // 卸载附加场景(异步方式卸载)
if (_param.LoadMode == LoadSceneMode.Additive) if (_sceneMode == LoadSceneMode.Additive)
{ {
var instance = AssetInstance as SceneInstance; if (Scene.IsValid() && Scene.isLoaded)
if (instance != null && instance.Scene != null) SceneManager.UnloadSceneAsync(Scene);
{
if (instance.Scene.IsValid() && instance.Scene.isLoaded)
SceneManager.UnloadSceneAsync(instance.Scene);
}
} }
} }
public override void WaitForAsyncComplete()
{
throw new System.Exception($"Unity scene is not support {nameof(WaitForAsyncComplete)}.");
}
} }
} }

View File

@ -5,7 +5,8 @@ namespace YooAsset
{ {
internal sealed class DatabaseSceneProvider : AssetProviderBase internal sealed class DatabaseSceneProvider : AssetProviderBase
{ {
private SceneInstanceParam _param; private readonly LoadSceneMode _sceneMode;
private readonly bool _activateOnLoad;
private AsyncOperation _asyncOp; private AsyncOperation _asyncOp;
public override float Progress 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) : base(scenePath, null)
{ {
_param = param; _sceneMode = sceneMode;
_activateOnLoad = activateOnLoad;
} }
public override void Update() public override void Update()
{ {
@ -37,7 +39,7 @@ namespace YooAsset
if (Status == EStatus.Loading) if (Status == EStatus.Loading)
{ {
LoadSceneParameters loadSceneParameters = new LoadSceneParameters(); LoadSceneParameters loadSceneParameters = new LoadSceneParameters();
loadSceneParameters.loadSceneMode = _param.LoadMode; loadSceneParameters.loadSceneMode = _sceneMode;
_asyncOp = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(AssetPath, loadSceneParameters); _asyncOp = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(AssetPath, loadSceneParameters);
if (_asyncOp != null) if (_asyncOp != null)
{ {
@ -57,13 +59,11 @@ namespace YooAsset
{ {
if (_asyncOp.isDone) if (_asyncOp.isDone)
{ {
SceneInstance instance = new SceneInstance(_asyncOp); Scene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
instance.Scene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1); if (_activateOnLoad)
AssetInstance = instance; SceneManager.SetActiveScene(Scene);
if(_param.ActivateOnLoad)
instance.Activate();
Status = instance.Scene.IsValid() ? EStatus.Success : EStatus.Fail; Status = Scene.IsValid() ? EStatus.Success : EStatus.Fail;
InvokeCompletion(); InvokeCompletion();
} }
} }
@ -75,20 +75,12 @@ namespace YooAsset
base.Destory(); base.Destory();
// 卸载附加场景(异步方式卸载) // 卸载附加场景(异步方式卸载)
if (_param.LoadMode == LoadSceneMode.Additive) if (_sceneMode == LoadSceneMode.Additive)
{ {
var instance = AssetInstance as SceneInstance; if (Scene.IsValid() && Scene.isLoaded)
if(instance != null && instance.Scene != null) SceneManager.UnloadSceneAsync(Scene);
{
if (instance.Scene.IsValid() && instance.Scene.isLoaded)
SceneManager.UnloadSceneAsync(instance.Scene);
}
} }
#endif #endif
} }
public override void WaitForAsyncComplete()
{
throw new System.Exception($"Unity scene is not support {nameof(WaitForAsyncComplete)}.");
}
} }
} }

View File

@ -40,7 +40,7 @@ namespace YooAsset
else else
{ {
Status = EStatus.Loading; Status = EStatus.Loading;
} }
// 注意:模拟异步加载效果提前返回 // 注意:模拟异步加载效果提前返回
if (IsWaitForAsyncComplete == false) if (IsWaitForAsyncComplete == false)
@ -50,14 +50,21 @@ namespace YooAsset
// 1. 加载资源对象 // 1. 加载资源对象
if (Status == EStatus.Loading) if (Status == EStatus.Loading)
{ {
var findAssets = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(AssetPath); if (AssetType == null)
List<UnityEngine.Object> result = new List<Object>(findAssets.Length);
foreach (var findObj in findAssets)
{ {
if (findObj.GetType() == AssetType) AllAssets = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetPath);
result.Add(findObj); }
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; Status = EStatus.Checking;
} }
@ -66,7 +73,7 @@ namespace YooAsset
{ {
Status = AllAssets == null ? EStatus.Fail : EStatus.Success; Status = AllAssets == null ? EStatus.Fail : EStatus.Success;
if (Status == EStatus.Fail) if (Status == EStatus.Fail)
YooLogger.Warning($"Failed to load all asset object : {AssetPath}"); YooLogger.Warning($"Failed to load sub assets : {AssetName}");
InvokeCompletion(); InvokeCompletion();
} }
#endif #endif

View File

@ -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; }
}
}

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine.SceneManagement;
namespace YooAsset namespace YooAsset
{ {
@ -288,69 +289,103 @@ namespace YooAsset
AssetSystem.GetDebugReport(report); AssetSystem.GetDebugReport(report);
} }
#region 场景接口
/// <summary>
/// 异步加载场景
/// </summary>
/// <param name="location">场景对象相对路径</param>
/// <param name="mode">场景加载模式</param>
/// <param name="activateOnLoad">加载完毕时是否主动激活</param>
public static SceneOperationHandle LoadSceneAsync(string location, LoadSceneMode mode, bool activateOnLoad)
{
string scenePath = ConvertLocationToAssetPath(location);
var handle = AssetSystem.LoadSceneAsync(scenePath, mode, activateOnLoad);
return handle;
}
#endregion
#region 资源加载接口 #region 资源加载接口
/// <summary> /// <summary>
/// 同步加载资源对象 /// 同步加载资源对象
/// </summary> /// </summary>
/// <typeparam name="TObject">资源类型</typeparam>
/// <param name="location">资源对象相对路径</param> /// <param name="location">资源对象相对路径</param>
public static AssetOperationHandle LoadAssetSync<TObject>(string location) where TObject : class public static AssetOperationHandle LoadAssetSync<TObject>(string location) where TObject : class
{ {
return LoadAssetInternal(location, typeof(TObject), true); return LoadAssetInternal(location, typeof(TObject), true);
} }
public static AssetOperationHandle LoadAssetSync(System.Type type, string location)
/// <summary>
/// 同步加载资源对象
/// </summary>
/// <param name="location">资源对象相对路径</param>
/// <param name="type">资源类型</param>
public static AssetOperationHandle LoadAssetSync(string location, System.Type type)
{ {
return LoadAssetInternal(location, type, true); return LoadAssetInternal(location, type, true);
} }
/// <summary> /// <summary>
/// 同步加载子资源对象集合 /// 同步加载子资源对象
/// </summary> /// </summary>
/// <typeparam name="TObject">资源类型</typeparam>
/// <param name="location">资源对象相对路径</param> /// <param name="location">资源对象相对路径</param>
public static AssetOperationHandle LoadSubAssetsSync<TObject>(string location) public static AssetOperationHandle LoadSubAssetsSync<TObject>(string location)
{ {
return LoadSubAssetsInternal(location, typeof(TObject), true); return LoadSubAssetsInternal(location, typeof(TObject), true);
} }
public static AssetOperationHandle LoadSubAssetsSync(System.Type type, string location)
/// <summary>
/// 同步加载子资源对象
/// </summary>
/// <param name="location">资源对象相对路径</param>
/// <param name="type">子对象类型</param>
public static AssetOperationHandle LoadSubAssetsSync(string location, System.Type type)
{ {
return LoadSubAssetsInternal(location, type, true); return LoadSubAssetsInternal(location, type, true);
} }
/// <summary> /// <summary>
/// 异步加载场景 /// 异步加载资源对象
/// </summary> /// </summary>
public static AssetOperationHandle LoadSceneAsync(string location, SceneInstanceParam instanceParam) /// <typeparam name="TObject">资源类型</typeparam>
/// <param name="location">资源对象相对路径</param>
public static AssetOperationHandle LoadAssetAsync<TObject>(string location)
{ {
string scenePath = ConvertLocationToAssetPath(location); return LoadAssetInternal(location, typeof(TObject), false);
var handle = AssetSystem.LoadSceneAsync(scenePath, instanceParam);
return handle;
} }
/// <summary> /// <summary>
/// 异步加载资源对象 /// 异步加载资源对象
/// </summary> /// </summary>
/// <param name="location">资源对象相对路径</param> /// <param name="location">资源对象相对路径</param>
public static AssetOperationHandle LoadAssetAsync<TObject>(string location) /// <param name="type">资源类型</param>
{ public static AssetOperationHandle LoadAssetAsync(string location, System.Type type)
return LoadAssetInternal(location, typeof(TObject), false);
}
public static AssetOperationHandle LoadAssetAsync(System.Type type, string location)
{ {
return LoadAssetInternal(location, type, false); return LoadAssetInternal(location, type, false);
} }
/// <summary> /// <summary>
/// 异步加载子资源对象集合 /// 异步加载子资源对象
/// </summary> /// </summary>
/// <typeparam name="TObject">资源类型</typeparam>
/// <param name="location">资源对象相对路径</param> /// <param name="location">资源对象相对路径</param>
public static AssetOperationHandle LoadSubAssetsAsync<TObject>(string location) public static AssetOperationHandle LoadSubAssetsAsync<TObject>(string location)
{ {
return LoadSubAssetsInternal(location, typeof(TObject), false); return LoadSubAssetsInternal(location, typeof(TObject), false);
} }
public static AssetOperationHandle LoadSubAssetsAsync(System.Type type, string location)
/// <summary>
/// 异步加载子资源对象
/// </summary>
/// <param name="location">资源对象相对路径</param>
/// <param name="type">子对象类型</param>
public static AssetOperationHandle LoadSubAssetsAsync(string location, System.Type type)
{ {
return LoadSubAssetsInternal(location, type, false); return LoadSubAssetsInternal(location, type, false);
} }
private static AssetOperationHandle LoadAssetInternal(string location, System.Type assetType, bool waitForAsyncComplete) private static AssetOperationHandle LoadAssetInternal(string location, System.Type assetType, bool waitForAsyncComplete)
{ {