From bd8ecab31b418fb50980a7c06789d0dcbcecf756 Mon Sep 17 00:00:00 2001 From: hevinci Date: Fri, 14 Oct 2022 11:49:32 +0800 Subject: [PATCH] Update runtime code --- .../{YooAssetPackage.cs => AssetsPackage.cs} | 13 +- ...tPackage.cs.meta => AssetsPackage.cs.meta} | 0 .../Runtime/Services/ILocationServices.cs | 2 +- .../AddressLocationServices.cs | 2 +- .../DefaultLocationServices.cs | 2 +- Assets/YooAsset/Runtime/YooAssets.cs | 22 +-- Assets/YooAsset/Runtime/YooAssetsExtension.cs | 174 ++++++++---------- 7 files changed, 94 insertions(+), 121 deletions(-) rename Assets/YooAsset/Runtime/{YooAssetPackage.cs => AssetsPackage.cs} (98%) rename Assets/YooAsset/Runtime/{YooAssetPackage.cs.meta => AssetsPackage.cs.meta} (100%) diff --git a/Assets/YooAsset/Runtime/YooAssetPackage.cs b/Assets/YooAsset/Runtime/AssetsPackage.cs similarity index 98% rename from Assets/YooAsset/Runtime/YooAssetPackage.cs rename to Assets/YooAsset/Runtime/AssetsPackage.cs index 1029666..ee96788 100644 --- a/Assets/YooAsset/Runtime/YooAssetPackage.cs +++ b/Assets/YooAsset/Runtime/AssetsPackage.cs @@ -6,7 +6,7 @@ using UnityEngine.SceneManagement; namespace YooAsset { - public class YooAssetPackage + public class AssetsPackage { private bool _isInitialize = false; private string _initializeError = string.Empty; @@ -33,13 +33,10 @@ namespace YooAsset } - /// - /// 拒绝外部实例化 - /// - internal YooAssetPackage() + internal AssetsPackage() { } - internal YooAssetPackage(string packageName) + internal AssetsPackage(string packageName) { PackageName = packageName; } @@ -136,10 +133,10 @@ namespace YooAsset private void CheckInitializeParameters(InitializeParameters parameters) { if (_isInitialize) - throw new Exception($"{nameof(YooAssetPackage)} is initialized yet."); + throw new Exception($"{nameof(AssetsPackage)} is initialized yet."); if (parameters == null) - throw new Exception($"{nameof(YooAssetPackage)} create parameters is null."); + throw new Exception($"{nameof(AssetsPackage)} create parameters is null."); #if !UNITY_EDITOR if (parameters is EditorSimulateModeParameters) diff --git a/Assets/YooAsset/Runtime/YooAssetPackage.cs.meta b/Assets/YooAsset/Runtime/AssetsPackage.cs.meta similarity index 100% rename from Assets/YooAsset/Runtime/YooAssetPackage.cs.meta rename to Assets/YooAsset/Runtime/AssetsPackage.cs.meta diff --git a/Assets/YooAsset/Runtime/Services/ILocationServices.cs b/Assets/YooAsset/Runtime/Services/ILocationServices.cs index 4d8c891..e646ed2 100644 --- a/Assets/YooAsset/Runtime/Services/ILocationServices.cs +++ b/Assets/YooAsset/Runtime/Services/ILocationServices.cs @@ -6,6 +6,6 @@ namespace YooAsset /// /// 定位地址转换为资源路径 /// - string ConvertLocationToAssetPath(YooAssetPackage package, string location); + string ConvertLocationToAssetPath(AssetsPackage package, string location); } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/Services/LocationServices/AddressLocationServices.cs b/Assets/YooAsset/Runtime/Services/LocationServices/AddressLocationServices.cs index f5f3020..158396c 100644 --- a/Assets/YooAsset/Runtime/Services/LocationServices/AddressLocationServices.cs +++ b/Assets/YooAsset/Runtime/Services/LocationServices/AddressLocationServices.cs @@ -3,7 +3,7 @@ namespace YooAsset { public class AddressLocationServices : ILocationServices { - string ILocationServices.ConvertLocationToAssetPath(YooAssetPackage package, string location) + string ILocationServices.ConvertLocationToAssetPath(AssetsPackage package, string location) { return package.MappingToAssetPath(location); } diff --git a/Assets/YooAsset/Runtime/Services/LocationServices/DefaultLocationServices.cs b/Assets/YooAsset/Runtime/Services/LocationServices/DefaultLocationServices.cs index 7d92c7d..4268a90 100644 --- a/Assets/YooAsset/Runtime/Services/LocationServices/DefaultLocationServices.cs +++ b/Assets/YooAsset/Runtime/Services/LocationServices/DefaultLocationServices.cs @@ -11,7 +11,7 @@ namespace YooAsset _resourceRoot = PathHelper.GetRegularPath(resourceRoot); } - string ILocationServices.ConvertLocationToAssetPath(YooAssetPackage package, string location) + string ILocationServices.ConvertLocationToAssetPath(AssetsPackage package, string location) { if (string.IsNullOrEmpty(_resourceRoot)) { diff --git a/Assets/YooAsset/Runtime/YooAssets.cs b/Assets/YooAsset/Runtime/YooAssets.cs index ab4c245..33189c0 100644 --- a/Assets/YooAsset/Runtime/YooAssets.cs +++ b/Assets/YooAsset/Runtime/YooAssets.cs @@ -9,7 +9,7 @@ namespace YooAsset public static partial class YooAssets { private static bool _isInitialize = false; - private static readonly List _packages = new List(); + private static readonly List _packages = new List(); /// /// 初始化资源系统 @@ -81,27 +81,27 @@ namespace YooAsset /// 创建资源包 /// /// 资源包名称 - public static YooAssetPackage CreateAssetPackage(string packageName) + public static AssetsPackage CreateAssetsPackage(string packageName) { if (_isInitialize == false) throw new Exception($"{nameof(YooAssets)} not initialize !"); if (string.IsNullOrEmpty(packageName)) - throw new Exception("PackageName is null or empty !"); + throw new Exception("Package name is null or empty !"); - if (HasAssetPackage(packageName)) + if (HasAssetsPackage(packageName)) throw new Exception($"Package {packageName} already existed !"); - YooAssetPackage component = new YooAssetPackage(packageName); - _packages.Add(component); - return component; + AssetsPackage assetsPackage = new AssetsPackage(packageName); + _packages.Add(assetsPackage); + return assetsPackage; } /// /// 获取资源包 /// /// 资源包名称 - public static YooAssetPackage GetAssetPackage(string packageName) + public static AssetsPackage GetAssetsPackage(string packageName) { if (_isInitialize == false) throw new Exception($"{nameof(YooAssets)} not initialize !"); @@ -115,7 +115,7 @@ namespace YooAsset return package; } - YooLogger.Warning($"Not found asset package : {packageName}"); + YooLogger.Warning($"Not found assets package : {packageName}"); return null; } @@ -123,7 +123,7 @@ namespace YooAsset /// 检测资源包是否存在 /// /// 资源包名称 - public static bool HasAssetPackage(string packageName) + public static bool HasAssetsPackage(string packageName) { if (_isInitialize == false) throw new Exception($"{nameof(YooAssets)} not initialize !"); @@ -135,7 +135,7 @@ namespace YooAsset } return false; } - + /// /// 开启一个异步操作 /// diff --git a/Assets/YooAsset/Runtime/YooAssetsExtension.cs b/Assets/YooAsset/Runtime/YooAssetsExtension.cs index b10d291..8b86987 100644 --- a/Assets/YooAsset/Runtime/YooAssetsExtension.cs +++ b/Assets/YooAsset/Runtime/YooAssetsExtension.cs @@ -8,80 +8,16 @@ namespace YooAsset { public static partial class YooAssets { - private static YooAssetPackage _mainPackage; + private static AssetsPackage _defaultPackage; /// - /// 初始化状态 + /// 设置默认的资源包 /// - public static EOperationStatus InitializeStatus + public static void SetDefaultAssetsPackage(AssetsPackage assetsPackage) { - get - { - if (_mainPackage == null) - return EOperationStatus.None; - return _mainPackage.InitializeStatus; - } + _defaultPackage = assetsPackage; } - - /// - /// 异步初始化 - /// - public static InitializationOperation InitializeAsync(InitializeParameters parameters, string packageName = "DefaultPackage") - { - if (_mainPackage != null) - throw new Exception("Main package is initialized yet."); - - _mainPackage = CreateAssetPackage(packageName); - return _mainPackage.InitializeAsync(parameters); - } - - /// - /// 向网络端请求静态资源版本 - /// - /// 超时时间(默认值:60秒) - public static UpdateStaticVersionOperation UpdateStaticVersionAsync(int timeout = 60) - { - return _mainPackage.UpdateStaticVersionAsync(timeout); - } - - /// - /// 向网络端请求并更新补丁清单 - /// - /// 更新的资源包裹版本 - /// 超时时间(默认值:60秒) - public static UpdateManifestOperation UpdateManifestAsync(string packageCRC, int timeout = 60) - { - return _mainPackage.UpdateManifestAsync(packageCRC, timeout); - } - - /// - /// 弱联网情况下加载补丁清单 - /// 注意:当指定版本内容验证失败后会返回失败。 - /// - /// 指定的资源包裹版本 - public static UpdateManifestOperation WeaklyUpdateManifestAsync(string packageCRC) - { - return _mainPackage.WeaklyUpdateManifestAsync(packageCRC); - } - - /// - /// 资源回收(卸载引用计数为零的资源) - /// - public static void UnloadUnusedAssets() - { - _mainPackage.UnloadUnusedAssets(); - } - - /// - /// 强制回收所有资源 - /// - public static void ForceUnloadAllAssets() - { - _mainPackage.ForceUnloadAllAssets(); - } - - #region 资源信息 /// /// 是否需要从远端更新下载 @@ -89,7 +25,8 @@ namespace YooAsset /// 资源的定位地址 public static bool IsNeedDownloadFromRemote(string location) { - return _mainPackage.IsNeedDownloadFromRemote(location); + DebugCheckDefaultPackageValid(); + return _defaultPackage.IsNeedDownloadFromRemote(location); } /// @@ -98,7 +35,8 @@ namespace YooAsset /// 资源的定位地址 public static bool IsNeedDownloadFromRemote(AssetInfo assetInfo) { - return _mainPackage.IsNeedDownloadFromRemote(assetInfo); + DebugCheckDefaultPackageValid(); + return _defaultPackage.IsNeedDownloadFromRemote(assetInfo); } /// @@ -107,7 +45,8 @@ namespace YooAsset /// 资源标签 public static AssetInfo[] GetAssetInfos(string tag) { - return _mainPackage.GetAssetInfos(tag); + DebugCheckDefaultPackageValid(); + return _defaultPackage.GetAssetInfos(tag); } /// @@ -116,7 +55,8 @@ namespace YooAsset /// 资源标签列表 public static AssetInfo[] GetAssetInfos(string[] tags) { - return _mainPackage.GetAssetInfos(tags); + DebugCheckDefaultPackageValid(); + return _defaultPackage.GetAssetInfos(tags); } /// @@ -125,7 +65,8 @@ namespace YooAsset /// 资源的定位地址 public static AssetInfo GetAssetInfo(string location) { - return _mainPackage.GetAssetInfo(location); + DebugCheckDefaultPackageValid(); + return _defaultPackage.GetAssetInfo(location); } /// @@ -135,7 +76,8 @@ namespace YooAsset /// 如果location地址无效,则返回空字符串 public static string GetAssetPath(string location) { - return _mainPackage.GetAssetPath(location); + DebugCheckDefaultPackageValid(); + return _defaultPackage.GetAssetPath(location); } #endregion @@ -147,7 +89,8 @@ namespace YooAsset /// 拷贝路径 public static RawFileOperation GetRawFileAsync(string location, string copyPath = null) { - return _mainPackage.GetRawFileAsync(location, copyPath); + DebugCheckDefaultPackageValid(); + return _defaultPackage.GetRawFileAsync(location, copyPath); } /// @@ -157,7 +100,8 @@ namespace YooAsset /// 拷贝路径 public static RawFileOperation GetRawFileAsync(AssetInfo assetInfo, string copyPath = null) { - return _mainPackage.GetRawFileAsync(assetInfo, copyPath); + DebugCheckDefaultPackageValid(); + return _defaultPackage.GetRawFileAsync(assetInfo, copyPath); } #endregion @@ -171,7 +115,8 @@ namespace YooAsset /// 优先级 public static SceneOperationHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool activateOnLoad = true, int priority = 100) { - return _mainPackage.LoadSceneAsync(location, sceneMode, activateOnLoad, priority); + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadSceneAsync(location, sceneMode, activateOnLoad, priority); } /// @@ -183,7 +128,8 @@ namespace YooAsset /// 优先级 public static SceneOperationHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool activateOnLoad = true, int priority = 100) { - return _mainPackage.LoadSceneAsync(assetInfo, sceneMode, activateOnLoad, priority); + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadSceneAsync(assetInfo, sceneMode, activateOnLoad, priority); } #endregion @@ -194,7 +140,8 @@ namespace YooAsset /// 资源信息 public static AssetOperationHandle LoadAssetSync(AssetInfo assetInfo) { - return _mainPackage.LoadAssetSync(assetInfo); + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadAssetSync(assetInfo); } /// @@ -204,7 +151,8 @@ namespace YooAsset /// 资源的定位地址 public static AssetOperationHandle LoadAssetSync(string location) where TObject : UnityEngine.Object { - return _mainPackage.LoadAssetSync(location); + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadAssetSync(location); } /// @@ -214,7 +162,8 @@ namespace YooAsset /// 资源类型 public static AssetOperationHandle LoadAssetSync(string location, System.Type type) { - return _mainPackage.LoadAssetSync(location, type); + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadAssetSync(location, type); } @@ -224,7 +173,8 @@ namespace YooAsset /// 资源信息 public static AssetOperationHandle LoadAssetAsync(AssetInfo assetInfo) { - return _mainPackage.LoadAssetAsync(assetInfo); + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadAssetAsync(assetInfo); } /// @@ -234,7 +184,8 @@ namespace YooAsset /// 资源的定位地址 public static AssetOperationHandle LoadAssetAsync(string location) where TObject : UnityEngine.Object { - return _mainPackage.LoadAssetAsync(location); + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadAssetAsync(location); } /// @@ -244,7 +195,8 @@ namespace YooAsset /// 资源类型 public static AssetOperationHandle LoadAssetAsync(string location, System.Type type) { - return _mainPackage.LoadAssetAsync(location, type); + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadAssetAsync(location, type); } #endregion @@ -255,7 +207,8 @@ namespace YooAsset /// 资源信息 public static SubAssetsOperationHandle LoadSubAssetsSync(AssetInfo assetInfo) { - return _mainPackage.LoadSubAssetsSync(assetInfo); + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadSubAssetsSync(assetInfo); } /// @@ -265,7 +218,8 @@ namespace YooAsset /// 资源的定位地址 public static SubAssetsOperationHandle LoadSubAssetsSync(string location) where TObject : UnityEngine.Object { - return _mainPackage.LoadSubAssetsSync(location); + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadSubAssetsSync(location); } /// @@ -275,7 +229,8 @@ namespace YooAsset /// 子对象类型 public static SubAssetsOperationHandle LoadSubAssetsSync(string location, System.Type type) { - return _mainPackage.LoadSubAssetsSync(location, type); + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadSubAssetsSync(location, type); } @@ -285,7 +240,8 @@ namespace YooAsset /// 资源信息 public static SubAssetsOperationHandle LoadSubAssetsAsync(AssetInfo assetInfo) { - return _mainPackage.LoadSubAssetsAsync(assetInfo); + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadSubAssetsAsync(assetInfo); } /// @@ -295,7 +251,8 @@ namespace YooAsset /// 资源的定位地址 public static SubAssetsOperationHandle LoadSubAssetsAsync(string location) where TObject : UnityEngine.Object { - return _mainPackage.LoadSubAssetsAsync(location); + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadSubAssetsAsync(location); } /// @@ -305,7 +262,8 @@ namespace YooAsset /// 子对象类型 public static SubAssetsOperationHandle LoadSubAssetsAsync(string location, System.Type type) { - return _mainPackage.LoadSubAssetsAsync(location, type); + DebugCheckDefaultPackageValid(); + return _defaultPackage.LoadSubAssetsAsync(location, type); } #endregion @@ -318,7 +276,8 @@ namespace YooAsset /// 下载失败的重试次数 public static PatchDownloaderOperation CreatePatchDownloader(string tag, int downloadingMaxNumber, int failedTryAgain) { - return _mainPackage.CreatePatchDownloader(new string[] { tag }, downloadingMaxNumber, failedTryAgain); + DebugCheckDefaultPackageValid(); + return _defaultPackage.CreatePatchDownloader(new string[] { tag }, downloadingMaxNumber, failedTryAgain); } /// @@ -329,7 +288,8 @@ namespace YooAsset /// 下载失败的重试次数 public static PatchDownloaderOperation CreatePatchDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain) { - return _mainPackage.CreatePatchDownloader(tags, downloadingMaxNumber, failedTryAgain); + DebugCheckDefaultPackageValid(); + return _defaultPackage.CreatePatchDownloader(tags, downloadingMaxNumber, failedTryAgain); } /// @@ -339,7 +299,8 @@ namespace YooAsset /// 下载失败的重试次数 public static PatchDownloaderOperation CreatePatchDownloader(int downloadingMaxNumber, int failedTryAgain) { - return _mainPackage.CreatePatchDownloader(downloadingMaxNumber, failedTryAgain); + DebugCheckDefaultPackageValid(); + return _defaultPackage.CreatePatchDownloader(downloadingMaxNumber, failedTryAgain); } @@ -351,7 +312,8 @@ namespace YooAsset /// 下载失败的重试次数 public static PatchDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain) { - return _mainPackage.CreateBundleDownloader(locations, downloadingMaxNumber, failedTryAgain); + DebugCheckDefaultPackageValid(); + return _defaultPackage.CreateBundleDownloader(locations, downloadingMaxNumber, failedTryAgain); } /// @@ -362,7 +324,8 @@ namespace YooAsset /// 下载失败的重试次数 public static PatchDownloaderOperation CreateBundleDownloader(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain) { - return _mainPackage.CreateBundleDownloader(assetInfos, downloadingMaxNumber, failedTryAgain); + DebugCheckDefaultPackageValid(); + return _defaultPackage.CreateBundleDownloader(assetInfos, downloadingMaxNumber, failedTryAgain); } #endregion @@ -375,7 +338,8 @@ namespace YooAsset /// 解压失败的重试次数 public static PatchUnpackerOperation CreatePatchUnpacker(string tag, int unpackingMaxNumber, int failedTryAgain) { - return _mainPackage.CreatePatchUnpacker(tag, unpackingMaxNumber, failedTryAgain); + DebugCheckDefaultPackageValid(); + return _defaultPackage.CreatePatchUnpacker(tag, unpackingMaxNumber, failedTryAgain); } /// @@ -386,7 +350,8 @@ namespace YooAsset /// 解压失败的重试次数 public static PatchUnpackerOperation CreatePatchUnpacker(string[] tags, int unpackingMaxNumber, int failedTryAgain) { - return _mainPackage.CreatePatchUnpacker(tags, unpackingMaxNumber, failedTryAgain); + DebugCheckDefaultPackageValid(); + return _defaultPackage.CreatePatchUnpacker(tags, unpackingMaxNumber, failedTryAgain); } /// @@ -396,7 +361,8 @@ namespace YooAsset /// 解压失败的重试次数 public static PatchUnpackerOperation CreatePatchUnpacker(int unpackingMaxNumber, int failedTryAgain) { - return _mainPackage.CreatePatchUnpacker(unpackingMaxNumber, failedTryAgain); + DebugCheckDefaultPackageValid(); + return _defaultPackage.CreatePatchUnpacker(unpackingMaxNumber, failedTryAgain); } #endregion @@ -408,7 +374,17 @@ namespace YooAsset /// 超时时间 public static UpdatePackageOperation UpdatePackageAsync(string packageCRC, int timeout = 60) { - return _mainPackage.UpdatePackageAsync(packageCRC, timeout); + DebugCheckDefaultPackageValid(); + return _defaultPackage.UpdatePackageAsync(packageCRC, timeout); + } + #endregion + + #region 调试方法 + [Conditional("DEBUG")] + private static void DebugCheckDefaultPackageValid() + { + if (_defaultPackage == null) + throw new Exception($"Default package is null. Please use {nameof(YooAssets.SetDefaultAssetsPackage)} !"); } #endregion }