diff --git a/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs b/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs index e06a0c0..730f6a6 100644 --- a/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs +++ b/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs @@ -9,7 +9,7 @@ namespace YooAsset private static readonly List _operations = new List(100); // 计时器相关 - private static Stopwatch _watch; + private static Stopwatch _watch; private static long _maxTimeSlice; private static long _frameTime; @@ -35,11 +35,12 @@ namespace YooAsset if (_watch.ElapsedMilliseconds - _frameTime >= _maxTimeSlice) return; - _operations[i].Update(); - if (_operations[i].IsDone) + var operation = _operations[i]; + operation.Update(); + if (operation.IsDone) { - _operations[i].Finish(); _operations.RemoveAt(i); + operation.Finish(); } } } diff --git a/Assets/YooAsset/Runtime/PatchSystem/BundleInfo.cs b/Assets/YooAsset/Runtime/PatchSystem/BundleInfo.cs index b84eeb1..3a5f26b 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/BundleInfo.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/BundleInfo.cs @@ -36,7 +36,7 @@ namespace YooAsset /// /// 编辑器资源路径 /// - public string EditorAssetPath { private set; get; } + internal string EditorAssetPath { private set; get; } /// /// 文件哈希值 diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs index 4fb853c..212cd0e 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs @@ -104,7 +104,6 @@ namespace YooAsset _steps = ESteps.Done; Status = EOperationStatus.Failed; Error = _appManifestLoader.Error; - throw new System.Exception($"FATAL : {_appManifestLoader.Error}"); } else { @@ -178,7 +177,6 @@ namespace YooAsset _steps = ESteps.Done; Status = EOperationStatus.Failed; Error = _appManifestLoader.Error; - throw new System.Exception($"FATAL : {_appManifestLoader.Error}"); } else { diff --git a/Assets/YooAsset/Runtime/YooAssets.cs b/Assets/YooAsset/Runtime/YooAssets.cs index 0ed5163..38154c9 100644 --- a/Assets/YooAsset/Runtime/YooAssets.cs +++ b/Assets/YooAsset/Runtime/YooAssets.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.Collections; using System.Collections.Generic; using UnityEngine.SceneManagement; @@ -96,6 +97,8 @@ namespace YooAsset private static bool _isInitialize = false; + private static string _initializeError = string.Empty; + private static EOperationStatus _initializeStatus = EOperationStatus.None; private static EPlayMode _playMode; private static IBundleServices _bundleServices; private static ILocationServices _locationServices; @@ -141,9 +144,9 @@ namespace YooAsset YooLogger.Warning($"{nameof(parameters.AssetLoadingMaxNumber)} minimum value is 1"); } - if (parameters.OperationSystemMaxTimeSlice < 33) + if (parameters.OperationSystemMaxTimeSlice < 30) { - parameters.OperationSystemMaxTimeSlice = 33; + parameters.OperationSystemMaxTimeSlice = 30; YooLogger.Warning($"{nameof(parameters.OperationSystemMaxTimeSlice)} minimum value is 33 milliseconds"); } @@ -172,19 +175,20 @@ namespace YooAsset } // 初始化资源系统 + InitializationOperation initializeOperation; if (_playMode == EPlayMode.EditorPlayMode) { _editorPlayModeImpl = new EditorPlayModeImpl(); _bundleServices = _editorPlayModeImpl; AssetSystem.Initialize(true, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices); - return _editorPlayModeImpl.InitializeAsync(); + initializeOperation = _editorPlayModeImpl.InitializeAsync(); } else if (_playMode == EPlayMode.OfflinePlayMode) { _offlinePlayModeImpl = new OfflinePlayModeImpl(); _bundleServices = _offlinePlayModeImpl; AssetSystem.Initialize(false, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices); - return _offlinePlayModeImpl.InitializeAsync(); + initializeOperation = _offlinePlayModeImpl.InitializeAsync(); } else if (_playMode == EPlayMode.HostPlayMode) { @@ -192,7 +196,7 @@ namespace YooAsset _bundleServices = _hostPlayModeImpl; AssetSystem.Initialize(false, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices); var hostPlayModeParameters = parameters as HostPlayModeParameters; - return _hostPlayModeImpl.InitializeAsync( + initializeOperation = _hostPlayModeImpl.InitializeAsync( hostPlayModeParameters.ClearCacheWhenDirty, hostPlayModeParameters.DefaultHostServer, hostPlayModeParameters.FallbackHostServer); @@ -201,6 +205,15 @@ namespace YooAsset { throw new NotImplementedException(); } + + // 监听初始化结果 + initializeOperation.Completed += InitializeOperation_Completed; + return initializeOperation; + } + private static void InitializeOperation_Completed(AsyncOperationBase op) + { + _initializeStatus = op.Status; + _initializeError = op.Error; } /// @@ -209,6 +222,7 @@ namespace YooAsset /// 超时时间(默认值:60秒) public static UpdateStaticVersionOperation UpdateStaticVersionAsync(int timeout = 60) { + DebugCheckInitialize(); if (_playMode == EPlayMode.EditorPlayMode) { var operation = new EditorPlayModeUpdateStaticVersionOperation(); @@ -240,6 +254,7 @@ namespace YooAsset /// 超时时间(默认值:60秒) public static UpdateManifestOperation UpdateManifestAsync(int resourceVersion, int timeout = 60) { + DebugCheckInitialize(); if (_playMode == EPlayMode.EditorPlayMode) { var operation = new EditorPlayModeUpdateManifestOperation(); @@ -269,6 +284,7 @@ namespace YooAsset /// public static int GetResourceVersion() { + DebugCheckInitialize(); if (_playMode == EPlayMode.EditorPlayMode) { if (_editorPlayModeImpl == null) @@ -316,6 +332,7 @@ namespace YooAsset /// 资源的定位地址 public static BundleInfo GetBundleInfo(string location) { + DebugCheckInitialize(); string assetPath = _locationServices.ConvertLocationToAssetPath(location); string bundleName = _bundleServices.GetBundleName(assetPath); return _bundleServices.GetBundleInfo(bundleName); @@ -327,6 +344,7 @@ namespace YooAsset /// 资源信息 public static BundleInfo GetBundleInfo(AssetInfo assetInfo) { + DebugCheckInitialize(); string bundleName = _bundleServices.GetBundleName(assetInfo.AssetPath); return _bundleServices.GetBundleInfo(bundleName); } @@ -338,6 +356,7 @@ namespace YooAsset /// public static AssetInfo[] GetAssetInfos(string tag) { + DebugCheckInitialize(); string[] tags = new string[] { tag }; return _bundleServices.GetAssetInfos(tags); } @@ -363,6 +382,7 @@ namespace YooAsset /// 优先级 public static SceneOperationHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool activateOnLoad = true, int priority = 100) { + DebugCheckInitialize(); string scenePath = _locationServices.ConvertLocationToAssetPath(location); var handle = AssetSystem.LoadSceneAsync(scenePath, sceneMode, activateOnLoad, priority); return handle; @@ -377,6 +397,7 @@ namespace YooAsset /// 优先级 public static SceneOperationHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool activateOnLoad = true, int priority = 100) { + DebugCheckInitialize(); string scenePath = assetInfo.AssetPath; var handle = AssetSystem.LoadSceneAsync(scenePath, sceneMode, activateOnLoad, priority); return handle; @@ -391,6 +412,7 @@ namespace YooAsset /// 拷贝路径 public static RawFileOperation GetRawFileAsync(string location, string copyPath = null) { + DebugCheckInitialize(); string assetPath = _locationServices.ConvertLocationToAssetPath(location); return GetRawFileInternal(assetPath, copyPath); } @@ -402,6 +424,7 @@ namespace YooAsset /// 拷贝路径 public static RawFileOperation GetRawFileAsync(AssetInfo assetInfo, string copyPath = null) { + DebugCheckInitialize(); return GetRawFileInternal(assetInfo.AssetPath, copyPath); } @@ -412,6 +435,7 @@ namespace YooAsset /// 资源信息 public static AssetOperationHandle LoadAssetSync(AssetInfo assetInfo) { + DebugCheckInitialize(); return LoadAssetInternal(assetInfo.AssetPath, assetInfo.AssetType, true); } @@ -422,6 +446,7 @@ namespace YooAsset /// 资源的定位地址 public static AssetOperationHandle LoadAssetSync(string location) where TObject : class { + DebugCheckInitialize(); string assetPath = _locationServices.ConvertLocationToAssetPath(location); return LoadAssetInternal(assetPath, typeof(TObject), true); } @@ -433,6 +458,7 @@ namespace YooAsset /// 资源类型 public static AssetOperationHandle LoadAssetSync(string location, System.Type type) { + DebugCheckInitialize(); string assetPath = _locationServices.ConvertLocationToAssetPath(location); return LoadAssetInternal(assetPath, type, true); } @@ -444,6 +470,7 @@ namespace YooAsset /// 资源信息 public static SubAssetsOperationHandle LoadSubAssetsSync(AssetInfo assetInfo) { + DebugCheckInitialize(); return LoadSubAssetsInternal(assetInfo.AssetPath, assetInfo.AssetType, true); } @@ -454,6 +481,7 @@ namespace YooAsset /// 资源的定位地址 public static SubAssetsOperationHandle LoadSubAssetsSync(string location) { + DebugCheckInitialize(); string assetPath = _locationServices.ConvertLocationToAssetPath(location); return LoadSubAssetsInternal(assetPath, typeof(TObject), true); } @@ -465,6 +493,7 @@ namespace YooAsset /// 子对象类型 public static SubAssetsOperationHandle LoadSubAssetsSync(string location, System.Type type) { + DebugCheckInitialize(); string assetPath = _locationServices.ConvertLocationToAssetPath(location); return LoadSubAssetsInternal(assetPath, type, true); } @@ -476,6 +505,7 @@ namespace YooAsset /// 资源信息 public static AssetOperationHandle LoadAssetAsync(AssetInfo assetInfo) { + DebugCheckInitialize(); return LoadAssetInternal(assetInfo.AssetPath, assetInfo.AssetType, false); } @@ -486,6 +516,7 @@ namespace YooAsset /// 资源的定位地址 public static AssetOperationHandle LoadAssetAsync(string location) { + DebugCheckInitialize(); string assetPath = _locationServices.ConvertLocationToAssetPath(location); return LoadAssetInternal(assetPath, typeof(TObject), false); } @@ -497,6 +528,7 @@ namespace YooAsset /// 资源类型 public static AssetOperationHandle LoadAssetAsync(string location, System.Type type) { + DebugCheckInitialize(); string assetPath = _locationServices.ConvertLocationToAssetPath(location); return LoadAssetInternal(assetPath, type, false); } @@ -508,6 +540,7 @@ namespace YooAsset /// 资源信息 public static SubAssetsOperationHandle LoadSubAssetsAsync(AssetInfo assetInfo) { + DebugCheckInitialize(); return LoadSubAssetsInternal(assetInfo.AssetPath, assetInfo.AssetType, false); } @@ -518,6 +551,7 @@ namespace YooAsset /// 资源的定位地址 public static SubAssetsOperationHandle LoadSubAssetsAsync(string location) { + DebugCheckInitialize(); string assetPath = _locationServices.ConvertLocationToAssetPath(location); return LoadSubAssetsInternal(assetPath, typeof(TObject), false); } @@ -529,6 +563,7 @@ namespace YooAsset /// 子对象类型 public static SubAssetsOperationHandle LoadSubAssetsAsync(string location, System.Type type) { + DebugCheckInitialize(); string assetPath = _locationServices.ConvertLocationToAssetPath(location); return LoadSubAssetsInternal(assetPath, type, false); } @@ -587,6 +622,7 @@ namespace YooAsset /// 下载失败的重试次数 public static PatchDownloaderOperation CreatePatchDownloader(string tag, int downloadingMaxNumber, int failedTryAgain) { + DebugCheckInitialize(); return CreatePatchDownloader(new string[] { tag }, downloadingMaxNumber, failedTryAgain); } @@ -598,6 +634,7 @@ namespace YooAsset /// 下载失败的重试次数 public static PatchDownloaderOperation CreatePatchDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain) { + DebugCheckInitialize(); if (_playMode == EPlayMode.EditorPlayMode || _playMode == EPlayMode.OfflinePlayMode) { List downloadList = new List(); @@ -623,6 +660,7 @@ namespace YooAsset /// 下载失败的重试次数 public static PatchDownloaderOperation CreatePatchDownloader(int downloadingMaxNumber, int failedTryAgain) { + DebugCheckInitialize(); if (_playMode == EPlayMode.EditorPlayMode || _playMode == EPlayMode.OfflinePlayMode) { List downloadList = new List(); @@ -650,6 +688,7 @@ namespace YooAsset /// 下载失败的重试次数 public static PatchDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain) { + DebugCheckInitialize(); if (_playMode == EPlayMode.EditorPlayMode || _playMode == EPlayMode.OfflinePlayMode) { List downloadList = new List(); @@ -683,6 +722,7 @@ namespace YooAsset /// 下载失败的重试次数 public static PatchDownloaderOperation CreateBundleDownloader(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain) { + DebugCheckInitialize(); if (_playMode == EPlayMode.EditorPlayMode || _playMode == EPlayMode.OfflinePlayMode) { List downloadList = new List(); @@ -717,6 +757,7 @@ namespace YooAsset /// 解压失败的重试次数 public static PatchUnpackerOperation CreatePatchUnpacker(string tag, int unpackingMaxNumber, int failedTryAgain) { + DebugCheckInitialize(); return CreatePatchUnpacker(new string[] { tag }, unpackingMaxNumber, failedTryAgain); } @@ -728,6 +769,7 @@ namespace YooAsset /// 解压失败的重试次数 public static PatchUnpackerOperation CreatePatchUnpacker(string[] tags, int unpackingMaxNumber, int failedTryAgain) { + DebugCheckInitialize(); if (_playMode == EPlayMode.EditorPlayMode) { List downloadList = new List(); @@ -761,6 +803,7 @@ namespace YooAsset /// 超时时间 public static UpdatePackageOperation UpdatePackageAsync(int resourceVersion, int timeout = 60) { + DebugCheckInitialize(); if (_playMode == EPlayMode.EditorPlayMode) { var operation = new EditorPlayModeUpdatePackageOperation(); @@ -835,15 +878,23 @@ namespace YooAsset } internal static string MappingToAssetPath(string location) { -#if UNITY_EDITOR - CheckLocation(location); -#endif - + DebugCheckLocation(location); return _bundleServices.MappingToAssetPath(location); } + #endregion -#if UNITY_EDITOR - private static void CheckLocation(string location) + #region 调试方法 + [Conditional("DEBUG")] + private static void DebugCheckInitialize() + { + if (_initializeStatus == EOperationStatus.None) + throw new Exception("YooAssets initialize not completed !"); + else if (_initializeStatus == EOperationStatus.Failed) + throw new Exception($"YooAssets initialize failed : {_initializeError}"); + } + + [Conditional("DEBUG")] + private static void DebugCheckLocation(string location) { if (string.IsNullOrEmpty(location)) { @@ -863,7 +914,6 @@ namespace YooAsset UnityEngine.Debug.LogWarning($"Found illegal character in location : \"{location}\""); } } -#endif #endregion } } \ No newline at end of file