Add load all asset method.

增加新的资源加载方法:加载资源对象所属资源包里的所有资源。
This commit is contained in:
hevinci
2022-05-06 23:15:03 +08:00
parent 65875b66c2
commit afc08d4e46
18 changed files with 605 additions and 112 deletions
@@ -0,0 +1,116 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace YooAsset
{
internal sealed class BundledAllAssetsProvider : BundledProvider
{
private AssetBundleRequest _cacheRequest;
public override float Progress
{
get
{
if (_cacheRequest == null)
return 0;
return _cacheRequest.progress;
}
}
public BundledAllAssetsProvider(string assetPath, System.Type assetType)
: base(assetPath, assetType)
{
}
public override void Update()
{
if (IsDone)
return;
if (Status == EStatus.None)
{
Status = EStatus.CheckBundle;
}
// 1. 检测资源包
if (Status == EStatus.CheckBundle)
{
if (IsWaitForAsyncComplete)
{
DependBundles.WaitForAsyncComplete();
OwnerBundle.WaitForAsyncComplete();
}
if (DependBundles.IsDone() == false)
return;
if (OwnerBundle.IsDone() == false)
return;
if (DependBundles.IsSucceed() == false)
{
Status = EStatus.Fail;
LastError = DependBundles.GetLastError();
InvokeCompletion();
return;
}
if (OwnerBundle.Status != AssetBundleLoaderBase.EStatus.Succeed)
{
Status = EStatus.Fail;
LastError = OwnerBundle.LastError;
InvokeCompletion();
return;
}
Status = EStatus.Loading;
}
// 2. 加载资源对象
if (Status == EStatus.Loading)
{
if (IsWaitForAsyncComplete)
{
if (AssetType == null)
AllAssetObjects = OwnerBundle.CacheBundle.LoadAllAssets();
else
AllAssetObjects = OwnerBundle.CacheBundle.LoadAllAssets(AssetType);
}
else
{
if (AssetType == null)
_cacheRequest = OwnerBundle.CacheBundle.LoadAllAssetsAsync();
else
_cacheRequest = OwnerBundle.CacheBundle.LoadAllAssetsAsync(AssetType);
}
Status = EStatus.Checking;
}
// 3. 检测加载结果
if (Status == EStatus.Checking)
{
if (_cacheRequest != null)
{
if (IsWaitForAsyncComplete)
{
// 强制挂起主线程(注意:该操作会很耗时)
YooLogger.Warning("Suspend the main thread to load unity asset.");
AllAssetObjects = _cacheRequest.allAssets;
}
else
{
if (_cacheRequest.isDone == false)
return;
AllAssetObjects = _cacheRequest.allAssets;
}
}
Status = AllAssetObjects == null ? EStatus.Fail : EStatus.Success;
if (Status == EStatus.Fail)
{
LastError = $"Failed to load all assets from bundle : {OwnerBundle.BundleFileInfo.BundleName}";
YooLogger.Error(LastError);
}
InvokeCompletion();
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: dd972d8e33cbbaf4aa7f976d59896b0e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,127 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace YooAsset
{
internal sealed class DatabaseAllAssetsProvider : ProviderBase
{
private string _bundleName;
public override float Progress
{
get
{
if (IsDone)
return 100f;
else
return 0;
}
}
public DatabaseAllAssetsProvider(string assetPath, System.Type assetType)
: base(assetPath, assetType)
{
}
public override void Update()
{
#if UNITY_EDITOR
if (IsDone)
return;
if (Status == EStatus.None)
{
// 检测资源文件是否存在
string guid = UnityEditor.AssetDatabase.AssetPathToGUID(AssetPath);
if (string.IsNullOrEmpty(guid))
{
Status = EStatus.Fail;
LastError = $"Not found asset : {AssetPath}";
YooLogger.Error(LastError);
InvokeCompletion();
return;
}
// 获取资源包名称
_bundleName = AssetSystem.BundleServices.GetBundleName(AssetPath);
if (string.IsNullOrEmpty(_bundleName))
{
Status = EStatus.Fail;
LastError = $"Not found bundle name : {AssetPath}";
YooLogger.Error(LastError);
InvokeCompletion();
return;
}
Status = EStatus.Loading;
// 注意:模拟异步加载效果提前返回
if (IsWaitForAsyncComplete == false)
return;
}
// 1. 加载资源对象
if (Status == EStatus.Loading)
{
bool loadFailed = false;
if (AssetType == null)
{
List<UnityEngine.Object> result = new List<Object>(100);
AssetInfo[] allAssetInfos = AssetSystem.BundleServices.GetAssetInfos(_bundleName);
foreach (var assetInfo in allAssetInfos)
{
var assetObject = UnityEditor.AssetDatabase.LoadMainAssetAtPath(assetInfo.AssetPath);
if (assetObject != null)
{
result.Add(assetObject);
}
else
{
YooLogger.Warning($"Failed to load main asset : {assetInfo.AssetPath}");
loadFailed = true;
break;
}
}
if (loadFailed == false)
AllAssetObjects = result.ToArray();
}
else
{
List<UnityEngine.Object> result = new List<Object>(100);
AssetInfo[] allAssetInfos = AssetSystem.BundleServices.GetAssetInfos(_bundleName);
foreach (var assetInfo in allAssetInfos)
{
var assetObject = UnityEditor.AssetDatabase.LoadAssetAtPath(assetInfo.AssetPath, AssetType);
if (assetObject != null)
{
if (AssetType.IsAssignableFrom(assetObject.GetType()))
result.Add(assetObject);
}
else
{
YooLogger.Warning($"Failed to load asset : {assetInfo.AssetPath}");
loadFailed = true;
break;
}
}
if (loadFailed == false)
AllAssetObjects = result.ToArray();
}
Status = EStatus.Checking;
}
// 2. 检测加载结果
if (Status == EStatus.Checking)
{
Status = AllAssetObjects == null ? EStatus.Fail : EStatus.Success;
if (Status == EStatus.Fail)
{
LastError = $"Failed to load all assets : {nameof(AssetType)} in bundle {_bundleName}";
YooLogger.Error(LastError);
}
InvokeCompletion();
}
#endif
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b418b6a8da0e3d240b1566cc44fc4b2b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -39,10 +39,8 @@ namespace YooAsset
InvokeCompletion();
return;
}
else
{
Status = EStatus.Loading;
}
Status = EStatus.Loading;
// 注意:模拟异步加载效果提前返回
if (IsWaitForAsyncComplete == false)
@@ -52,7 +50,10 @@ namespace YooAsset
// 1. 加载资源对象
if (Status == EStatus.Loading)
{
AssetObject = UnityEditor.AssetDatabase.LoadAssetAtPath(AssetPath, AssetType);
if (AssetType == null)
AssetObject = UnityEditor.AssetDatabase.LoadMainAssetAtPath(AssetPath);
else
AssetObject = UnityEditor.AssetDatabase.LoadAssetAtPath(AssetPath, AssetType);
Status = EStatus.Checking;
}
@@ -70,7 +70,7 @@ namespace YooAsset
Status = SceneObject.IsValid() ? EStatus.Success : EStatus.Fail;
if (Status == EStatus.Fail)
{
LastError = $"The load scene is invalid : {AssetPath}";
LastError = $"The loaded scene is invalid : {AssetPath}";
YooLogger.Error(LastError);
}
InvokeCompletion();
@@ -39,10 +39,8 @@ namespace YooAsset
InvokeCompletion();
return;
}
else
{
Status = EStatus.Loading;
}
Status = EStatus.Loading;
// 注意:模拟异步加载效果提前返回
if (IsWaitForAsyncComplete == false)
@@ -62,7 +60,7 @@ namespace YooAsset
List<UnityEngine.Object> result = new List<Object>(findAssets.Length);
foreach (var findAsset in findAssets)
{
if (findAsset.GetType() == AssetType)
if (AssetType.IsAssignableFrom(findAsset.GetType()))
result.Add(findAsset);
}
AllAssetObjects = result.ToArray();
@@ -140,6 +140,8 @@ namespace YooAsset
handle = new SceneOperationHandle(this);
else if (IsSubAssetsProvider())
handle = new SubAssetsOperationHandle(this);
else if (IsAllAssetsProvider())
handle = new AllAssetsOperationHandle(this);
else
handle = new AssetOperationHandle(this);
@@ -162,28 +164,6 @@ namespace YooAsset
RefCount--;
}
/// <summary>
/// 是否为场景提供者
/// </summary>
public bool IsSceneProvider()
{
if (this is BundledSceneProvider || this is DatabaseSceneProvider)
return true;
else
return false;
}
/// <summary>
/// 是否为子资源对象提供者
/// </summary>
public bool IsSubAssetsProvider()
{
if (this is BundledSubAssetsProvider || this is DatabaseSubAssetsProvider)
return true;
else
return false;
}
/// <summary>
/// 等待异步执行完毕
/// </summary>
@@ -218,6 +198,28 @@ namespace YooAsset
}
}
public bool IsSceneProvider()
{
if (this is BundledSceneProvider || this is DatabaseSceneProvider)
return true;
else
return false;
}
public bool IsSubAssetsProvider()
{
if (this is BundledSubAssetsProvider || this is DatabaseSubAssetsProvider)
return true;
else
return false;
}
public bool IsAllAssetsProvider()
{
if (this is BundledAllAssetsProvider || this is DatabaseAllAssetsProvider)
return true;
else
return false;
}
#region 异步编程相关
private TaskCompletionSource<object> _taskCompletionSource;
protected void InvokeCompletion()