Initial commit

This commit is contained in:
hevinci
2022-03-01 10:44:12 +08:00
parent 1d54164bfb
commit e66853fd46
237 changed files with 14462 additions and 2 deletions
@@ -0,0 +1,138 @@
namespace YooAsset
{
internal abstract class AssetProviderBase : IAssetProvider
{
protected bool IsWaitForAsyncComplete { private set; get; } = false;
public string AssetPath { private set; get; }
public string AssetName { private set; get; }
public System.Type AssetType { private set; get; }
public UnityEngine.Object AssetObject { protected set; get; }
public UnityEngine.Object[] AllAssets { protected set; get; }
public IAssetInstance AssetInstance { protected set; get; }
public EAssetStates States { protected set; get; }
public int RefCount { private set; get; }
public AssetOperationHandle Handle { private set; get; }
public System.Action<AssetOperationHandle> Callback { set; get; }
public bool IsDestroyed { private set; get; } = false;
public bool IsDone
{
get
{
return States == EAssetStates.Success || States == EAssetStates.Fail;
}
}
public bool IsValid
{
get
{
return IsDestroyed == false;
}
}
public virtual float Progress
{
get
{
return 0;
}
}
public AssetProviderBase(string assetPath, System.Type assetType)
{
AssetPath = assetPath;
AssetName = System.IO.Path.GetFileName(assetPath);
AssetType = assetType;
States = EAssetStates.None;
Handle = new AssetOperationHandle(this);
}
public abstract void Update();
public virtual void Destory()
{
IsDestroyed = true;
}
public void Reference()
{
RefCount++;
}
public void Release()
{
if (RefCount <= 0)
Logger.Warning("Asset provider reference count is already zero. There may be resource leaks !");
RefCount--;
}
public bool CanDestroy()
{
if (IsDone == false)
return false;
return RefCount <= 0;
}
/// <summary>
/// 是否为场景提供者
/// </summary>
public bool IsSceneProvider()
{
if (this is BundledSceneProvider || this is DatabaseSceneProvider)
return true;
else
return false;
}
/// <summary>
/// 等待异步执行完毕
/// </summary>
public virtual void WaitForAsyncComplete()
{
IsWaitForAsyncComplete = true;
// 注意:主动轮询更新完成同步加载
Update();
// 验证结果
if (IsDone == false)
{
Logger.Warning($"WaitForAsyncComplete failed to loading : {AssetPath}");
}
}
/// <summary>
/// 异步操作任务
/// </summary>
System.Threading.Tasks.Task<object> IAssetProvider.Task
{
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()
{
Callback?.Invoke(Handle);
_waitHandle?.Set();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 02cc2ce3da5a88b48942ad96e6061aad
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,105 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace YooAsset
{
internal sealed class BundledAssetProvider : BundledProvider
{
private AssetBundleRequest _cacheRequest;
public override float Progress
{
get
{
if (_cacheRequest == null)
return 0;
return _cacheRequest.progress;
}
}
public BundledAssetProvider(string assetPath, System.Type assetType)
: base(assetPath, assetType)
{
}
public override void Update()
{
if (IsDone)
return;
if (States == EAssetStates.None)
{
States = EAssetStates.CheckBundle;
}
// 1. 检测资源包
if (States == EAssetStates.CheckBundle)
{
if (IsWaitForAsyncComplete)
{
DependBundles.WaitForAsyncComplete();
OwnerBundle.WaitForAsyncComplete();
}
if (DependBundles.IsDone() == false)
return;
if (OwnerBundle.IsDone() == false)
return;
if (OwnerBundle.CacheBundle == null)
{
States = EAssetStates.Fail;
InvokeCompletion();
}
else
{
States = EAssetStates.Loading;
}
}
// 2. 加载资源对象
if (States == EAssetStates.Loading)
{
if (IsWaitForAsyncComplete)
{
if (AssetType == null)
AssetObject = OwnerBundle.CacheBundle.LoadAsset(AssetName);
else
AssetObject = OwnerBundle.CacheBundle.LoadAsset(AssetName, AssetType);
}
else
{
if (AssetType == null)
_cacheRequest = OwnerBundle.CacheBundle.LoadAssetAsync(AssetName);
else
_cacheRequest = OwnerBundle.CacheBundle.LoadAssetAsync(AssetName, AssetType);
}
States = EAssetStates.Checking;
}
// 3. 检测加载结果
if (States == EAssetStates.Checking)
{
if (_cacheRequest != null)
{
if (IsWaitForAsyncComplete)
{
// 强制挂起主线程(注意:该操作会很耗时)
Logger.Warning("Suspend the main thread to load unity asset.");
AssetObject = _cacheRequest.asset;
}
else
{
if (_cacheRequest.isDone == false)
return;
AssetObject = _cacheRequest.asset;
}
}
States = AssetObject == null ? EAssetStates.Fail : EAssetStates.Success;
if (States == EAssetStates.Fail)
Logger.Warning($"Failed to load asset : {AssetName} from bundle : {OwnerBundle.BundleInfo.BundleName}");
InvokeCompletion();
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ca5e4bf0c3efe6742bb57b494487be52
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,51 @@
using System.Collections;
using System.Collections.Generic;
namespace YooAsset
{
internal abstract class BundledProvider : AssetProviderBase
{
protected BundleFileLoader OwnerBundle { private set; get; }
protected DependBundleGrouper DependBundles { private set; get; }
public BundledProvider(string assetPath, System.Type assetType) : base(assetPath, assetType)
{
OwnerBundle = AssetSystem.CreateOwnerBundleLoader(assetPath);
OwnerBundle.Reference();
OwnerBundle.AddProvider(this);
DependBundles = new DependBundleGrouper(assetPath);
DependBundles.Reference();
}
public override void Destory()
{
base.Destory();
// 释放资源包
if (OwnerBundle != null)
{
OwnerBundle.Release();
OwnerBundle = null;
}
if (DependBundles != null)
{
DependBundles.Release();
DependBundles = null;
}
}
/// <summary>
/// 获取资源包的调试信息列表
/// </summary>
internal void GetBundleDebugInfos(List<DebugSummy.BundleInfo> output)
{
var ownerInfo = new DebugSummy.BundleInfo();
ownerInfo.BundleName = OwnerBundle.BundleInfo.BundleName;
ownerInfo.Version = OwnerBundle.BundleInfo.Version;
ownerInfo.RefCount = OwnerBundle.RefCount;
ownerInfo.States = OwnerBundle.States;
output.Add(ownerInfo);
DependBundles.GetBundleDebugInfos(output);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4eacb00a7e3873740b599112dfcf0123
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,109 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace YooAsset
{
internal sealed class BundledSceneProvider : BundledProvider
{
private SceneInstanceParam _param;
private AsyncOperation _asyncOp;
public override float Progress
{
get
{
if (_asyncOp == null)
return 0;
return _asyncOp.progress;
}
}
public BundledSceneProvider(string scenePath, SceneInstanceParam param)
: base(scenePath, null)
{
_param = param;
}
public override void Update()
{
if (IsDone)
return;
if (States == EAssetStates.None)
{
States = EAssetStates.CheckBundle;
}
// 1. 检测资源包
if (States == EAssetStates.CheckBundle)
{
if (DependBundles.IsDone() == false)
return;
if (OwnerBundle.IsDone() == false)
return;
if (OwnerBundle.CacheBundle == null)
{
States = EAssetStates.Fail;
InvokeCompletion();
}
else
{
States = EAssetStates.Loading;
}
}
// 2. 加载场景
if (States == EAssetStates.Loading)
{
_asyncOp = SceneManager.LoadSceneAsync(AssetName, _param.LoadMode);
if (_asyncOp != null)
{
_asyncOp.allowSceneActivation = true;
States = EAssetStates.Checking;
}
else
{
Logger.Warning($"Failed to load scene : {AssetName}");
States = EAssetStates.Fail;
InvokeCompletion();
}
}
// 3. 检测加载结果
if (States == EAssetStates.Checking)
{
if (_asyncOp.isDone)
{
SceneInstance instance = new SceneInstance(_asyncOp);
instance.Scene = SceneManager.GetSceneByName(AssetName);
AssetInstance = instance;
if (_param.ActivateOnLoad)
instance.Activate();
States = instance.Scene.IsValid() ? EAssetStates.Success : EAssetStates.Fail;
InvokeCompletion();
}
}
}
public override void Destory()
{
base.Destory();
// 卸载附加场景(异步方式卸载)
if (_param.LoadMode == LoadSceneMode.Additive)
{
var instance = AssetInstance as SceneInstance;
if (instance != null && instance.Scene != null)
{
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)}.");
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 38b1b77cff590ca4e808c5068c9bf88b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,105 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace YooAsset
{
internal sealed class BundledSubAssetsProvider : BundledProvider
{
private AssetBundleRequest _cacheRequest;
public override float Progress
{
get
{
if (_cacheRequest == null)
return 0;
return _cacheRequest.progress;
}
}
public BundledSubAssetsProvider(string assetPath, System.Type assetType)
: base(assetPath, assetType)
{
}
public override void Update()
{
if (IsDone)
return;
if (States == EAssetStates.None)
{
States = EAssetStates.CheckBundle;
}
// 1. 检测资源包
if (States == EAssetStates.CheckBundle)
{
if (IsWaitForAsyncComplete)
{
DependBundles.WaitForAsyncComplete();
OwnerBundle.WaitForAsyncComplete();
}
if (DependBundles.IsDone() == false)
return;
if (OwnerBundle.IsDone() == false)
return;
if (OwnerBundle.CacheBundle == null)
{
States = EAssetStates.Fail;
InvokeCompletion();
}
else
{
States = EAssetStates.Loading;
}
}
// 2. 加载资源对象
if (States == EAssetStates.Loading)
{
if (IsWaitForAsyncComplete)
{
if (AssetType == null)
AllAssets = OwnerBundle.CacheBundle.LoadAssetWithSubAssets(AssetName);
else
AllAssets = OwnerBundle.CacheBundle.LoadAssetWithSubAssets(AssetName, AssetType);
}
else
{
if (AssetType == null)
_cacheRequest = OwnerBundle.CacheBundle.LoadAssetWithSubAssetsAsync(AssetName);
else
_cacheRequest = OwnerBundle.CacheBundle.LoadAssetWithSubAssetsAsync(AssetName, AssetType);
}
States = EAssetStates.Checking;
}
// 3. 检测加载结果
if (States == EAssetStates.Checking)
{
if (_cacheRequest != null)
{
if (IsWaitForAsyncComplete)
{
// 强制挂起主线程(注意:该操作会很耗时)
Logger.Warning("Suspend the main thread to load unity asset.");
AllAssets = _cacheRequest.allAssets;
}
else
{
if (_cacheRequest.isDone == false)
return;
AllAssets = _cacheRequest.allAssets;
}
}
States = AllAssets == null ? EAssetStates.Fail : EAssetStates.Success;
if (States == EAssetStates.Fail)
Logger.Warning($"Failed to load sub assets : {AssetName} from bundle : {OwnerBundle.BundleInfo.BundleName}");
InvokeCompletion();
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b3763bf52bde23b41a756f0d015cb30d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,68 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace YooAsset
{
internal sealed class DatabaseAssetProvider : AssetProviderBase
{
public override float Progress
{
get
{
if (IsDone)
return 100f;
else
return 0;
}
}
public DatabaseAssetProvider(string assetPath, System.Type assetType)
: base(assetPath, assetType)
{
}
public override void Update()
{
#if UNITY_EDITOR
if (IsDone)
return;
if (States == EAssetStates.None)
{
// 检测资源文件是否存在
string guid = UnityEditor.AssetDatabase.AssetPathToGUID(AssetPath);
if (string.IsNullOrEmpty(guid))
{
States = EAssetStates.Fail;
InvokeCompletion();
return;
}
else
{
States = EAssetStates.Loading;
}
// 注意:模拟异步加载效果提前返回
if (IsWaitForAsyncComplete == false)
return;
}
// 1. 加载资源对象
if (States == EAssetStates.Loading)
{
AssetObject = UnityEditor.AssetDatabase.LoadAssetAtPath(AssetPath, AssetType);
States = EAssetStates.Checking;
}
// 2. 检测加载结果
if (States == EAssetStates.Checking)
{
States = AssetObject == null ? EAssetStates.Fail : EAssetStates.Success;
if (States == EAssetStates.Fail)
Logger.Warning($"Failed to load asset object : {AssetPath}");
InvokeCompletion();
}
#endif
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d4022dd2ea39af5458fb1d61ec997347
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,94 @@
using UnityEngine;
using UnityEngine.SceneManagement;
namespace YooAsset
{
internal sealed class DatabaseSceneProvider : AssetProviderBase
{
private SceneInstanceParam _param;
private AsyncOperation _asyncOp;
public override float Progress
{
get
{
if (_asyncOp == null)
return 0;
return _asyncOp.progress;
}
}
public DatabaseSceneProvider(string scenePath, SceneInstanceParam param)
: base(scenePath, null)
{
_param = param;
}
public override void Update()
{
#if UNITY_EDITOR
if (IsDone)
return;
if (States == EAssetStates.None)
{
States = EAssetStates.Loading;
}
// 1. 加载资源对象
if (States == EAssetStates.Loading)
{
LoadSceneParameters loadSceneParameters = new LoadSceneParameters();
loadSceneParameters.loadSceneMode = _param.LoadMode;
_asyncOp = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(AssetPath, loadSceneParameters);
if (_asyncOp != null)
{
_asyncOp.allowSceneActivation = true;
States = EAssetStates.Checking;
}
else
{
Logger.Warning($"Failed to load scene : {AssetName}");
States = EAssetStates.Fail;
InvokeCompletion();
}
}
// 2. 检测加载结果
if (States == EAssetStates.Checking)
{
if (_asyncOp.isDone)
{
SceneInstance instance = new SceneInstance(_asyncOp);
instance.Scene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
AssetInstance = instance;
if(_param.ActivateOnLoad)
instance.Activate();
States = instance.Scene.IsValid() ? EAssetStates.Success : EAssetStates.Fail;
InvokeCompletion();
}
}
#endif
}
public override void Destory()
{
#if UNITY_EDITOR
base.Destory();
// 卸载附加场景(异步方式卸载)
if (_param.LoadMode == LoadSceneMode.Additive)
{
var instance = AssetInstance as SceneInstance;
if(instance != null && instance.Scene != null)
{
if (instance.Scene.IsValid() && instance.Scene.isLoaded)
SceneManager.UnloadSceneAsync(instance.Scene);
}
}
#endif
}
public override void WaitForAsyncComplete()
{
throw new System.Exception($"Unity scene is not support {nameof(WaitForAsyncComplete)}.");
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8252a639423064f498ed22f14912adae
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,75 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace YooAsset
{
internal sealed class DatabaseSubAssetsProvider : AssetProviderBase
{
public override float Progress
{
get
{
if (IsDone)
return 100f;
else
return 0;
}
}
public DatabaseSubAssetsProvider(string assetPath, System.Type assetType)
: base(assetPath, assetType)
{
}
public override void Update()
{
#if UNITY_EDITOR
if (IsDone)
return;
if (States == EAssetStates.None)
{
// 检测资源文件是否存在
string guid = UnityEditor.AssetDatabase.AssetPathToGUID(AssetPath);
if (string.IsNullOrEmpty(guid))
{
States = EAssetStates.Fail;
InvokeCompletion();
return;
}
else
{
States = EAssetStates.Loading;
}
// 注意:模拟异步加载效果提前返回
if (IsWaitForAsyncComplete == false)
return;
}
// 1. 加载资源对象
if (States == EAssetStates.Loading)
{
var findAssets = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(AssetPath);
List<UnityEngine.Object> result = new List<Object>(findAssets.Length);
foreach (var findObj in findAssets)
{
if (findObj.GetType() == AssetType)
result.Add(findObj);
}
AllAssets = result.ToArray();
States = EAssetStates.Checking;
}
// 2. 检测加载结果
if (States == EAssetStates.Checking)
{
States = AllAssets == null ? EAssetStates.Fail : EAssetStates.Success;
if (States == EAssetStates.Fail)
Logger.Warning($"Failed to load all asset object : {AssetPath}");
InvokeCompletion();
}
#endif
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3837962b901d7ba4abf02a9991ac4c4a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,16 @@
namespace YooAsset
{
/// <summary>
/// 资源加载状态
/// </summary>
public enum EAssetStates
{
None = 0,
CheckBundle,
Loading,
Checking,
Success,
Fail,
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: aa6c0fe63326c604bb699be16747c1e2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,114 @@
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>
EAssetStates States { 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; }
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 54f7b01043e68994189cc954e55813aa
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: