From afc08d4e466402970a030f639f391a5eb6b04094 Mon Sep 17 00:00:00 2001 From: hevinci Date: Fri, 6 May 2022 23:15:03 +0800 Subject: [PATCH] Add load all asset method. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加新的资源加载方法:加载资源对象所属资源包里的所有资源。 --- .../Runtime/AssetSystem/AssetSystem.cs | 24 +- .../Handles/AllAssetsOperationHandle.cs | 69 +++++ .../Handles/AllAssetsOperationHandle.cs.meta | 11 + .../Provider/BundledAllAssetsProvider.cs | 116 +++++++++ .../Provider/BundledAllAssetsProvider.cs.meta | 11 + .../Provider/DatabaseAllAssetsProvider.cs | 127 ++++++++++ .../DatabaseAllAssetsProvider.cs.meta | 11 + .../Provider/DatabaseAssetProvider.cs | 11 +- .../Provider/DatabaseSceneProvider.cs | 2 +- .../Provider/DatabaseSubAssetsProvider.cs | 8 +- .../AssetSystem/Provider/ProviderBase.cs | 46 ++-- .../Runtime/PatchSystem/PatchManifest.cs | 2 +- .../PlayMode/EditorSimulateModeImpl.cs | 6 +- .../PatchSystem/PlayMode/HostPlayModeImpl.cs | 6 +- .../PlayMode/OfflinePlayModeImpl.cs | 6 +- .../Runtime/Services/IBundleServices.cs | 5 + Assets/YooAsset/Runtime/Utility/YooHelper.cs | 17 +- Assets/YooAsset/Runtime/YooAssets.cs | 239 ++++++++++++------ 18 files changed, 605 insertions(+), 112 deletions(-) create mode 100644 Assets/YooAsset/Runtime/AssetSystem/Handles/AllAssetsOperationHandle.cs create mode 100644 Assets/YooAsset/Runtime/AssetSystem/Handles/AllAssetsOperationHandle.cs.meta create mode 100644 Assets/YooAsset/Runtime/AssetSystem/Provider/BundledAllAssetsProvider.cs create mode 100644 Assets/YooAsset/Runtime/AssetSystem/Provider/BundledAllAssetsProvider.cs.meta create mode 100644 Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseAllAssetsProvider.cs create mode 100644 Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseAllAssetsProvider.cs.meta diff --git a/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs b/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs index a80e830..ef158fb 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs @@ -130,7 +130,7 @@ namespace YooAsset /// - /// 异步加载场景 + /// 加载场景 /// public static SceneOperationHandle LoadSceneAsync(string scenePath, LoadSceneMode sceneMode, bool activateOnLoad, int priority) { @@ -161,7 +161,7 @@ namespace YooAsset } /// - /// 异步加载资源对象 + /// 加载资源对象 /// public static AssetOperationHandle LoadAssetAsync(string assetPath, System.Type assetType) { @@ -179,7 +179,7 @@ namespace YooAsset } /// - /// 异步加载所有子资源对象 + /// 加载子资源对象 /// public static SubAssetsOperationHandle LoadSubAssetsAsync(string assetPath, System.Type assetType) { @@ -196,6 +196,24 @@ namespace YooAsset return provider.CreateHandle() as SubAssetsOperationHandle; } + /// + /// 加载资源包里的所有资源对象 + /// + public static AllAssetsOperationHandle LoadAllAssetsAsync(string assetPath, System.Type assetType) + { + ProviderBase provider = TryGetProvider(assetPath); + if (provider == null) + { + if (SimulationOnEditor) + provider = new DatabaseAllAssetsProvider(assetPath, assetType); + else + provider = new BundledAllAssetsProvider(assetPath, assetType); + provider.InitSpawnDebugInfo(); + _providers.Add(provider); + } + return provider.CreateHandle() as AllAssetsOperationHandle; + } + internal static void UnloadSubScene(ProviderBase provider) { diff --git a/Assets/YooAsset/Runtime/AssetSystem/Handles/AllAssetsOperationHandle.cs b/Assets/YooAsset/Runtime/AssetSystem/Handles/AllAssetsOperationHandle.cs new file mode 100644 index 0000000..3d0ae3e --- /dev/null +++ b/Assets/YooAsset/Runtime/AssetSystem/Handles/AllAssetsOperationHandle.cs @@ -0,0 +1,69 @@ + +namespace YooAsset +{ + public sealed class AllAssetsOperationHandle : OperationHandleBase + { + private System.Action _callback; + + internal AllAssetsOperationHandle(ProviderBase provider) : base(provider) + { + } + internal override void InvokeCallback() + { + _callback?.Invoke(this); + } + + /// + /// 完成委托 + /// + public event System.Action Completed + { + add + { + if (IsValid == false) + throw new System.Exception($"{nameof(AllAssetsOperationHandle)} is invalid"); + if (Provider.IsDone) + value.Invoke(this); + else + _callback += value; + } + remove + { + if (IsValid == false) + throw new System.Exception($"{nameof(AllAssetsOperationHandle)} is invalid"); + _callback -= value; + } + } + + /// + /// 资源包内的资源对象集合 + /// + public UnityEngine.Object[] AllAssetObjects + { + get + { + if (IsValid == false) + return null; + return Provider.AllAssetObjects; + } + } + + /// + /// 等待异步执行完毕 + /// + public void WaitForAsyncComplete() + { + if (IsValid == false) + return; + Provider.WaitForAsyncComplete(); + } + + /// + /// 释放资源句柄 + /// + public void Release() + { + this.ReleaseInternal(); + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/AssetSystem/Handles/AllAssetsOperationHandle.cs.meta b/Assets/YooAsset/Runtime/AssetSystem/Handles/AllAssetsOperationHandle.cs.meta new file mode 100644 index 0000000..b5dd03a --- /dev/null +++ b/Assets/YooAsset/Runtime/AssetSystem/Handles/AllAssetsOperationHandle.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 05c287035a2264f469a654a6152b02c9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledAllAssetsProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledAllAssetsProvider.cs new file mode 100644 index 0000000..420a2b6 --- /dev/null +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledAllAssetsProvider.cs @@ -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(); + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledAllAssetsProvider.cs.meta b/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledAllAssetsProvider.cs.meta new file mode 100644 index 0000000..c2a5778 --- /dev/null +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledAllAssetsProvider.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: dd972d8e33cbbaf4aa7f976d59896b0e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseAllAssetsProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseAllAssetsProvider.cs new file mode 100644 index 0000000..b88f6b6 --- /dev/null +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseAllAssetsProvider.cs @@ -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 result = new List(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 result = new List(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 + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseAllAssetsProvider.cs.meta b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseAllAssetsProvider.cs.meta new file mode 100644 index 0000000..cec238b --- /dev/null +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseAllAssetsProvider.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b418b6a8da0e3d240b1566cc44fc4b2b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseAssetProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseAssetProvider.cs index 660fd3f..29fdbbd 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseAssetProvider.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseAssetProvider.cs @@ -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; } diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSceneProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSceneProvider.cs index f60fcba..c2a17bf 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSceneProvider.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSceneProvider.cs @@ -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(); diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSubAssetsProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSubAssetsProvider.cs index 216f210..a633835 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSubAssetsProvider.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSubAssetsProvider.cs @@ -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 result = new List(findAssets.Length); foreach (var findAsset in findAssets) { - if (findAsset.GetType() == AssetType) + if (AssetType.IsAssignableFrom(findAsset.GetType())) result.Add(findAsset); } AllAssetObjects = result.ToArray(); diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/ProviderBase.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/ProviderBase.cs index ab1ce1d..4b75c6d 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/ProviderBase.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/ProviderBase.cs @@ -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--; } - /// - /// 是否为场景提供者 - /// - 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; - } - /// /// 等待异步执行完毕 /// @@ -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 _taskCompletionSource; protected void InvokeCompletion() diff --git a/Assets/YooAsset/Runtime/PatchSystem/PatchManifest.cs b/Assets/YooAsset/Runtime/PatchSystem/PatchManifest.cs index 2152ef4..06e84de 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PatchManifest.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PatchManifest.cs @@ -210,7 +210,7 @@ namespace YooAsset } else { - YooLogger.Warning($"Failed to mapping location to asset path : {location}"); + YooLogger.Error($"Failed to mapping location to asset path : {location}"); return string.Empty; } } diff --git a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeImpl.cs b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeImpl.cs index a564988..0059576 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeImpl.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeImpl.cs @@ -56,9 +56,13 @@ namespace YooAsset return bundleInfo; } } + AssetInfo[] IBundleServices.GetAssetInfos(string bundleName) + { + return PatchHelper.GetAssetsInfoByBundleName(_simulatePatchManifest, bundleName); + } AssetInfo[] IBundleServices.GetAssetInfos(string[] tags) { - return PatchHelper.GetAssetsInfoByTag(_simulatePatchManifest, tags); + return PatchHelper.GetAssetsInfoByTags(_simulatePatchManifest, tags); } string IBundleServices.MappingToAssetPath(string location) { diff --git a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs index ef60c21..8d608dd 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs @@ -322,9 +322,13 @@ namespace YooAsset return bundleInfo; } } + AssetInfo[] IBundleServices.GetAssetInfos(string bundleName) + { + return PatchHelper.GetAssetsInfoByBundleName(LocalPatchManifest, bundleName); + } AssetInfo[] IBundleServices.GetAssetInfos(string[] tags) { - return PatchHelper.GetAssetsInfoByTag(LocalPatchManifest, tags); + return PatchHelper.GetAssetsInfoByTags(LocalPatchManifest, tags); } string IBundleServices.MappingToAssetPath(string location) { diff --git a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs index 78c73ab..2847a16 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs @@ -65,9 +65,13 @@ namespace YooAsset return bundleInfo; } } + AssetInfo[] IBundleServices.GetAssetInfos(string bundleName) + { + return PatchHelper.GetAssetsInfoByBundleName(_appPatchManifest, bundleName); + } AssetInfo[] IBundleServices.GetAssetInfos(string[] tags) { - return PatchHelper.GetAssetsInfoByTag(_appPatchManifest, tags); + return PatchHelper.GetAssetsInfoByTags(_appPatchManifest, tags); } string IBundleServices.MappingToAssetPath(string location) { diff --git a/Assets/YooAsset/Runtime/Services/IBundleServices.cs b/Assets/YooAsset/Runtime/Services/IBundleServices.cs index fd4e219..7fea0d0 100644 --- a/Assets/YooAsset/Runtime/Services/IBundleServices.cs +++ b/Assets/YooAsset/Runtime/Services/IBundleServices.cs @@ -8,6 +8,11 @@ namespace YooAsset /// BundleInfo GetBundleInfo(string bundleName); + /// + /// 获取资源信息列表 + /// + AssetInfo[] GetAssetInfos(string bundleName); + /// /// 获取资源信息列表 /// diff --git a/Assets/YooAsset/Runtime/Utility/YooHelper.cs b/Assets/YooAsset/Runtime/Utility/YooHelper.cs index 22cd528..0ab520c 100644 --- a/Assets/YooAsset/Runtime/Utility/YooHelper.cs +++ b/Assets/YooAsset/Runtime/Utility/YooHelper.cs @@ -173,7 +173,7 @@ namespace YooAsset /// /// 获取资源信息列表 /// - public static AssetInfo[] GetAssetsInfoByTag(PatchManifest patchManifest, string[] tags) + public static AssetInfo[] GetAssetsInfoByTags(PatchManifest patchManifest, string[] tags) { List result = new List(100); foreach (var patchAsset in patchManifest.AssetList) @@ -189,5 +189,20 @@ namespace YooAsset } return result.ToArray(); } + + /// + /// 获取资源信息列表 + /// + public static AssetInfo[] GetAssetsInfoByBundleName(PatchManifest patchManifest, string bundleName) + { + List result = new List(100); + foreach (var patchAsset in patchManifest.AssetList) + { + string tempName = patchManifest.GetBundleName(patchAsset.AssetPath); + if (tempName == bundleName) + result.Add(new AssetInfo(patchAsset.AssetPath)); + } + return result.ToArray(); + } } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/YooAssets.cs b/Assets/YooAsset/Runtime/YooAssets.cs index d940c11..49a0fab 100644 --- a/Assets/YooAsset/Runtime/YooAssets.cs +++ b/Assets/YooAsset/Runtime/YooAssets.cs @@ -18,7 +18,7 @@ namespace YooAsset /// 注意:在初始化的时候自动构建真机模拟环境。 /// EditorSimulateMode, - + /// /// 离线运行模式 /// @@ -368,6 +368,17 @@ namespace YooAsset return _bundleServices.GetAssetInfos(tags); } + /// + /// 获取资源信息列表 + /// + /// 资源标签列表 + /// + public static AssetInfo[] GetAssetInfos(string[] tags) + { + DebugCheckInitialize(); + return _bundleServices.GetAssetInfos(tags); + } + /// /// 获取调试信息 /// @@ -436,6 +447,37 @@ namespace YooAsset } + private static RawFileOperation GetRawFileInternal(string assetPath, string copyPath) + { + string bundleName = _bundleServices.GetBundleName(assetPath); + BundleInfo bundleInfo = _bundleServices.GetBundleInfo(bundleName); + + if (_playMode == EPlayMode.EditorSimulateMode) + { + RawFileOperation operation = new EditorPlayModeRawFileOperation(bundleInfo, copyPath); + OperationSystem.ProcessOperaiton(operation); + return operation; + } + else if (_playMode == EPlayMode.OfflinePlayMode) + { + RawFileOperation operation = new OfflinePlayModeRawFileOperation(bundleInfo, copyPath); + OperationSystem.ProcessOperaiton(operation); + return operation; + } + else if (_playMode == EPlayMode.HostPlayMode) + { + RawFileOperation operation = new HostPlayModeRawFileOperation(bundleInfo, copyPath); + OperationSystem.ProcessOperaiton(operation); + return operation; + } + else + { + throw new NotImplementedException(); + } + } + #endregion + + #region 资源加载 /// /// 同步加载资源对象 /// @@ -471,41 +513,6 @@ namespace YooAsset } - /// - /// 同步加载子资源对象 - /// - /// 资源信息 - public static SubAssetsOperationHandle LoadSubAssetsSync(AssetInfo assetInfo) - { - DebugCheckInitialize(); - return LoadSubAssetsInternal(assetInfo.AssetPath, assetInfo.AssetType, true); - } - - /// - /// 同步加载子资源对象 - /// - /// 资源类型 - /// 资源的定位地址 - public static SubAssetsOperationHandle LoadSubAssetsSync(string location) - { - DebugCheckInitialize(); - string assetPath = _locationServices.ConvertLocationToAssetPath(location); - return LoadSubAssetsInternal(assetPath, typeof(TObject), true); - } - - /// - /// 同步加载子资源对象 - /// - /// 资源的定位地址 - /// 子对象类型 - public static SubAssetsOperationHandle LoadSubAssetsSync(string location, System.Type type) - { - DebugCheckInitialize(); - string assetPath = _locationServices.ConvertLocationToAssetPath(location); - return LoadSubAssetsInternal(assetPath, type, true); - } - - /// /// 异步加载资源对象 /// @@ -541,6 +548,51 @@ namespace YooAsset } + private static AssetOperationHandle LoadAssetInternal(string assetPath, System.Type assetType, bool waitForAsyncComplete) + { + var handle = AssetSystem.LoadAssetAsync(assetPath, assetType); + if (waitForAsyncComplete) + handle.WaitForAsyncComplete(); + return handle; + } + #endregion + + #region 资源加载 + /// + /// 同步加载子资源对象 + /// + /// 资源信息 + public static SubAssetsOperationHandle LoadSubAssetsSync(AssetInfo assetInfo) + { + DebugCheckInitialize(); + return LoadSubAssetsInternal(assetInfo.AssetPath, assetInfo.AssetType, true); + } + + /// + /// 同步加载子资源对象 + /// + /// 资源类型 + /// 资源的定位地址 + public static SubAssetsOperationHandle LoadSubAssetsSync(string location) + { + DebugCheckInitialize(); + string assetPath = _locationServices.ConvertLocationToAssetPath(location); + return LoadSubAssetsInternal(assetPath, typeof(TObject), true); + } + + /// + /// 同步加载子资源对象 + /// + /// 资源的定位地址 + /// 子对象类型 + public static SubAssetsOperationHandle LoadSubAssetsSync(string location, System.Type type) + { + DebugCheckInitialize(); + string assetPath = _locationServices.ConvertLocationToAssetPath(location); + return LoadSubAssetsInternal(assetPath, type, true); + } + + /// /// 异步加载子资源对象 /// @@ -576,44 +628,89 @@ namespace YooAsset } - private static RawFileOperation GetRawFileInternal(string assetPath, string copyPath) + private static SubAssetsOperationHandle LoadSubAssetsInternal(string assetPath, System.Type assetType, bool waitForAsyncComplete) { - string bundleName = _bundleServices.GetBundleName(assetPath); - BundleInfo bundleInfo = _bundleServices.GetBundleInfo(bundleName); - - if (_playMode == EPlayMode.EditorSimulateMode) - { - RawFileOperation operation = new EditorPlayModeRawFileOperation(bundleInfo, copyPath); - OperationSystem.ProcessOperaiton(operation); - return operation; - } - else if (_playMode == EPlayMode.OfflinePlayMode) - { - RawFileOperation operation = new OfflinePlayModeRawFileOperation(bundleInfo, copyPath); - OperationSystem.ProcessOperaiton(operation); - return operation; - } - else if (_playMode == EPlayMode.HostPlayMode) - { - RawFileOperation operation = new HostPlayModeRawFileOperation(bundleInfo, copyPath); - OperationSystem.ProcessOperaiton(operation); - return operation; - } - else - { - throw new NotImplementedException(); - } - } - private static AssetOperationHandle LoadAssetInternal(string assetPath, System.Type assetType, bool waitForAsyncComplete) - { - var handle = AssetSystem.LoadAssetAsync(assetPath, assetType); + var handle = AssetSystem.LoadSubAssetsAsync(assetPath, assetType); if (waitForAsyncComplete) handle.WaitForAsyncComplete(); return handle; } - private static SubAssetsOperationHandle LoadSubAssetsInternal(string assetPath, System.Type assetType, bool waitForAsyncComplete) + #endregion + + #region 资源加载 + /// + /// 同步加载资源对象所属资源包里的所有资源 + /// + /// 资源信息 + public static AllAssetsOperationHandle LoadAllAssetsSync(AssetInfo assetInfo) { - var handle = AssetSystem.LoadSubAssetsAsync(assetPath, assetType); + DebugCheckInitialize(); + return LoadAllAssetsInternal(assetInfo.AssetPath, assetInfo.AssetType, true); + } + + /// + /// 同步加载资源对象所属资源包里的所有资源 + /// + /// 资源类型 + /// 资源的定位地址 + public static AllAssetsOperationHandle LoadAllAssetsSync(string location) + { + DebugCheckInitialize(); + string assetPath = _locationServices.ConvertLocationToAssetPath(location); + return LoadAllAssetsInternal(assetPath, typeof(TObject), true); + } + + /// + /// 同步加载资源对象所属资源包里的所有资源 + /// + /// 资源的定位地址 + /// 资源类型 + public static AllAssetsOperationHandle LoadAllAssetsSync(string location, System.Type type) + { + DebugCheckInitialize(); + string assetPath = _locationServices.ConvertLocationToAssetPath(location); + return LoadAllAssetsInternal(assetPath, type, true); + } + + + /// + /// 异步加载资源对象所属资源包里的所有资源 + /// + /// 资源信息 + public static AllAssetsOperationHandle LoadAllAssetsAsync(AssetInfo assetInfo) + { + DebugCheckInitialize(); + return LoadAllAssetsInternal(assetInfo.AssetPath, assetInfo.AssetType, false); + } + + /// + /// 异步加载资源对象所属资源包里的所有资源 + /// + /// 资源类型 + /// 资源的定位地址 + public static AllAssetsOperationHandle LoadAllAssetsAsync(string location) + { + DebugCheckInitialize(); + string assetPath = _locationServices.ConvertLocationToAssetPath(location); + return LoadAllAssetsInternal(assetPath, typeof(TObject), false); + } + + /// + /// 异步加载资源对象所属资源包里的所有资源 + /// + /// 资源的定位地址 + /// 资源类型 + public static AllAssetsOperationHandle LoadAllAssetsAsync(string location, System.Type type) + { + DebugCheckInitialize(); + string assetPath = _locationServices.ConvertLocationToAssetPath(location); + return LoadAllAssetsInternal(assetPath, type, false); + } + + + private static AllAssetsOperationHandle LoadAllAssetsInternal(string assetPath, System.Type assetType, bool waitForAsyncComplete) + { + var handle = AssetSystem.LoadAllAssetsAsync(assetPath, assetType); if (waitForAsyncComplete) handle.WaitForAsyncComplete(); return handle; @@ -909,7 +1006,7 @@ namespace YooAsset { if (string.IsNullOrEmpty(location)) { - UnityEngine.Debug.LogError("location param is null or empty!"); + YooLogger.Error("location param is null or empty!"); } else { @@ -918,11 +1015,11 @@ namespace YooAsset if (index != -1) { if (location.Length == index + 1) - UnityEngine.Debug.LogWarning($"Found blank character in location : \"{location}\""); + YooLogger.Warning($"Found blank character in location : \"{location}\""); } if (location.IndexOfAny(System.IO.Path.GetInvalidPathChars()) >= 0) - UnityEngine.Debug.LogWarning($"Found illegal character in location : \"{location}\""); + YooLogger.Warning($"Found illegal character in location : \"{location}\""); } } #endregion