refactor : 重构代码

This commit is contained in:
何冠峰
2026-01-17 16:12:03 +08:00
parent 3e3661ad95
commit da222c6179
9 changed files with 269 additions and 315 deletions
@@ -9,9 +9,8 @@ namespace YooAsset
public class ResourcePackage
{
private InitializePackageOperation _initializeOp;
internal ResourceManager _resourceManager;
internal FileSystemHost _fileSystemHost;
internal EPlayMode _playMode;
private ResourceManager _resourceManager;
private FileSystemHost _fileSystemHost;
/// <summary>
/// 包裹名
@@ -53,15 +52,16 @@ namespace YooAsset
set { OperationSystem.SetSchedulerPriority(PackageName, value); }
}
internal ResourcePackage(string packageName)
{
PackageName = packageName;
}
/// <summary>
/// 销毁资源包裹
/// </summary>
internal void InternalInitialize(ResourceManager manager, FileSystemHost host)
{
_resourceManager = manager;
_fileSystemHost = host;
}
internal void InternalDestroy()
{
_initializeOp = null;
@@ -91,7 +91,7 @@ namespace YooAsset
// 检测重复初始化
if (_initializeOp != null)
throw new YooPackageException(PackageName, $"Package '{PackageName}' is already initialized !");
throw new YooPackageException(PackageName, $"Resource package '{PackageName}' is already initialized!");
// 开始初始化操作
_initializeOp = new InitializePackageOperation(this, options);
@@ -113,7 +113,7 @@ namespace YooAsset
{
var options = new UnloadAllAssetsOptions(true, true);
var operation = new DestroyPackageOperation(this, options);
OperationSystem.StartOperation(OperationSystem.GLOBAL_SCHEDULER_NAME, operation);
OperationSystem.StartOperation(OperationSystem.GlobalSchedulerName, operation);
return operation;
}
@@ -123,7 +123,8 @@ namespace YooAsset
/// </summary>
public RequestPackageVersionOperation RequestPackageVersionAsync()
{
var options = new RequestPackageVersionOptions(true, 60);
int defaultTimeout = 60;
var options = new RequestPackageVersionOptions(true, defaultTimeout);
return RequestPackageVersionAsync(options);
}
@@ -132,7 +133,7 @@ namespace YooAsset
/// </summary>
public RequestPackageVersionOperation RequestPackageVersionAsync(RequestPackageVersionOptions options)
{
DebugCheckInitialize(false);
EnsureInitialized(false);
var operation = new RequestPackageVersionOperation(_fileSystemHost, options);
OperationSystem.StartOperation(PackageName, operation);
return operation;
@@ -143,12 +144,12 @@ namespace YooAsset
/// </summary>
public LoadPackageManifestOperation LoadPackageManifestAsync(LoadPackageManifestOptions options)
{
DebugCheckInitialize(false);
EnsureInitialized(false);
// 注意:强烈建议在更新之前保持加载器为空!
if (_resourceManager.HasAnyLoader())
{
YooLogger.Warning($"Found loaded bundle before update manifest ! Recommended to call the {nameof(UnloadAllAssetsAsync)} method to release loaded bundle !");
YooLogger.Warning($"Found loaded bundle before update manifest ! Recommended to call the {nameof(UnloadAllAssetsAsync)} method to release loaded bundle !");
}
var operation = new LoadPackageManifestOperation(_fileSystemHost, options);
@@ -161,7 +162,7 @@ namespace YooAsset
/// </summary>
public PreDownloadContentOperation PreDownloadContentAsync(PreDownloadContentOptions options)
{
DebugCheckInitialize(false);
EnsureInitialized(false);
var operation = new PreDownloadContentOperation(_fileSystemHost, options);
OperationSystem.StartOperation(PackageName, operation);
return operation;
@@ -172,7 +173,7 @@ namespace YooAsset
/// </summary>
public ClearCacheFilesOperation ClearCacheFilesAsync(ClearCacheFilesOptions options)
{
DebugCheckInitialize(false);
EnsureInitialized(false);
options.Manifest = _fileSystemHost.ActiveManifest;
var operation = new ClearCacheFilesOperation(_fileSystemHost, options);
OperationSystem.StartOperation(PackageName, operation);
@@ -186,7 +187,7 @@ namespace YooAsset
/// </summary>
public string GetPackageVersion()
{
DebugCheckInitialize();
EnsureInitialized();
return _fileSystemHost.ActiveManifest.PackageVersion;
}
@@ -195,7 +196,7 @@ namespace YooAsset
/// </summary>
public string GetPackageNote()
{
DebugCheckInitialize();
EnsureInitialized();
return _fileSystemHost.ActiveManifest.PackageNote;
}
@@ -204,7 +205,7 @@ namespace YooAsset
/// </summary>
public PackageDetails GetPackageDetails()
{
DebugCheckInitialize();
EnsureInitialized();
return _fileSystemHost.ActiveManifest.GetPackageDetails();
}
#endregion
@@ -225,7 +226,7 @@ namespace YooAsset
/// <param name="options">卸载选项</param>
public UnloadAllAssetsOperation UnloadAllAssetsAsync(UnloadAllAssetsOptions options)
{
DebugCheckInitialize();
EnsureInitialized();
var operation = new UnloadAllAssetsOperation(_resourceManager, options);
OperationSystem.StartOperation(PackageName, operation);
return operation;
@@ -238,7 +239,8 @@ namespace YooAsset
/// </summary>
public UnloadUnusedAssetsOperation UnloadUnusedAssetsAsync()
{
var options = new UnloadUnusedAssetsOptions(10);
int defaultLoopCount = 10;
var options = new UnloadUnusedAssetsOptions(defaultLoopCount);
return UnloadUnusedAssetsAsync(options);
}
@@ -248,7 +250,7 @@ namespace YooAsset
/// </summary>
public UnloadUnusedAssetsOperation UnloadUnusedAssetsAsync(UnloadUnusedAssetsOptions options)
{
DebugCheckInitialize();
EnsureInitialized();
var operation = new UnloadUnusedAssetsOperation(_resourceManager, options);
OperationSystem.StartOperation(PackageName, operation);
return operation;
@@ -260,7 +262,7 @@ namespace YooAsset
/// </summary>
public void TryUnloadUnusedAsset(string location, int loopCount = 10)
{
DebugCheckInitialize();
EnsureInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
_resourceManager.TryUnloadUnusedAsset(assetInfo, loopCount);
}
@@ -271,7 +273,7 @@ namespace YooAsset
/// </summary>
public void TryUnloadUnusedAsset(AssetInfo assetInfo, int loopCount = 10)
{
DebugCheckInitialize();
EnsureInitialized();
_resourceManager.TryUnloadUnusedAsset(assetInfo, loopCount);
}
#endregion
@@ -283,7 +285,7 @@ namespace YooAsset
/// <param name="location">资源的定位地址</param>
public bool IsNeedDownloadFromRemote(string location)
{
DebugCheckInitialize();
EnsureInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
return _fileSystemHost.IsNeedDownloadFromRemoteInternal(assetInfo);
}
@@ -294,7 +296,7 @@ namespace YooAsset
/// <param name="location">资源的定位地址</param>
public bool IsNeedDownloadFromRemote(AssetInfo assetInfo)
{
DebugCheckInitialize();
EnsureInitialized();
return _fileSystemHost.IsNeedDownloadFromRemoteInternal(assetInfo);
}
@@ -303,7 +305,7 @@ namespace YooAsset
/// </summary>
public AssetInfo[] GetAllAssetInfos()
{
DebugCheckInitialize();
EnsureInitialized();
return _fileSystemHost.ActiveManifest.GetAllAssetInfos();
}
@@ -313,7 +315,7 @@ namespace YooAsset
/// <param name="tag">资源标签</param>
public AssetInfo[] GetAssetInfos(string tag)
{
DebugCheckInitialize();
EnsureInitialized();
string[] tags = new string[] { tag };
return _fileSystemHost.ActiveManifest.GetAssetInfosByTags(tags);
}
@@ -324,7 +326,7 @@ namespace YooAsset
/// <param name="tags">资源标签列表</param>
public AssetInfo[] GetAssetInfos(string[] tags)
{
DebugCheckInitialize();
EnsureInitialized();
return _fileSystemHost.ActiveManifest.GetAssetInfosByTags(tags);
}
@@ -334,7 +336,7 @@ namespace YooAsset
/// <param name="location">资源的定位地址</param>
public AssetInfo GetAssetInfo(string location)
{
DebugCheckInitialize();
EnsureInitialized();
return ConvertLocationToAssetInfo(location, null);
}
@@ -345,7 +347,7 @@ namespace YooAsset
/// <param name="type">资源类型</param>
public AssetInfo GetAssetInfo(string location, System.Type type)
{
DebugCheckInitialize();
EnsureInitialized();
return ConvertLocationToAssetInfo(location, type);
}
@@ -355,7 +357,7 @@ namespace YooAsset
/// <param name="assetGUID">资源GUID</param>
public AssetInfo GetAssetInfoByGUID(string assetGUID)
{
DebugCheckInitialize();
EnsureInitialized();
return ConvertAssetGUIDToAssetInfo(assetGUID, null);
}
@@ -366,7 +368,7 @@ namespace YooAsset
/// <param name="type">资源类型</param>
public AssetInfo GetAssetInfoByGUID(string assetGUID, System.Type type)
{
DebugCheckInitialize();
EnsureInitialized();
return ConvertAssetGUIDToAssetInfo(assetGUID, type);
}
@@ -376,7 +378,7 @@ namespace YooAsset
/// <param name="location">资源的定位地址</param>
public bool CheckLocationValid(string location)
{
DebugCheckInitialize();
EnsureInitialized();
string assetPath = _fileSystemHost.ActiveManifest.TryMappingToAssetPath(location);
return string.IsNullOrEmpty(assetPath) == false;
}
@@ -389,7 +391,7 @@ namespace YooAsset
/// <param name="assetInfo">资源信息</param>
public RawFileHandle LoadRawFileSync(AssetInfo assetInfo)
{
DebugCheckInitialize();
EnsureInitialized();
return LoadRawFileInternal(assetInfo, true, 0);
}
@@ -399,7 +401,7 @@ namespace YooAsset
/// <param name="location">资源的定位地址</param>
public RawFileHandle LoadRawFileSync(string location)
{
DebugCheckInitialize();
EnsureInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
return LoadRawFileInternal(assetInfo, true, 0);
}
@@ -411,7 +413,7 @@ namespace YooAsset
/// <param name="priority">加载的优先级</param>
public RawFileHandle LoadRawFileAsync(AssetInfo assetInfo, uint priority = 0)
{
DebugCheckInitialize();
EnsureInitialized();
return LoadRawFileInternal(assetInfo, false, priority);
}
@@ -422,7 +424,7 @@ namespace YooAsset
/// <param name="priority">加载的优先级</param>
public RawFileHandle LoadRawFileAsync(string location, uint priority = 0)
{
DebugCheckInitialize();
EnsureInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
return LoadRawFileInternal(assetInfo, false, priority);
}
@@ -447,7 +449,7 @@ namespace YooAsset
/// <param name="physicsMode">场景物理模式</param>
public SceneHandle LoadSceneSync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, LocalPhysicsMode physicsMode = LocalPhysicsMode.None)
{
DebugCheckInitialize();
EnsureInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
return LoadSceneInternal(assetInfo, true, sceneMode, physicsMode, false, 0);
}
@@ -460,7 +462,7 @@ namespace YooAsset
/// <param name="physicsMode">场景物理模式</param>
public SceneHandle LoadSceneSync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, LocalPhysicsMode physicsMode = LocalPhysicsMode.None)
{
DebugCheckInitialize();
EnsureInitialized();
return LoadSceneInternal(assetInfo, true, sceneMode, physicsMode, false, 0);
}
@@ -474,7 +476,7 @@ namespace YooAsset
/// <param name="priority">加载的优先级</param>
public SceneHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, LocalPhysicsMode physicsMode = LocalPhysicsMode.None, bool suspendLoad = false, uint priority = 0)
{
DebugCheckInitialize();
EnsureInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
return LoadSceneInternal(assetInfo, false, sceneMode, physicsMode, suspendLoad, priority);
}
@@ -489,13 +491,13 @@ namespace YooAsset
/// <param name="priority">加载的优先级</param>
public SceneHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, LocalPhysicsMode physicsMode = LocalPhysicsMode.None, bool suspendLoad = false, uint priority = 0)
{
DebugCheckInitialize();
EnsureInitialized();
return LoadSceneInternal(assetInfo, false, sceneMode, physicsMode, suspendLoad, priority);
}
private SceneHandle LoadSceneInternal(AssetInfo assetInfo, bool waitForAsyncComplete, LoadSceneMode sceneMode, LocalPhysicsMode physicsMode, bool suspendLoad, uint priority)
{
DebugCheckAssetLoadType(assetInfo.AssetType);
DebugEnsureAssetType(assetInfo.AssetType);
assetInfo.LoadMethod = AssetInfo.ELoadMethod.LoadScene;
var loadSceneParams = new LoadSceneParameters(sceneMode, physicsMode);
var handle = _resourceManager.LoadSceneAsync(assetInfo, loadSceneParams, suspendLoad, priority);
@@ -505,14 +507,14 @@ namespace YooAsset
}
#endregion
#region 资源加载
#region 资源加载 [主资源]
/// <summary>
/// 同步加载资源对象
/// </summary>
/// <param name="assetInfo">资源信息</param>
public AssetHandle LoadAssetSync(AssetInfo assetInfo)
{
DebugCheckInitialize();
EnsureInitialized();
return LoadAssetInternal(assetInfo, true, 0);
}
@@ -523,7 +525,7 @@ namespace YooAsset
/// <param name="location">资源的定位地址</param>
public AssetHandle LoadAssetSync<TObject>(string location) where TObject : UnityEngine.Object
{
DebugCheckInitialize();
EnsureInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject));
return LoadAssetInternal(assetInfo, true, 0);
}
@@ -535,7 +537,7 @@ namespace YooAsset
/// <param name="type">资源类型</param>
public AssetHandle LoadAssetSync(string location, System.Type type)
{
DebugCheckInitialize();
EnsureInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
return LoadAssetInternal(assetInfo, true, 0);
}
@@ -546,9 +548,8 @@ namespace YooAsset
/// <param name="location">资源的定位地址</param>
public AssetHandle LoadAssetSync(string location)
{
DebugCheckInitialize();
Type type = typeof(UnityEngine.Object);
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
EnsureInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(UnityEngine.Object));
return LoadAssetInternal(assetInfo, true, 0);
}
@@ -560,7 +561,7 @@ namespace YooAsset
/// <param name="priority">加载的优先级</param>
public AssetHandle LoadAssetAsync(AssetInfo assetInfo, uint priority = 0)
{
DebugCheckInitialize();
EnsureInitialized();
return LoadAssetInternal(assetInfo, false, priority);
}
@@ -572,7 +573,7 @@ namespace YooAsset
/// <param name="priority">加载的优先级</param>
public AssetHandle LoadAssetAsync<TObject>(string location, uint priority = 0) where TObject : UnityEngine.Object
{
DebugCheckInitialize();
EnsureInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject));
return LoadAssetInternal(assetInfo, false, priority);
}
@@ -585,7 +586,7 @@ namespace YooAsset
/// <param name="priority">加载的优先级</param>
public AssetHandle LoadAssetAsync(string location, System.Type type, uint priority = 0)
{
DebugCheckInitialize();
EnsureInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
return LoadAssetInternal(assetInfo, false, priority);
}
@@ -597,16 +598,15 @@ namespace YooAsset
/// <param name="priority">加载的优先级</param>
public AssetHandle LoadAssetAsync(string location, uint priority = 0)
{
DebugCheckInitialize();
Type type = typeof(UnityEngine.Object);
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
EnsureInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(UnityEngine.Object));
return LoadAssetInternal(assetInfo, false, priority);
}
private AssetHandle LoadAssetInternal(AssetInfo assetInfo, bool waitForAsyncComplete, uint priority)
{
DebugCheckAssetLoadType(assetInfo.AssetType);
DebugEnsureAssetType(assetInfo.AssetType);
assetInfo.LoadMethod = AssetInfo.ELoadMethod.LoadAsset;
var handle = _resourceManager.LoadAssetAsync(assetInfo, priority);
if (waitForAsyncComplete)
@@ -615,14 +615,14 @@ namespace YooAsset
}
#endregion
#region 资源加载
#region 资源加载 [子资源]
/// <summary>
/// 同步加载子资源对象
/// </summary>
/// <param name="assetInfo">资源信息</param>
public SubAssetsHandle LoadSubAssetsSync(AssetInfo assetInfo)
{
DebugCheckInitialize();
EnsureInitialized();
return LoadSubAssetsInternal(assetInfo, true, 0);
}
@@ -633,7 +633,7 @@ namespace YooAsset
/// <param name="location">资源的定位地址</param>
public SubAssetsHandle LoadSubAssetsSync<TObject>(string location) where TObject : UnityEngine.Object
{
DebugCheckInitialize();
EnsureInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject));
return LoadSubAssetsInternal(assetInfo, true, 0);
}
@@ -645,7 +645,7 @@ namespace YooAsset
/// <param name="type">子对象类型</param>
public SubAssetsHandle LoadSubAssetsSync(string location, System.Type type)
{
DebugCheckInitialize();
EnsureInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
return LoadSubAssetsInternal(assetInfo, true, 0);
}
@@ -656,9 +656,8 @@ namespace YooAsset
/// <param name="location">资源的定位地址</param>
public SubAssetsHandle LoadSubAssetsSync(string location)
{
DebugCheckInitialize();
Type type = typeof(UnityEngine.Object);
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
EnsureInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(UnityEngine.Object));
return LoadSubAssetsInternal(assetInfo, true, 0);
}
@@ -670,7 +669,7 @@ namespace YooAsset
/// <param name="priority">加载的优先级</param>
public SubAssetsHandle LoadSubAssetsAsync(AssetInfo assetInfo, uint priority = 0)
{
DebugCheckInitialize();
EnsureInitialized();
return LoadSubAssetsInternal(assetInfo, false, priority);
}
@@ -682,7 +681,7 @@ namespace YooAsset
/// <param name="priority">加载的优先级</param>
public SubAssetsHandle LoadSubAssetsAsync<TObject>(string location, uint priority = 0) where TObject : UnityEngine.Object
{
DebugCheckInitialize();
EnsureInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject));
return LoadSubAssetsInternal(assetInfo, false, priority);
}
@@ -695,7 +694,7 @@ namespace YooAsset
/// <param name="priority">加载的优先级</param>
public SubAssetsHandle LoadSubAssetsAsync(string location, System.Type type, uint priority = 0)
{
DebugCheckInitialize();
EnsureInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
return LoadSubAssetsInternal(assetInfo, false, priority);
}
@@ -707,16 +706,15 @@ namespace YooAsset
/// <param name="priority">加载的优先级</param>
public SubAssetsHandle LoadSubAssetsAsync(string location, uint priority = 0)
{
DebugCheckInitialize();
Type type = typeof(UnityEngine.Object);
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
EnsureInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(UnityEngine.Object));
return LoadSubAssetsInternal(assetInfo, false, priority);
}
private SubAssetsHandle LoadSubAssetsInternal(AssetInfo assetInfo, bool waitForAsyncComplete, uint priority)
{
DebugCheckAssetLoadType(assetInfo.AssetType);
DebugEnsureAssetType(assetInfo.AssetType);
assetInfo.LoadMethod = AssetInfo.ELoadMethod.LoadSubAssets;
var handle = _resourceManager.LoadSubAssetsAsync(assetInfo, priority);
if (waitForAsyncComplete)
@@ -725,14 +723,14 @@ namespace YooAsset
}
#endregion
#region 资源加载
#region 资源加载 [全部资源]
/// <summary>
/// 同步加载资源包内所有资源对象
/// </summary>
/// <param name="assetInfo">资源信息</param>
public AllAssetsHandle LoadAllAssetsSync(AssetInfo assetInfo)
{
DebugCheckInitialize();
EnsureInitialized();
return LoadAllAssetsInternal(assetInfo, true, 0);
}
@@ -743,7 +741,7 @@ namespace YooAsset
/// <param name="location">资源的定位地址</param>
public AllAssetsHandle LoadAllAssetsSync<TObject>(string location) where TObject : UnityEngine.Object
{
DebugCheckInitialize();
EnsureInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject));
return LoadAllAssetsInternal(assetInfo, true, 0);
}
@@ -755,7 +753,7 @@ namespace YooAsset
/// <param name="type">子对象类型</param>
public AllAssetsHandle LoadAllAssetsSync(string location, System.Type type)
{
DebugCheckInitialize();
EnsureInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
return LoadAllAssetsInternal(assetInfo, true, 0);
}
@@ -766,9 +764,8 @@ namespace YooAsset
/// <param name="location">资源的定位地址</param>
public AllAssetsHandle LoadAllAssetsSync(string location)
{
DebugCheckInitialize();
Type type = typeof(UnityEngine.Object);
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
EnsureInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(UnityEngine.Object));
return LoadAllAssetsInternal(assetInfo, true, 0);
}
@@ -780,7 +777,7 @@ namespace YooAsset
/// <param name="priority">加载的优先级</param>
public AllAssetsHandle LoadAllAssetsAsync(AssetInfo assetInfo, uint priority = 0)
{
DebugCheckInitialize();
EnsureInitialized();
return LoadAllAssetsInternal(assetInfo, false, priority);
}
@@ -792,7 +789,7 @@ namespace YooAsset
/// <param name="priority">加载的优先级</param>
public AllAssetsHandle LoadAllAssetsAsync<TObject>(string location, uint priority = 0) where TObject : UnityEngine.Object
{
DebugCheckInitialize();
EnsureInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject));
return LoadAllAssetsInternal(assetInfo, false, priority);
}
@@ -805,7 +802,7 @@ namespace YooAsset
/// <param name="priority">加载的优先级</param>
public AllAssetsHandle LoadAllAssetsAsync(string location, System.Type type, uint priority = 0)
{
DebugCheckInitialize();
EnsureInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
return LoadAllAssetsInternal(assetInfo, false, priority);
}
@@ -817,16 +814,15 @@ namespace YooAsset
/// <param name="priority">加载的优先级</param>
public AllAssetsHandle LoadAllAssetsAsync(string location, uint priority = 0)
{
DebugCheckInitialize();
Type type = typeof(UnityEngine.Object);
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
EnsureInitialized();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(UnityEngine.Object));
return LoadAllAssetsInternal(assetInfo, false, priority);
}
private AllAssetsHandle LoadAllAssetsInternal(AssetInfo assetInfo, bool waitForAsyncComplete, uint priority)
{
DebugCheckAssetLoadType(assetInfo.AssetType);
DebugEnsureAssetType(assetInfo.AssetType);
assetInfo.LoadMethod = AssetInfo.ELoadMethod.LoadAllAssets;
var handle = _resourceManager.LoadAllAssetsAsync(assetInfo, priority);
if (waitForAsyncComplete)
@@ -841,16 +837,16 @@ namespace YooAsset
/// </summary>
public ResourceDownloaderOperation CreateResourceDownloader(ResourceDownloaderOptions options)
{
DebugCheckInitialize();
EnsureInitialized();
return _fileSystemHost.CreateResourceDownloader(options);
}
/// <summary>
/// 创建资源下载器,用于下载指定的资源信息列表依赖的资源包文件
/// </summary>
public ResourceDownloaderOperation CreateResourceDownloader(BundleDownloaderOptions options)
{
DebugCheckInitialize();
EnsureInitialized();
return _fileSystemHost.CreateResourceDownloader(options);
}
#endregion
@@ -861,7 +857,7 @@ namespace YooAsset
/// </summary>
public ResourceUnpackerOperation CreateResourceUnpacker(ResourceUnpackerOptions options)
{
DebugCheckInitialize();
EnsureInitialized();
return _fileSystemHost.CreateResourceUnpacker(options);
}
#endregion
@@ -872,7 +868,7 @@ namespace YooAsset
/// </summary>
public ResourceImporterOperation CreateResourceImporter(BundleImporterOptions options)
{
DebugCheckInitialize();
EnsureInitialized();
return _fileSystemHost.CreateResourceImporter(options);
}
#endregion
@@ -889,47 +885,43 @@ namespace YooAsset
#endregion
#region 调试方法
private void DebugCheckInitialize(bool checkActiveManifest = true)
private void EnsureInitialized(bool checkActiveManifest = true)
{
if (InitializeStatus != EOperationStatus.Succeed)
{
if (InitializeStatus == EOperationStatus.None)
{
throw new YooPackageException(PackageName, "Package not initialize !");
throw new YooPackageException(PackageName, "Resource package not initialized!");
}
else if (InitializeStatus == EOperationStatus.Processing)
{
throw new YooPackageException(PackageName, "Package initialize not completed !");
throw new YooPackageException(PackageName, "Resource package initialization not completed!");
}
else if (InitializeStatus == EOperationStatus.Failed)
{
string error = _initializeOp == null ? string.Empty : _initializeOp.Error;
throw new YooPackageException(PackageName, $"Package initialize failed ! {error}");
throw new YooPackageException(PackageName, $"Resource package initialization failed! Error : {error}");
}
}
if (checkActiveManifest)
{
if (_fileSystemHost.ActiveManifest == null)
throw new YooPackageException(PackageName, "Can not found active package manifest !");
throw new YooPackageException(PackageName, "Cannot found active package manifest!");
}
}
[Conditional("DEBUG")]
private void DebugCheckAssetLoadType(System.Type type)
private void DebugEnsureAssetType(System.Type type)
{
if (type == null)
return;
if (typeof(UnityEngine.Behaviour).IsAssignableFrom(type))
{
throw new YooLoadException($"Load asset type is invalid : {type.FullName} !");
}
throw new YooLoadException($"Load asset type is invalid : {type.FullName}");
if (typeof(UnityEngine.Object).IsAssignableFrom(type) == false)
{
throw new YooLoadException($"Load asset type is invalid : {type.FullName} !");
}
throw new YooLoadException($"Load asset type is invalid : {type.FullName}");
}
#endregion