mirror of https://github.com/tuyoogame/YooAsset
Update AssetSystem
parent
fa0009685d
commit
2bf36ebd8b
|
@ -2,6 +2,7 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
|
@ -163,21 +164,18 @@ namespace YooAsset
|
|||
/// 异步加载场景
|
||||
/// </summary>
|
||||
/// <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);
|
||||
if (provider == null)
|
||||
{
|
||||
if (SimulationOnEditor)
|
||||
provider = new DatabaseSceneProvider(scenePath, instanceParam);
|
||||
provider = new DatabaseSceneProvider(scenePath, mode, activateOnLoad);
|
||||
else
|
||||
provider = new BundledSceneProvider(scenePath, instanceParam);
|
||||
provider = new BundledSceneProvider(scenePath, mode, activateOnLoad);
|
||||
_providers.Add(provider);
|
||||
}
|
||||
|
||||
// 引用计数增加
|
||||
provider.Reference();
|
||||
return provider.Handle;
|
||||
return provider.CreateHandle() as SceneOperationHandle;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -196,10 +194,7 @@ namespace YooAsset
|
|||
provider = new BundledAssetProvider(assetPath, assetType);
|
||||
_providers.Add(provider);
|
||||
}
|
||||
|
||||
// 引用计数增加
|
||||
provider.Reference();
|
||||
return provider.Handle;
|
||||
return provider.CreateHandle() as AssetOperationHandle;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -218,10 +213,7 @@ namespace YooAsset
|
|||
provider = new BundledSubAssetsProvider(assetPath, assetType);
|
||||
_providers.Add(provider);
|
||||
}
|
||||
|
||||
// 引用计数增加
|
||||
provider.Reference();
|
||||
return provider.Handle;
|
||||
return provider.CreateHandle() as AssetOperationHandle;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a9b18d6c47ddf9b4296619d3d50ff826
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -1,7 +0,0 @@
|
|||
|
||||
namespace YooAsset
|
||||
{
|
||||
public interface IAssetInstance
|
||||
{
|
||||
}
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e1d0cfc6a2d5b1f4d9b9ac8f93682497
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -1,7 +0,0 @@
|
|||
|
||||
namespace YooAsset
|
||||
{
|
||||
public interface IAssetParam
|
||||
{
|
||||
}
|
||||
}
|
|
@ -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; }
|
||||
}
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 0ab6e7cef318cff4684ed733679bca0e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -1,69 +1,19 @@
|
|||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 句柄是否有效(AssetFileLoader销毁会导致所有句柄失效)
|
||||
/// </summary>
|
||||
public bool IsValid
|
||||
internal override void InvokeCallback()
|
||||
{
|
||||
get
|
||||
if (IsValid)
|
||||
{
|
||||
return _provider != null && _provider.IsValid;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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;
|
||||
_callback?.Invoke(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -79,13 +29,13 @@ namespace YooAsset
|
|||
if (_provider.IsDone)
|
||||
value.Invoke(this);
|
||||
else
|
||||
_provider.Callback += value;
|
||||
_callback += value;
|
||||
}
|
||||
remove
|
||||
{
|
||||
if (IsValid == false)
|
||||
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>
|
||||
/// 初始化的游戏对象(只限于请求的资源对象类型为GameObject)
|
||||
/// </summary>
|
||||
|
@ -143,17 +67,6 @@ namespace YooAsset
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放资源句柄
|
||||
/// </summary>
|
||||
public void Release()
|
||||
{
|
||||
if (IsValid == false)
|
||||
return;
|
||||
_provider.Release();
|
||||
_provider = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 等待异步执行完毕
|
||||
/// </summary>
|
||||
|
@ -163,28 +76,5 @@ namespace YooAsset
|
|||
return;
|
||||
_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
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 54f7b01043e68994189cc954e55813aa
|
||||
guid: a9f5acda1a4584648af7b47e19a0d24f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a8995b1e1e511714b948f7fa6275028e
|
||||
guid: c5d788ba2dd3c5243ae2e0be8e780825
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
|
@ -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,6 +1,7 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
|
@ -288,69 +289,103 @@ namespace YooAsset
|
|||
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 资源加载接口
|
||||
/// <summary>
|
||||
/// 同步加载资源对象
|
||||
/// </summary>
|
||||
/// <typeparam name="TObject">资源类型</typeparam>
|
||||
/// <param name="location">资源对象相对路径</param>
|
||||
public static AssetOperationHandle LoadAssetSync<TObject>(string location) where TObject : class
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 同步加载子资源对象集合
|
||||
/// 同步加载子资源对象
|
||||
/// </summary>
|
||||
/// <typeparam name="TObject">资源类型</typeparam>
|
||||
/// <param name="location">资源对象相对路径</param>
|
||||
public static AssetOperationHandle LoadSubAssetsSync<TObject>(string location)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
/// <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);
|
||||
var handle = AssetSystem.LoadSceneAsync(scenePath, instanceParam);
|
||||
return handle;
|
||||
return LoadAssetInternal(location, typeof(TObject), false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载资源对象
|
||||
/// </summary>
|
||||
/// <param name="location">资源对象相对路径</param>
|
||||
public static AssetOperationHandle LoadAssetAsync<TObject>(string location)
|
||||
{
|
||||
return LoadAssetInternal(location, typeof(TObject), false);
|
||||
}
|
||||
public static AssetOperationHandle LoadAssetAsync(System.Type type, string location)
|
||||
/// <param name="type">资源类型</param>
|
||||
public static AssetOperationHandle LoadAssetAsync(string location, System.Type type)
|
||||
{
|
||||
return LoadAssetInternal(location, type, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载子资源对象集合
|
||||
/// 异步加载子资源对象
|
||||
/// </summary>
|
||||
/// <typeparam name="TObject">资源类型</typeparam>
|
||||
/// <param name="location">资源对象相对路径</param>
|
||||
public static AssetOperationHandle LoadSubAssetsAsync<TObject>(string location)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
private static AssetOperationHandle LoadAssetInternal(string location, System.Type assetType, bool waitForAsyncComplete)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue