diff --git a/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs b/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs index 9d0b965..aa4265d 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs @@ -6,24 +6,26 @@ using UnityEngine.SceneManagement; namespace YooAsset { - internal static class AssetSystem + internal class AssetSystemImpl { - private static readonly List _loaders = new List(1000); - private static readonly List _providers = new List(1000); - private static readonly Dictionary _sceneHandles = new Dictionary(100); - private static long _sceneCreateCount = 0; + private static string SceneRunningPackage = string.Empty; - private static bool _simulationOnEditor; - private static int _loadingMaxNumber; - public static IDecryptionServices DecryptionServices { private set; get; } - public static IBundleServices BundleServices { private set; get; } + private readonly List _loaders = new List(1000); + private readonly List _providers = new List(1000); + private readonly Dictionary _sceneHandles = new Dictionary(100); + + private long _sceneCreateCount = 0; + private bool _simulationOnEditor; + private int _loadingMaxNumber; + public IDecryptionServices DecryptionServices { private set; get; } + public IBundleServices BundleServices { private set; get; } /// /// 初始化 /// 注意:在使用AssetSystem之前需要初始化 /// - public static void Initialize(bool simulationOnEditor, int loadingMaxNumber, IDecryptionServices decryptionServices, IBundleServices bundleServices) + public void Initialize(bool simulationOnEditor, int loadingMaxNumber, IDecryptionServices decryptionServices, IBundleServices bundleServices) { _simulationOnEditor = simulationOnEditor; _loadingMaxNumber = loadingMaxNumber; @@ -34,7 +36,7 @@ namespace YooAsset /// /// 更新 /// - public static void Update() + public void Update() { // 更新加载器 foreach (var loader in _loaders) @@ -67,7 +69,7 @@ namespace YooAsset /// /// 销毁 /// - public static void DestroyAll() + public void DestroyAll() { _loaders.Clear(); _providers.Clear(); @@ -80,7 +82,7 @@ namespace YooAsset /// /// 资源回收(卸载引用计数为零的资源) /// - public static void UnloadUnusedAssets() + public void UnloadUnusedAssets() { if (_simulationOnEditor) { @@ -115,7 +117,7 @@ namespace YooAsset /// /// 强制回收所有资源 /// - public static void ForceUnloadAllAssets() + public void ForceUnloadAllAssets() { foreach (var provider in _providers) { @@ -136,7 +138,7 @@ namespace YooAsset /// /// 加载场景 /// - public static SceneOperationHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode, bool activateOnLoad, int priority) + public SceneOperationHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode, bool activateOnLoad, int priority) { if (assetInfo.IsInvalid) { @@ -145,6 +147,19 @@ namespace YooAsset return completedProvider.CreateHandle(); } + // 注意:场景只允许运行在一个资源包内 + if (string.IsNullOrEmpty(SceneRunningPackage)) + { + SceneRunningPackage = BundleServices.GetPackageName(); + } + if (BundleServices.GetPackageName() != SceneRunningPackage) + { + CompletedProvider completedProvider = new CompletedProvider(assetInfo); + string error = $"Scene are allowed to running within only {SceneRunningPackage}"; + completedProvider.SetCompleted(error); + return completedProvider.CreateHandle(); + } + // 如果加载的是主场景,则卸载所有缓存的场景 if (sceneMode == LoadSceneMode.Single) { @@ -156,9 +171,9 @@ namespace YooAsset ProviderBase provider; { if (_simulationOnEditor) - provider = new DatabaseSceneProvider(providerGUID, assetInfo, sceneMode, activateOnLoad, priority); + provider = new DatabaseSceneProvider(this, providerGUID, assetInfo, sceneMode, activateOnLoad, priority); else - provider = new BundledSceneProvider(providerGUID, assetInfo, sceneMode, activateOnLoad, priority); + provider = new BundledSceneProvider(this, providerGUID, assetInfo, sceneMode, activateOnLoad, priority); provider.InitSpawnDebugInfo(); _providers.Add(provider); } @@ -171,7 +186,7 @@ namespace YooAsset /// /// 加载资源对象 /// - public static AssetOperationHandle LoadAssetAsync(AssetInfo assetInfo) + public AssetOperationHandle LoadAssetAsync(AssetInfo assetInfo) { if (assetInfo.IsInvalid) { @@ -185,9 +200,9 @@ namespace YooAsset if (provider == null) { if (_simulationOnEditor) - provider = new DatabaseAssetProvider(providerGUID, assetInfo); + provider = new DatabaseAssetProvider(this, providerGUID, assetInfo); else - provider = new BundledAssetProvider(providerGUID, assetInfo); + provider = new BundledAssetProvider(this, providerGUID, assetInfo); provider.InitSpawnDebugInfo(); _providers.Add(provider); } @@ -197,7 +212,7 @@ namespace YooAsset /// /// 加载子资源对象 /// - public static SubAssetsOperationHandle LoadSubAssetsAsync(AssetInfo assetInfo) + public SubAssetsOperationHandle LoadSubAssetsAsync(AssetInfo assetInfo) { if (assetInfo.IsInvalid) { @@ -211,16 +226,16 @@ namespace YooAsset if (provider == null) { if (_simulationOnEditor) - provider = new DatabaseSubAssetsProvider(providerGUID, assetInfo); + provider = new DatabaseSubAssetsProvider(this, providerGUID, assetInfo); else - provider = new BundledSubAssetsProvider(providerGUID, assetInfo); + provider = new BundledSubAssetsProvider(this, providerGUID, assetInfo); provider.InitSpawnDebugInfo(); _providers.Add(provider); } return provider.CreateHandle(); } - internal static void UnloadSubScene(ProviderBase provider) + internal void UnloadSubScene(ProviderBase provider) { string providerGUID = provider.ProviderGUID; if (_sceneHandles.ContainsKey(providerGUID) == false) @@ -231,9 +246,9 @@ namespace YooAsset _sceneHandles.Remove(providerGUID); // 卸载未被使用的资源(包括场景) - AssetSystem.UnloadUnusedAssets(); + UnloadUnusedAssets(); } - internal static void UnloadAllScene() + internal void UnloadAllScene() { // 释放所有场景句柄 foreach (var valuePair in _sceneHandles) @@ -243,15 +258,15 @@ namespace YooAsset _sceneHandles.Clear(); // 卸载未被使用的资源(包括场景) - AssetSystem.UnloadUnusedAssets(); + UnloadUnusedAssets(); } - internal static AssetBundleLoaderBase CreateOwnerAssetBundleLoader(AssetInfo assetInfo) + internal AssetBundleLoaderBase CreateOwnerAssetBundleLoader(AssetInfo assetInfo) { BundleInfo bundleInfo = BundleServices.GetBundleInfo(assetInfo); return CreateAssetBundleLoaderInternal(bundleInfo); } - internal static List CreateDependAssetBundleLoaders(AssetInfo assetInfo) + internal List CreateDependAssetBundleLoaders(AssetInfo assetInfo) { BundleInfo[] depends = BundleServices.GetAllDependBundleInfos(assetInfo); List result = new List(depends.Length); @@ -262,7 +277,7 @@ namespace YooAsset } return result; } - internal static void RemoveBundleProviders(List providers) + internal void RemoveBundleProviders(List providers) { foreach (var provider in providers) { @@ -270,7 +285,7 @@ namespace YooAsset } } - private static AssetBundleLoaderBase CreateAssetBundleLoaderInternal(BundleInfo bundleInfo) + private AssetBundleLoaderBase CreateAssetBundleLoaderInternal(BundleInfo bundleInfo) { // 如果加载器已经存在 AssetBundleLoaderBase loader = TryGetAssetBundleLoader(bundleInfo.Bundle.BundleName); @@ -279,15 +294,15 @@ namespace YooAsset // 新增下载需求 #if UNITY_WEBGL - loader = new AssetBundleWebLoader(bundleInfo); + loader = new AssetBundleWebLoader(this, bundleInfo); #else - loader = new AssetBundleFileLoader(bundleInfo); + loader = new AssetBundleFileLoader(this, bundleInfo); #endif _loaders.Add(loader); return loader; } - private static AssetBundleLoaderBase TryGetAssetBundleLoader(string bundleName) + private AssetBundleLoaderBase TryGetAssetBundleLoader(string bundleName) { AssetBundleLoaderBase loader = null; for (int i = 0; i < _loaders.Count; i++) @@ -301,7 +316,7 @@ namespace YooAsset } return loader; } - private static ProviderBase TryGetProvider(string providerGUID) + private ProviderBase TryGetProvider(string providerGUID) { ProviderBase provider = null; for (int i = 0; i < _providers.Count; i++) @@ -316,37 +331,31 @@ namespace YooAsset return provider; } - #region 调试专属方法 - internal static DebugReport GetDebugReport() + #region 调试信息 + internal List GetDebugReportInfos() { - DebugReport report = new DebugReport(); - report.FrameCount = Time.frameCount; - report.BundleCount = _loaders.Count; - report.AssetCount = _providers.Count; - + List result = new List(_providers.Count); foreach (var provider in _providers) { DebugProviderInfo providerInfo = new DebugProviderInfo(); + providerInfo.PackageName = BundleServices.GetPackageName(); providerInfo.AssetPath = provider.MainAssetInfo.AssetPath; providerInfo.SpawnScene = provider.SpawnScene; providerInfo.SpawnTime = provider.SpawnTime; providerInfo.RefCount = provider.RefCount; providerInfo.Status = (int)provider.Status; - providerInfo.BundleInfos = new List(); - report.ProviderInfos.Add(providerInfo); + providerInfo.DependBundleInfos = new List(); + result.Add(providerInfo); if (provider is BundledProvider) { BundledProvider temp = provider as BundledProvider; - temp.GetBundleDebugInfos(providerInfo.BundleInfos); + temp.GetBundleDebugInfos(providerInfo.DependBundleInfos); } } - - // 重新排序 - report.ProviderInfos.Sort(); - return report; + return result; } - internal static List GetLoadedBundleInfos() + internal List GetLoadedBundleInfos() { List result = new List(100); foreach (var bundleLoader in _loaders) diff --git a/Assets/YooAsset/Runtime/AssetSystem/Handles/SceneOperationHandle.cs b/Assets/YooAsset/Runtime/AssetSystem/Handles/SceneOperationHandle.cs index 71fae54..d269c4d 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Handles/SceneOperationHandle.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Handles/SceneOperationHandle.cs @@ -118,7 +118,7 @@ namespace YooAsset // 卸载子场景 Scene sceneObject = SceneObject; - AssetSystem.UnloadSubScene(Provider); + Provider.Impl.UnloadSubScene(Provider); { var operation = new UnloadSceneOperation(sceneObject); OperationSystem.StartOperation(operation); diff --git a/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleFileLoader.cs b/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleFileLoader.cs index 5be5ec7..f79cf43 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleFileLoader.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleFileLoader.cs @@ -26,7 +26,7 @@ namespace YooAsset private AssetBundleCreateRequest _createRequest; - public AssetBundleFileLoader(BundleInfo bundleInfo) : base(bundleInfo) + public AssetBundleFileLoader(AssetSystemImpl impl, BundleInfo bundleInfo) : base(impl, bundleInfo) { } @@ -105,13 +105,13 @@ namespace YooAsset // Load assetBundle file if (MainBundleInfo.Bundle.IsEncrypted) { - if (AssetSystem.DecryptionServices == null) + if (Impl.DecryptionServices == null) throw new Exception($"{nameof(AssetBundleFileLoader)} need {nameof(IDecryptionServices)} : {MainBundleInfo.Bundle.BundleName}"); DecryptionFileInfo fileInfo = new DecryptionFileInfo(); fileInfo.BundleName = MainBundleInfo.Bundle.BundleName; fileInfo.FileHash = MainBundleInfo.Bundle.FileHash; - ulong offset = AssetSystem.DecryptionServices.GetFileOffset(fileInfo); + ulong offset = Impl.DecryptionServices.GetFileOffset(fileInfo); if (_isWaitForAsyncComplete) CacheBundle = AssetBundle.LoadFromFile(_fileLoadPath, 0, offset); else diff --git a/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleLoaderBase.cs b/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleLoaderBase.cs index 0d385da..125f836 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleLoaderBase.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleLoaderBase.cs @@ -14,6 +14,11 @@ namespace YooAsset Failed } + /// + /// 所属资源系统 + /// + public AssetSystemImpl Impl { private set; get; } + /// /// 资源包文件信息 /// @@ -43,8 +48,9 @@ namespace YooAsset internal AssetBundle CacheBundle { set; get; } - public AssetBundleLoaderBase(BundleInfo bundleInfo) + public AssetBundleLoaderBase(AssetSystemImpl impl, BundleInfo bundleInfo) { + Impl = impl; MainBundleInfo = bundleInfo; RefCount = 0; Status = EStatus.None; @@ -79,7 +85,7 @@ namespace YooAsset /// 轮询更新 /// public abstract void Update(); - + /// /// 销毁 /// @@ -148,7 +154,7 @@ namespace YooAsset } // 从列表里移除Providers - AssetSystem.RemoveBundleProviders(_providers); + Impl.RemoveBundleProviders(_providers); _providers.Clear(); } diff --git a/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleWebLoader.cs b/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleWebLoader.cs index 3bc2a22..d9f7110 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleWebLoader.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleWebLoader.cs @@ -31,7 +31,7 @@ namespace YooAsset private AssetBundleCreateRequest _createRequest; - public AssetBundleWebLoader(BundleInfo bundleInfo) : base(bundleInfo) + public AssetBundleWebLoader(AssetSystemImpl impl, BundleInfo bundleInfo) : base(impl, bundleInfo) { } @@ -110,13 +110,13 @@ namespace YooAsset // Load assetBundle file if (MainBundleInfo.Bundle.IsEncrypted) { - if (AssetSystem.DecryptionServices == null) + if (Impl.DecryptionServices == null) throw new Exception($"{nameof(AssetBundleFileLoader)} need {nameof(IDecryptionServices)} : {MainBundleInfo.Bundle.BundleName}"); DecryptionFileInfo fileInfo = new DecryptionFileInfo(); fileInfo.BundleName = MainBundleInfo.Bundle.BundleName; fileInfo.FileHash = MainBundleInfo.Bundle.FileHash; - ulong offset = AssetSystem.DecryptionServices.GetFileOffset(fileInfo); + ulong offset = Impl.DecryptionServices.GetFileOffset(fileInfo); _createRequest = AssetBundle.LoadFromFileAsync(_fileLoadPath, 0, offset); } else diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledAssetProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledAssetProvider.cs index fa6f34a..814b30c 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledAssetProvider.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledAssetProvider.cs @@ -17,7 +17,7 @@ namespace YooAsset } } - public BundledAssetProvider(string providerGUID, AssetInfo assetInfo) : base(providerGUID, assetInfo) + public BundledAssetProvider(AssetSystemImpl impl, string providerGUID, AssetInfo assetInfo) : base(impl, providerGUID, assetInfo) { } public override void Update() diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledProvider.cs index 576a746..e100a34 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledProvider.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledProvider.cs @@ -8,13 +8,13 @@ namespace YooAsset protected AssetBundleLoaderBase OwnerBundle { private set; get; } protected DependAssetBundleGroup DependBundleGroup { private set; get; } - public BundledProvider(string providerGUID, AssetInfo assetInfo) : base(providerGUID, assetInfo) + public BundledProvider(AssetSystemImpl impl, string providerGUID, AssetInfo assetInfo) : base(impl, providerGUID, assetInfo) { - OwnerBundle = AssetSystem.CreateOwnerAssetBundleLoader(assetInfo); + OwnerBundle = impl.CreateOwnerAssetBundleLoader(assetInfo); OwnerBundle.Reference(); OwnerBundle.AddProvider(this); - var dependBundles = AssetSystem.CreateDependAssetBundleLoaders(assetInfo); + var dependBundles = impl.CreateDependAssetBundleLoaders(assetInfo); DependBundleGroup = new DependAssetBundleGroup(dependBundles); DependBundleGroup.Reference(); } diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledSceneProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledSceneProvider.cs index 191e030..5a95701 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledSceneProvider.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledSceneProvider.cs @@ -23,7 +23,7 @@ namespace YooAsset } } - public BundledSceneProvider(string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool activateOnLoad, int priority) : base(providerGUID, assetInfo) + public BundledSceneProvider(AssetSystemImpl impl, string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool activateOnLoad, int priority) : base(impl, providerGUID, assetInfo) { SceneMode = sceneMode; _sceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath); diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledSubAssetsProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledSubAssetsProvider.cs index 8181fbb..4f4e337 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledSubAssetsProvider.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledSubAssetsProvider.cs @@ -17,7 +17,7 @@ namespace YooAsset } } - public BundledSubAssetsProvider(string providerGUID, AssetInfo assetInfo) : base(providerGUID, assetInfo) + public BundledSubAssetsProvider(AssetSystemImpl impl, string providerGUID, AssetInfo assetInfo) : base(impl, providerGUID, assetInfo) { } public override void Update() diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/CompletedProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/CompletedProvider.cs index dc51312..6f6692a 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/CompletedProvider.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/CompletedProvider.cs @@ -14,7 +14,7 @@ namespace YooAsset } } - public CompletedProvider(AssetInfo assetInfo) : base(string.Empty, assetInfo) + public CompletedProvider(AssetInfo assetInfo) : base(null, string.Empty, assetInfo) { } public override void Update() diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseAssetProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseAssetProvider.cs index b99694e..9ec2f2c 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseAssetProvider.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseAssetProvider.cs @@ -17,7 +17,7 @@ namespace YooAsset } } - public DatabaseAssetProvider(string providerGUID, AssetInfo assetInfo) : base(providerGUID, assetInfo) + public DatabaseAssetProvider(AssetSystemImpl impl, string providerGUID, AssetInfo assetInfo) : base(impl, providerGUID, assetInfo) { } public override void Update() diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSceneProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSceneProvider.cs index 7f80d2f..02d03c3 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSceneProvider.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSceneProvider.cs @@ -19,7 +19,7 @@ namespace YooAsset } } - public DatabaseSceneProvider(string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool activateOnLoad, int priority) : base(providerGUID, assetInfo) + public DatabaseSceneProvider(AssetSystemImpl impl, string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool activateOnLoad, int priority) : base(impl, providerGUID, assetInfo) { SceneMode = sceneMode; _activateOnLoad = activateOnLoad; diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSubAssetsProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSubAssetsProvider.cs index d87f1f6..52e1536 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSubAssetsProvider.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSubAssetsProvider.cs @@ -17,7 +17,7 @@ namespace YooAsset } } - public DatabaseSubAssetsProvider(string providerGUID, AssetInfo assetInfo) : base(providerGUID, assetInfo) + public DatabaseSubAssetsProvider(AssetSystemImpl impl, string providerGUID, AssetInfo assetInfo) : base(impl, providerGUID, assetInfo) { } public override void Update() diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/ProviderBase.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/ProviderBase.cs index be09c41..c0a8885 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/ProviderBase.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/ProviderBase.cs @@ -22,6 +22,11 @@ namespace YooAsset /// public string ProviderGUID { private set; get; } + /// + /// 所属资源系统 + /// + public AssetSystemImpl Impl { private set; get; } + /// /// 资源信息 /// @@ -90,8 +95,9 @@ namespace YooAsset private readonly List _handles = new List(); - public ProviderBase(string providerGUID, AssetInfo assetInfo) + public ProviderBase(AssetSystemImpl impl, string providerGUID, AssetInfo assetInfo) { + Impl = impl; ProviderGUID = providerGUID; MainAssetInfo = assetInfo; } diff --git a/Assets/YooAsset/Runtime/CacheSystem/CacheSystem.cs b/Assets/YooAsset/Runtime/CacheSystem/CacheSystem.cs index 0a1bd17..4549745 100644 --- a/Assets/YooAsset/Runtime/CacheSystem/CacheSystem.cs +++ b/Assets/YooAsset/Runtime/CacheSystem/CacheSystem.cs @@ -13,13 +13,13 @@ namespace YooAsset /// /// 初始化时的验证级别 /// - public static EVerifyLevel InitVerifyLevel { private set; get; } + public static EVerifyLevel InitVerifyLevel { set; get; } = EVerifyLevel.Low; - public static void Initialize(EVerifyLevel initVerifyLevel) - { - InitVerifyLevel = initVerifyLevel; - } - public static void DestroyAll() + + /// + /// 清空所有数据 + /// + public static void ClearAll() { _cachedDic.Clear(); } diff --git a/Assets/YooAsset/Runtime/Debugger/DebugProviderInfo.cs b/Assets/YooAsset/Runtime/Debugger/DebugProviderInfo.cs index 67f2080..bcfd52e 100644 --- a/Assets/YooAsset/Runtime/Debugger/DebugProviderInfo.cs +++ b/Assets/YooAsset/Runtime/Debugger/DebugProviderInfo.cs @@ -7,6 +7,11 @@ namespace YooAsset [Serializable] internal class DebugProviderInfo : IComparer, IComparable { + /// + /// 所属的资源包裹 + /// + public string PackageName; + /// /// 资源对象路径 /// @@ -35,7 +40,7 @@ namespace YooAsset /// /// 依赖的资源包列表 /// - public List BundleInfos; + public List DependBundleInfos; public int CompareTo(DebugProviderInfo other) { diff --git a/Assets/YooAsset/Runtime/Debugger/DebugReport.cs b/Assets/YooAsset/Runtime/Debugger/DebugReport.cs index 4db512a..2b13fe7 100644 --- a/Assets/YooAsset/Runtime/Debugger/DebugReport.cs +++ b/Assets/YooAsset/Runtime/Debugger/DebugReport.cs @@ -19,17 +19,6 @@ namespace YooAsset /// public int FrameCount; - /// - /// 资源包总数 - /// - public int BundleCount; - - /// - /// 资源对象总数 - /// - public int AssetCount; - - /// /// 序列化 /// diff --git a/Assets/YooAsset/Runtime/Debugger/RemoteDebuggerInRuntime.cs b/Assets/YooAsset/Runtime/Debugger/RemoteDebuggerInRuntime.cs index 243015d..af2e48f 100644 --- a/Assets/YooAsset/Runtime/Debugger/RemoteDebuggerInRuntime.cs +++ b/Assets/YooAsset/Runtime/Debugger/RemoteDebuggerInRuntime.cs @@ -20,7 +20,7 @@ namespace YooAsset { if(UnityEditor.EditorApplication.isPlaying) { - var report = AssetSystem.GetDebugReport(); + var report = YooAssets.GetDebugReport(); EditorHandleDebugReportCallback?.Invoke(0, report); } } @@ -39,7 +39,7 @@ namespace YooAsset YooLogger.Log($"On handle remote command : {command.CommandType} Param : {command.CommandParam}"); if (command.CommandType == (int)ERemoteCommand.SampleOnce) { - var debugReport = AssetSystem.GetDebugReport(); + var debugReport = YooAssets.GetDebugReport(); var data = DebugReport.Serialize(debugReport); PlayerConnection.instance.Send(RemoteDebuggerDefine.kMsgSendPlayerToEditor, data); } diff --git a/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs b/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs index d767451..70f58b6 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystem.cs @@ -14,16 +14,11 @@ namespace YooAsset { private static readonly Dictionary _downloaderDic = new Dictionary(); private static readonly List _removeList = new List(100); - private static int _breakpointResumeFileSize = int.MaxValue; - /// - /// 初始化 + /// 启用断点续传的文件大小 /// - public static void Initialize(int breakpointResumeFileSize) - { - _breakpointResumeFileSize = breakpointResumeFileSize; - } + public static int BreakpointResumeFileSize { set; get; } = int.MaxValue; /// /// 更新所有下载器 @@ -59,7 +54,7 @@ namespace YooAsset } _downloaderDic.Clear(); _removeList.Clear(); - _breakpointResumeFileSize = int.MaxValue; + BreakpointResumeFileSize = int.MaxValue; } @@ -86,7 +81,7 @@ namespace YooAsset { YooLogger.Log($"Beginning to download file : {bundleInfo.Bundle.FileName} URL : {bundleInfo.RemoteMainURL}"); FileUtility.CreateFileDirectory(bundleInfo.Bundle.CachedFilePath); - bool breakDownload = bundleInfo.Bundle.FileSize >= _breakpointResumeFileSize; + bool breakDownload = bundleInfo.Bundle.FileSize >= BreakpointResumeFileSize; DownloaderBase newDownloader = new FileDownloader(bundleInfo, breakDownload); newDownloader.SendRequest(failedTryAgain, timeout); _downloaderDic.Add(bundleInfo.Bundle.CachedFilePath, newDownloader); diff --git a/Assets/YooAsset/Runtime/InitializeParameters.cs b/Assets/YooAsset/Runtime/InitializeParameters.cs index 961df17..007e4c2 100644 --- a/Assets/YooAsset/Runtime/InitializeParameters.cs +++ b/Assets/YooAsset/Runtime/InitializeParameters.cs @@ -2,6 +2,28 @@ namespace YooAsset { + /// + /// 运行模式 + /// + public enum EPlayMode + { + /// + /// 编辑器下的模拟模式 + /// 注意:在初始化的时候自动构建真机模拟环境。 + /// + EditorSimulateMode, + + /// + /// 离线运行模式 + /// + OfflinePlayMode, + + /// + /// 联机运行模式 + /// + HostPlayMode, + } + /// /// 初始化参数 /// @@ -9,6 +31,7 @@ namespace YooAsset { /// /// 资源定位地址大小写不敏感 + /// 注意:默认值为False /// public bool LocationToLower = false; @@ -24,13 +47,9 @@ namespace YooAsset /// /// 资源加载的最大数量 + /// 注意:默认值为MaxValue /// public int AssetLoadingMaxNumber = int.MaxValue; - - /// - /// 异步操作系统每帧允许运行的最大时间切片(单位:毫秒) - /// - public long OperationSystemMaxTimeSlice = long.MaxValue; } /// @@ -42,7 +61,7 @@ namespace YooAsset /// 用于模拟运行的资源清单路径 /// 注意:如果路径为空,会自动重新构建补丁清单。 /// - public string SimulatePatchManifestPath; + public string SimulatePatchManifestPath = string.Empty; } /// @@ -50,10 +69,6 @@ namespace YooAsset /// public class OfflinePlayModeParameters : InitializeParameters { - /// - /// 内置的资源包裹名称 - /// - public string BuildinPackageName = string.Empty; } /// @@ -64,29 +79,12 @@ namespace YooAsset /// /// 默认的资源服务器下载地址 /// - public string DefaultHostServer; + public string DefaultHostServer = string.Empty; /// /// 备用的资源服务器下载地址 /// - public string FallbackHostServer; - -#if UNITY_WEBGL - /// - /// WEBGL模式不支持多线程下载 - /// - internal int BreakpointResumeFileSize = int.MaxValue; -#else - /// - /// 启用断点续传功能的文件大小 - /// - public int BreakpointResumeFileSize = int.MaxValue; -#endif - - /// - /// 下载文件校验等级 - /// - public EVerifyLevel VerifyLevel = EVerifyLevel.High; + public string FallbackHostServer = string.Empty; /// /// 查询服务类 diff --git a/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs b/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs index ac39ec5..85ce6fe 100644 --- a/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs +++ b/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs @@ -10,9 +10,13 @@ namespace YooAsset // 计时器相关 private static Stopwatch _watch; - private static long _maxTimeSlice; private static long _frameTime; + /// + /// 异步操作的最小时间片段 + /// + public static long MaxTimeSlice { set; get; } = long.MaxValue; + /// /// 处理器是否繁忙 /// @@ -20,7 +24,7 @@ namespace YooAsset { get { - return _watch.ElapsedMilliseconds - _frameTime >= _maxTimeSlice; + return _watch.ElapsedMilliseconds - _frameTime >= MaxTimeSlice; } } @@ -28,9 +32,8 @@ namespace YooAsset /// /// 初始化异步操作系统 /// - public static void Initialize(long maxTimeSlice) + public static void Initialize() { - _maxTimeSlice = maxTimeSlice; _watch = Stopwatch.StartNew(); } @@ -63,8 +66,8 @@ namespace YooAsset { _operations.Clear(); _watch = null; - _maxTimeSlice = 0; _frameTime = 0; + MaxTimeSlice = long.MaxValue; } /// diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/ClearUnusedCacheFilesOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/ClearUnusedCacheFilesOperation.cs deleted file mode 100644 index cebf6a7..0000000 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/ClearUnusedCacheFilesOperation.cs +++ /dev/null @@ -1,111 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using System.IO; -using UnityEngine; - -namespace YooAsset -{ - /// - /// 清理未使用的缓存资源操作类 - /// - public abstract class ClearUnusedCacheFilesOperation : AsyncOperationBase - { - } - - /// - /// 编辑器模式 - /// - internal sealed class EditorPlayModeClearUnusedCacheFilesOperation : ClearUnusedCacheFilesOperation - { - internal override void Start() - { - Status = EOperationStatus.Succeed; - } - internal override void Update() - { - } - } - - /// - /// 离线模式 - /// - internal sealed class OfflinePlayModeClearUnusedCacheFilesOperation : ClearUnusedCacheFilesOperation - { - internal override void Start() - { - Status = EOperationStatus.Succeed; - } - internal override void Update() - { - } - } - - /// - /// 联机模式 - /// - internal sealed class HostPlayModeClearUnusedCacheFilesOperation : ClearUnusedCacheFilesOperation - { - private enum ESteps - { - None, - GetUnusedCacheFiles, - ClearUnusedCacheFiles, - Done, - } - - private ESteps _steps = ESteps.None; - private List _unusedCacheFilePaths; - private int _unusedFileTotalCount = 0; - private HostPlayModeImpl _impl; - - internal HostPlayModeClearUnusedCacheFilesOperation(HostPlayModeImpl impl) - { - _impl = impl; - } - internal override void Start() - { - _steps = ESteps.GetUnusedCacheFiles; - } - internal override void Update() - { - if (_steps == ESteps.None || _steps == ESteps.Done) - return; - - if (_steps == ESteps.GetUnusedCacheFiles) - { - _unusedCacheFilePaths = _impl.ClearUnusedCacheFilePaths(); - _unusedFileTotalCount = _unusedCacheFilePaths.Count; - YooLogger.Log($"Found unused cache file count : {_unusedFileTotalCount}"); - _steps = ESteps.ClearUnusedCacheFiles; - } - - if (_steps == ESteps.ClearUnusedCacheFiles) - { - for (int i = _unusedCacheFilePaths.Count - 1; i >= 0; i--) - { - string filePath = _unusedCacheFilePaths[i]; - if (File.Exists(filePath)) - { - YooLogger.Log($"Delete unused cache file : {filePath}"); - File.Delete(filePath); - } - _unusedCacheFilePaths.RemoveAt(i); - - if (OperationSystem.IsBusy) - break; - } - - if (_unusedFileTotalCount == 0) - Progress = 1.0f; - else - Progress = 1.0f - (_unusedCacheFilePaths.Count / _unusedFileTotalCount); - - if (_unusedCacheFilePaths.Count == 0) - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - } - } - } -} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs index 9dc7ccf..2c42f3f 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/InitializationOperation.cs @@ -20,7 +20,6 @@ namespace YooAsset private enum ESteps { None, - Builder, Load, Done, } @@ -36,26 +35,10 @@ namespace YooAsset } internal override void Start() { - if (string.IsNullOrEmpty(_simulatePatchManifestPath)) - _steps = ESteps.Builder; - else - _steps = ESteps.Load; + _steps = ESteps.Load; } internal override void Update() { - if (_steps == ESteps.Builder) - { - _simulatePatchManifestPath = EditorSimulateModeHelper.SimulateBuild(); - if (string.IsNullOrEmpty(_simulatePatchManifestPath)) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = "Simulate build failed, see the detail info on the console window."; - return; - } - _steps = ESteps.Load; - } - if (_steps == ESteps.Load) { if (File.Exists(_simulatePatchManifestPath) == false) diff --git a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeHelper.cs b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeHelper.cs index b050f93..0815cc3 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeHelper.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeHelper.cs @@ -3,20 +3,22 @@ using System.Reflection; namespace YooAsset { - internal static class EditorSimulateModeHelper + public static class EditorSimulateModeHelper { private static System.Type _classType; - public static string SimulateBuild() + /// + /// 编辑器下模拟构建补丁清单 + /// + public static string SimulateBuild(string packageName, bool enableAddressable) { - _classType = Assembly.Load("YooAsset.Editor").GetType("YooAsset.Editor.AssetBundleSimulateBuilder"); - InvokePublicStaticMethod(_classType, "SimulateBuild"); - return GetPatchManifestFilePath(); - } - private static string GetPatchManifestFilePath() - { - return (string)InvokePublicStaticMethod(_classType, "GetPatchManifestPath"); + if (_classType == null) + _classType = Assembly.Load("YooAsset.Editor").GetType("YooAsset.Editor.AssetBundleSimulateBuilder"); + + string manifestFilePath = (string)InvokePublicStaticMethod(_classType, "SimulateBuild", packageName, enableAddressable); + return manifestFilePath; } + private static object InvokePublicStaticMethod(System.Type type, string method, params object[] parameters) { var methodInfo = type.GetMethod(method, BindingFlags.Public | BindingFlags.Static); @@ -30,8 +32,14 @@ namespace YooAsset } } #else - internal static class EditorSimulateModeHelper +namespace YooAsset +{ + public static class EditorSimulateModeHelper { - public static string SimulateBuild() { throw new System.Exception("Only support in unity editor !"); } + /// + /// 编辑器下模拟构建补丁清单 + /// + public static string SimulateBuild(string packageName, bool enableAddressable) { throw new System.Exception("Only support in unity editor !"); } } +} #endif \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeImpl.cs b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeImpl.cs index 0e985d4..3d5969d 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeImpl.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/EditorSimulateModeImpl.cs @@ -57,6 +57,10 @@ namespace YooAsset { return _simulatePatchManifest.MappingToAssetPath(location); } + string IBundleServices.GetPackageName() + { + return _simulatePatchManifest.PackageName; + } #endregion } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs index cf6bc30..936eafa 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs @@ -71,35 +71,6 @@ namespace YooAsset return operation; } - /// - /// 获取未被使用的缓存文件路径集合 - /// - public List ClearUnusedCacheFilePaths() - { - string cacheFolderPath = SandboxHelper.GetCacheFolderPath(); - if (Directory.Exists(cacheFolderPath) == false) - return new List(); - - DirectoryInfo directoryInfo = new DirectoryInfo(cacheFolderPath); - FileInfo[] fileInfos = directoryInfo.GetFiles(); - List result = new List(fileInfos.Length); - foreach (FileInfo fileInfo in fileInfos) - { - bool used = false; - foreach (var patchBundle in LocalPatchManifest.BundleList) - { - if (fileInfo.Name == patchBundle.FileName) - { - used = true; - break; - } - } - if (used == false) - result.Add(fileInfo.FullName); - } - return result; - } - /// /// 创建下载器 /// @@ -379,6 +350,10 @@ namespace YooAsset { return LocalPatchManifest.MappingToAssetPath(location); } + string IBundleServices.GetPackageName() + { + return LocalPatchManifest.PackageName; + } #endregion } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs index 95640d9..847766e 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs +++ b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/OfflinePlayModeImpl.cs @@ -85,6 +85,10 @@ namespace YooAsset { return _appPatchManifest.MappingToAssetPath(location); } + string IBundleServices.GetPackageName() + { + return _appPatchManifest.PackageName; + } #endregion } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/Services/IBundleServices.cs b/Assets/YooAsset/Runtime/Services/IBundleServices.cs index 3eb381c..f9cd655 100644 --- a/Assets/YooAsset/Runtime/Services/IBundleServices.cs +++ b/Assets/YooAsset/Runtime/Services/IBundleServices.cs @@ -27,5 +27,10 @@ namespace YooAsset /// 映射为资源路径 /// string MappingToAssetPath(string location); + + /// + /// 获取所属的包裹名 + /// + string GetPackageName(); } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/Services/ILocationServices.cs b/Assets/YooAsset/Runtime/Services/ILocationServices.cs index 65df54f..4d8c891 100644 --- a/Assets/YooAsset/Runtime/Services/ILocationServices.cs +++ b/Assets/YooAsset/Runtime/Services/ILocationServices.cs @@ -6,6 +6,6 @@ namespace YooAsset /// /// 定位地址转换为资源路径 /// - string ConvertLocationToAssetPath(string location); + string ConvertLocationToAssetPath(YooAssetPackage 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 ff1d7e7..f5f3020 100644 --- a/Assets/YooAsset/Runtime/Services/LocationServices/AddressLocationServices.cs +++ b/Assets/YooAsset/Runtime/Services/LocationServices/AddressLocationServices.cs @@ -3,9 +3,9 @@ namespace YooAsset { public class AddressLocationServices : ILocationServices { - string ILocationServices.ConvertLocationToAssetPath(string location) + string ILocationServices.ConvertLocationToAssetPath(YooAssetPackage package, string location) { - return YooAssets.MappingToAssetPath(location); + return package.MappingToAssetPath(location); } } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/Services/LocationServices/DefaultLocationServices.cs b/Assets/YooAsset/Runtime/Services/LocationServices/DefaultLocationServices.cs index a0338ed..7d92c7d 100644 --- a/Assets/YooAsset/Runtime/Services/LocationServices/DefaultLocationServices.cs +++ b/Assets/YooAsset/Runtime/Services/LocationServices/DefaultLocationServices.cs @@ -11,16 +11,16 @@ namespace YooAsset _resourceRoot = PathHelper.GetRegularPath(resourceRoot); } - string ILocationServices.ConvertLocationToAssetPath(string location) + string ILocationServices.ConvertLocationToAssetPath(YooAssetPackage package, string location) { if (string.IsNullOrEmpty(_resourceRoot)) { - return YooAssets.MappingToAssetPath(location); + return package.MappingToAssetPath(location); } else { string tempLocation = $"{_resourceRoot}/{location}"; - return YooAssets.MappingToAssetPath(tempLocation); + return package.MappingToAssetPath(tempLocation); } } } diff --git a/Assets/YooAsset/Runtime/YooAssetComponent.cs b/Assets/YooAsset/Runtime/YooAssetComponent.cs index bb14bf3..4db450f 100644 --- a/Assets/YooAsset/Runtime/YooAssetComponent.cs +++ b/Assets/YooAsset/Runtime/YooAssetComponent.cs @@ -6,40 +6,23 @@ using UnityEngine.SceneManagement; namespace YooAsset { - public class YooAssetComponent + public class YooAssetPackage { - /// - /// 运行模式 - /// - public enum EPlayMode - { - /// - /// 编辑器下的模拟模式 - /// 注意:在初始化的时候自动构建真机模拟环境。 - /// - EditorSimulateMode, - - /// - /// 离线运行模式 - /// - OfflinePlayMode, - - /// - /// 联机运行模式 - /// - HostPlayMode, - } - private bool _isInitialize = false; private string _initializeError = string.Empty; private EOperationStatus _initializeStatus = EOperationStatus.None; private EPlayMode _playMode; private IBundleServices _bundleServices; private ILocationServices _locationServices; + private AssetSystemImpl _assetSystemImpl; private EditorSimulateModeImpl _editorSimulateModeImpl; private OfflinePlayModeImpl _offlinePlayModeImpl; private HostPlayModeImpl _hostPlayModeImpl; + /// + /// 包裹名 + /// + public string PackageName { private set; get; } /// /// 是否已经初始化 @@ -49,19 +32,141 @@ namespace YooAsset get { return _isInitialize; } } + + /// + /// 拒绝外部实例化 + /// + internal YooAssetPackage() + { + } + internal YooAssetPackage(string packageName) + { + PackageName = packageName; + } + + /// + /// 更新资源包裹 + /// + internal void UpdatePackage() + { + if (_assetSystemImpl != null) + _assetSystemImpl.Update(); + } + + /// + /// 销毁资源包裹 + /// + internal void DestroyPackage() + { + if (_isInitialize) + { + _isInitialize = false; + _initializeError = string.Empty; + _initializeStatus = EOperationStatus.None; + + _bundleServices = null; + _locationServices = null; + _editorSimulateModeImpl = null; + _offlinePlayModeImpl = null; + _hostPlayModeImpl = null; + + if (_assetSystemImpl != null) + { + _assetSystemImpl.DestroyAll(); + _assetSystemImpl = null; + } + + YooLogger.Log("YooAssets destroy all !"); + } + } + /// /// 异步初始化 /// public InitializationOperation InitializeAsync(InitializeParameters parameters) { + // 检测初始化参数合法性 + CheckInitializeParameters(parameters); + + // 初始化资源系统 + InitializationOperation initializeOperation; + _locationServices = parameters.LocationServices; + _assetSystemImpl = new AssetSystemImpl(); + if (_playMode == EPlayMode.EditorSimulateMode) + { + _editorSimulateModeImpl = new EditorSimulateModeImpl(); + _bundleServices = _editorSimulateModeImpl; + _assetSystemImpl.Initialize(true, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices); + var initializeParameters = parameters as EditorSimulateModeParameters; + initializeOperation = _editorSimulateModeImpl.InitializeAsync( + initializeParameters.LocationToLower, + initializeParameters.SimulatePatchManifestPath); + } + else if (_playMode == EPlayMode.OfflinePlayMode) + { + _offlinePlayModeImpl = new OfflinePlayModeImpl(); + _bundleServices = _offlinePlayModeImpl; + _assetSystemImpl.Initialize(false, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices); + var initializeParameters = parameters as OfflinePlayModeParameters; + initializeOperation = _offlinePlayModeImpl.InitializeAsync( + initializeParameters.LocationToLower, + PackageName); + } + else if (_playMode == EPlayMode.HostPlayMode) + { + _hostPlayModeImpl = new HostPlayModeImpl(); + _bundleServices = _hostPlayModeImpl; + _assetSystemImpl.Initialize(false, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices); + var initializeParameters = parameters as HostPlayModeParameters; + initializeOperation = _hostPlayModeImpl.InitializeAsync( + initializeParameters.LocationToLower, + initializeParameters.DefaultHostServer, + initializeParameters.FallbackHostServer, + initializeParameters.QueryServices); + } + else + { + throw new NotImplementedException(); + } + + // 监听初始化结果 + initializeOperation.Completed += InitializeOperation_Completed; + return initializeOperation; + } + private void CheckInitializeParameters(InitializeParameters parameters) + { + if (_isInitialize) + throw new Exception($"{nameof(YooAssetPackage)} is initialized yet."); + if (parameters == null) - throw new Exception($"YooAsset create parameters is null."); + throw new Exception($"{nameof(YooAssetPackage)} create parameters is null."); #if !UNITY_EDITOR if (parameters is EditorSimulateModeParameters) throw new Exception($"Editor simulate mode only support unity editor."); #endif + if (parameters.LocationServices == null) + throw new Exception($"{nameof(ILocationServices)} is null."); + + if(parameters is EditorSimulateModeParameters) + { + var editorSimulateModeParameters = parameters as EditorSimulateModeParameters; + if (string.IsNullOrEmpty(editorSimulateModeParameters.SimulatePatchManifestPath)) + throw new Exception($"${editorSimulateModeParameters.SimulatePatchManifestPath} is null or empty."); + } + + if (parameters is HostPlayModeParameters) + { + var hostPlayModeParameters = parameters as HostPlayModeParameters; + if (string.IsNullOrEmpty(hostPlayModeParameters.DefaultHostServer)) + throw new Exception($"${hostPlayModeParameters.DefaultHostServer} is null or empty."); + if (string.IsNullOrEmpty(hostPlayModeParameters.FallbackHostServer)) + throw new Exception($"${hostPlayModeParameters.FallbackHostServer} is null or empty."); + if (hostPlayModeParameters.QueryServices == null) + throw new Exception($"{nameof(IQueryServices)} is null."); + } + // 鉴定运行模式 if (parameters is EditorSimulateModeParameters) _playMode = EPlayMode.EditorSimulateMode; @@ -72,113 +177,12 @@ namespace YooAsset else throw new NotImplementedException(); - if (parameters.LocationServices == null) - throw new Exception($"{nameof(IBundleServices)} is null."); - - if (_playMode == EPlayMode.OfflinePlayMode) - { - var playModeParameters = parameters as OfflinePlayModeParameters; - if (string.IsNullOrEmpty(playModeParameters.BuildinPackageName)) - throw new Exception($"{nameof(playModeParameters.BuildinPackageName)} is empty."); - } - - if (_playMode == EPlayMode.HostPlayMode) - { - var playModeParameters = parameters as HostPlayModeParameters; - if (playModeParameters.QueryServices == null) - throw new Exception($"{nameof(IQueryServices)} is null."); - } - - if (_isInitialize) - { - throw new Exception("YooAsset is initialized yet."); - } - - _locationServices = parameters.LocationServices; - // 检测参数范围 if (parameters.AssetLoadingMaxNumber < 1) { parameters.AssetLoadingMaxNumber = 1; YooLogger.Warning($"{nameof(parameters.AssetLoadingMaxNumber)} minimum value is 1"); } - if (parameters.OperationSystemMaxTimeSlice < 30) - { - parameters.OperationSystemMaxTimeSlice = 30; - YooLogger.Warning($"{nameof(parameters.OperationSystemMaxTimeSlice)} minimum value is 30 milliseconds"); - } - - // 创建驱动器 - if (_isInitialize == false) - { - _isInitialize = true; - UnityEngine.GameObject driverGo = new UnityEngine.GameObject("[YooAsset]"); - driverGo.AddComponent(); - UnityEngine.Object.DontDestroyOnLoad(driverGo); - -#if DEBUG - driverGo.AddComponent(); -#endif - } - - // 初始化异步系统 - OperationSystem.Initialize(parameters.OperationSystemMaxTimeSlice); - - // 初始化下载系统 - if (_playMode == EPlayMode.HostPlayMode) - { - var hostPlayModeParameters = parameters as HostPlayModeParameters; - CacheSystem.Initialize(hostPlayModeParameters.VerifyLevel); - DownloadSystem.Initialize(hostPlayModeParameters.BreakpointResumeFileSize); - } - else - { - CacheSystem.Initialize(EVerifyLevel.Low); - DownloadSystem.Initialize(int.MaxValue); - } - - // 初始化资源系统 - InitializationOperation initializeOperation; - if (_playMode == EPlayMode.EditorSimulateMode) - { - _editorSimulateModeImpl = new EditorSimulateModeImpl(); - _bundleServices = _editorSimulateModeImpl; - AssetSystem.Initialize(true, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices); - var simulateModeParameters = parameters as EditorSimulateModeParameters; - initializeOperation = _editorSimulateModeImpl.InitializeAsync( - simulateModeParameters.LocationToLower, - simulateModeParameters.SimulatePatchManifestPath); - } - else if (_playMode == EPlayMode.OfflinePlayMode) - { - _offlinePlayModeImpl = new OfflinePlayModeImpl(); - _bundleServices = _offlinePlayModeImpl; - AssetSystem.Initialize(false, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices); - var playModeParameters = parameters as OfflinePlayModeParameters; - initializeOperation = _offlinePlayModeImpl.InitializeAsync( - playModeParameters.LocationToLower, - playModeParameters.BuildinPackageName); - } - else if (_playMode == EPlayMode.HostPlayMode) - { - _hostPlayModeImpl = new HostPlayModeImpl(); - _bundleServices = _hostPlayModeImpl; - AssetSystem.Initialize(false, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices); - var playModeParameters = parameters as HostPlayModeParameters; - initializeOperation = _hostPlayModeImpl.InitializeAsync( - playModeParameters.LocationToLower, - playModeParameters.DefaultHostServer, - playModeParameters.FallbackHostServer, - playModeParameters.QueryServices); - } - else - { - throw new NotImplementedException(); - } - - // 监听初始化结果 - initializeOperation.Completed += InitializeOperation_Completed; - return initializeOperation; } private void InitializeOperation_Completed(AsyncOperationBase op) { @@ -189,9 +193,8 @@ namespace YooAsset /// /// 向网络端请求静态资源版本 /// - /// 更新的资源包裹名称 /// 超时时间(默认值:60秒) - public UpdateStaticVersionOperation UpdateStaticVersionAsync(string packageName, int timeout = 60) + public UpdateStaticVersionOperation UpdateStaticVersionAsync(int timeout = 60) { DebugCheckInitialize(); if (_playMode == EPlayMode.EditorSimulateMode) @@ -208,7 +211,7 @@ namespace YooAsset } else if (_playMode == EPlayMode.HostPlayMode) { - return _hostPlayModeImpl.UpdateStaticVersionAsync(packageName, timeout); + return _hostPlayModeImpl.UpdateStaticVersionAsync(PackageName, timeout); } else { @@ -219,10 +222,9 @@ namespace YooAsset /// /// 向网络端请求并更新补丁清单 /// - /// 更新的资源包裹名称 /// 更新的资源包裹版本 /// 超时时间(默认值:60秒) - public UpdateManifestOperation UpdateManifestAsync(string packageName, string packageCRC, int timeout = 60) + public UpdateManifestOperation UpdateManifestAsync(string packageCRC, int timeout = 60) { DebugCheckInitialize(); DebugCheckUpdateManifest(); @@ -240,7 +242,7 @@ namespace YooAsset } else if (_playMode == EPlayMode.HostPlayMode) { - return _hostPlayModeImpl.UpdatePatchManifestAsync(packageName, packageCRC, timeout); + return _hostPlayModeImpl.UpdatePatchManifestAsync(PackageName, packageCRC, timeout); } else { @@ -252,9 +254,8 @@ namespace YooAsset /// 弱联网情况下加载补丁清单 /// 注意:当指定版本内容验证失败后会返回失败。 /// - /// 指定的资源包裹名称 /// 指定的资源包裹版本 - public UpdateManifestOperation WeaklyUpdateManifestAsync(string packageName, string packageCRC) + public UpdateManifestOperation WeaklyUpdateManifestAsync(string packageCRC) { DebugCheckInitialize(); if (_playMode == EPlayMode.EditorSimulateMode) @@ -271,7 +272,7 @@ namespace YooAsset } else if (_playMode == EPlayMode.HostPlayMode) { - return _hostPlayModeImpl.WeaklyUpdatePatchManifestAsync(packageName, packageCRC); + return _hostPlayModeImpl.WeaklyUpdatePatchManifestAsync(PackageName, packageCRC); } else { @@ -279,15 +280,6 @@ namespace YooAsset } } - /// - /// 开启一个异步操作 - /// - /// 异步操作对象 - public void StartOperation(GameAsyncOperation operation) - { - OperationSystem.StartOperation(operation); - } - /// /// 资源回收(卸载引用计数为零的资源) /// @@ -295,8 +287,8 @@ namespace YooAsset { if (_isInitialize) { - AssetSystem.Update(); - AssetSystem.UnloadUnusedAssets(); + _assetSystemImpl.Update(); + _assetSystemImpl.UnloadUnusedAssets(); } } @@ -307,7 +299,7 @@ namespace YooAsset { if (_isInitialize) { - AssetSystem.ForceUnloadAllAssets(); + _assetSystemImpl.ForceUnloadAllAssets(); } } @@ -391,7 +383,7 @@ namespace YooAsset public string GetAssetPath(string location) { DebugCheckInitialize(); - return _locationServices.ConvertLocationToAssetPath(location); + return _locationServices.ConvertLocationToAssetPath(this, location); } #endregion @@ -481,7 +473,7 @@ namespace YooAsset { DebugCheckInitialize(); AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null); - var handle = AssetSystem.LoadSceneAsync(assetInfo, sceneMode, activateOnLoad, priority); + var handle = _assetSystemImpl.LoadSceneAsync(assetInfo, sceneMode, activateOnLoad, priority); return handle; } @@ -497,7 +489,7 @@ namespace YooAsset DebugCheckInitialize(); if (assetInfo.IsInvalid) YooLogger.Warning(assetInfo.Error); - var handle = AssetSystem.LoadSceneAsync(assetInfo, sceneMode, activateOnLoad, priority); + var handle = _assetSystemImpl.LoadSceneAsync(assetInfo, sceneMode, activateOnLoad, priority); return handle; } #endregion @@ -591,7 +583,7 @@ namespace YooAsset } #endif - var handle = AssetSystem.LoadAssetAsync(assetInfo); + var handle = _assetSystemImpl.LoadAssetAsync(assetInfo); if (waitForAsyncComplete) handle.WaitForAsyncComplete(); return handle; @@ -687,7 +679,7 @@ namespace YooAsset } #endif - var handle = AssetSystem.LoadSubAssetsAsync(assetInfo); + var handle = _assetSystemImpl.LoadSubAssetsAsync(assetInfo); if (waitForAsyncComplete) handle.WaitForAsyncComplete(); return handle; @@ -893,10 +885,9 @@ namespace YooAsset /// /// 创建资源包裹下载器,用于下载更新指定资源版本所有的资源包文件 /// - /// 指定更新的资源包裹名称 /// 指定更新的资源包裹版本 /// 超时时间 - public UpdatePackageOperation UpdatePackageAsync(string packageName, string packageCRC, int timeout = 60) + public UpdatePackageOperation UpdatePackageAsync(string packageCRC, int timeout = 60) { DebugCheckInitialize(); if (_playMode == EPlayMode.EditorSimulateMode) @@ -913,63 +904,7 @@ namespace YooAsset } else if (_playMode == EPlayMode.HostPlayMode) { - return _hostPlayModeImpl.UpdatePackageAsync(packageName, packageCRC, timeout); - } - else - { - throw new NotImplementedException(); - } - } - #endregion - - #region 沙盒相关 - /// - /// 获取沙盒的根路径 - /// - public string GetSandboxRoot() - { - return PathHelper.MakePersistentRootPath(); - } - - /// - /// 清空沙盒目录 - /// - public void ClearSandbox() - { - SandboxHelper.DeleteSandbox(); - } - - /// - /// 清空所有的缓存文件 - /// - public void ClearAllCacheFiles() - { - SandboxHelper.DeleteCacheFolder(); - } - - /// - /// 清空未被使用的缓存文件 - /// - public ClearUnusedCacheFilesOperation ClearUnusedCacheFiles() - { - DebugCheckInitialize(); - if (_playMode == EPlayMode.EditorSimulateMode) - { - var operation = new EditorPlayModeClearUnusedCacheFilesOperation(); - OperationSystem.StartOperation(operation); - return operation; - } - else if (_playMode == EPlayMode.OfflinePlayMode) - { - var operation = new OfflinePlayModeClearUnusedCacheFilesOperation(); - OperationSystem.StartOperation(operation); - return operation; - } - else if (_playMode == EPlayMode.HostPlayMode) - { - var operation = new HostPlayModeClearUnusedCacheFilesOperation(_hostPlayModeImpl); - OperationSystem.StartOperation(operation); - return operation; + return _hostPlayModeImpl.UpdatePackageAsync(PackageName, packageCRC, timeout); } else { @@ -979,34 +914,6 @@ namespace YooAsset #endregion #region 内部方法 - internal void InternalDestroy() - { - if (_isInitialize) - { - _isInitialize = false; - _initializeError = string.Empty; - _initializeStatus = EOperationStatus.None; - - _bundleServices = null; - _locationServices = null; - _editorSimulateModeImpl = null; - _offlinePlayModeImpl = null; - _hostPlayModeImpl = null; - - OperationSystem.DestroyAll(); - DownloadSystem.DestroyAll(); - CacheSystem.DestroyAll(); - AssetSystem.DestroyAll(); - YooLogger.Log("YooAssets destroy all !"); - } - } - internal void InternalUpdate() - { - OperationSystem.Update(); - DownloadSystem.Update(); - AssetSystem.Update(); - } - /// /// 资源定位地址转换为资源完整路径 /// @@ -1047,7 +954,7 @@ namespace YooAsset [Conditional("DEBUG")] private void DebugCheckUpdateManifest() { - var loadedBundleInfos = AssetSystem.GetLoadedBundleInfos(); + var loadedBundleInfos = _assetSystemImpl.GetLoadedBundleInfos(); if (loadedBundleInfos.Count > 0) { YooLogger.Warning($"Found loaded bundle before update manifest ! Recommended to call the {nameof(ForceUnloadAllAssets)} method to release loaded bundle !"); @@ -1055,6 +962,13 @@ namespace YooAsset } #endregion + #region 调试信息 + internal List GetDebugReportInfos() + { + return _assetSystemImpl.GetDebugReportInfos(); + } + #endregion + #region 私有方法 /// /// 资源定位地址转换为资源信息类,失败时内部会发出错误日志。 @@ -1063,7 +977,7 @@ namespace YooAsset private AssetInfo ConvertLocationToAssetInfo(string location, System.Type assetType) { DebugCheckLocation(location); - string assetPath = _locationServices.ConvertLocationToAssetPath(location); + string assetPath = _locationServices.ConvertLocationToAssetPath(this, location); PatchAsset patchAsset = _bundleServices.TryGetPatchAsset(assetPath); if (patchAsset != null) { diff --git a/Assets/YooAsset/Runtime/YooAssetDriver.cs b/Assets/YooAsset/Runtime/YooAssetDriver.cs index 96c9117..94bb69a 100644 --- a/Assets/YooAsset/Runtime/YooAssetDriver.cs +++ b/Assets/YooAsset/Runtime/YooAssetDriver.cs @@ -6,17 +6,17 @@ namespace YooAsset { void Update() { - YooAssets.InternalUpdate(); + YooAssets.Update(); } void OnDestroy() { - YooAssets.InternalDestroy(); + YooAssets.Destroy(); } void OnApplicationQuit() { - YooAssets.InternalDestroy(); + YooAssets.Destroy(); } } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/YooAssets.cs b/Assets/YooAsset/Runtime/YooAssets.cs index 4f4a91f..8b75e71 100644 --- a/Assets/YooAsset/Runtime/YooAssets.cs +++ b/Assets/YooAsset/Runtime/YooAssets.cs @@ -2,204 +2,22 @@ using System; using System.Diagnostics; using System.Collections; using System.Collections.Generic; -using UnityEngine.SceneManagement; +using UnityEngine; namespace YooAsset { public static partial class YooAssets { - /// - /// 运行模式 - /// - public enum EPlayMode - { - /// - /// 编辑器下的模拟模式 - /// 注意:在初始化的时候自动构建真机模拟环境。 - /// - EditorSimulateMode, - - /// - /// 离线运行模式 - /// - OfflinePlayMode, - - /// - /// 联机运行模式 - /// - HostPlayMode, - } - - /// - /// 初始化参数 - /// - public abstract class InitializeParameters - { - /// - /// 资源定位地址大小写不敏感 - /// - public bool LocationToLower = false; - - /// - /// 资源定位服务接口 - /// - public ILocationServices LocationServices = null; - - /// - /// 文件解密服务接口 - /// - public IDecryptionServices DecryptionServices = null; - - /// - /// 资源加载的最大数量 - /// - public int AssetLoadingMaxNumber = int.MaxValue; - - /// - /// 异步操作系统每帧允许运行的最大时间切片(单位:毫秒) - /// - public long OperationSystemMaxTimeSlice = long.MaxValue; - } - - /// - /// 编辑器下模拟运行模式的初始化参数 - /// - public class EditorSimulateModeParameters : InitializeParameters - { - /// - /// 用于模拟运行的资源清单路径 - /// 注意:如果路径为空,会自动重新构建补丁清单。 - /// - public string SimulatePatchManifestPath; - } - - /// - /// 离线运行模式的初始化参数 - /// - public class OfflinePlayModeParameters : InitializeParameters - { - /// - /// 内置的资源包裹名称 - /// - public string BuildinPackageName = string.Empty; - } - - /// - /// 联机运行模式的初始化参数 - /// - public class HostPlayModeParameters : InitializeParameters - { - /// - /// 默认的资源服务器下载地址 - /// - public string DefaultHostServer; - - /// - /// 备用的资源服务器下载地址 - /// - public string FallbackHostServer; - -#if UNITY_WEBGL - /// - /// WEBGL模式不支持多线程下载 - /// - internal int BreakpointResumeFileSize = int.MaxValue; -#else - /// - /// 启用断点续传功能的文件大小 - /// - public int BreakpointResumeFileSize = int.MaxValue; -#endif - - /// - /// 下载文件校验等级 - /// - public EVerifyLevel VerifyLevel = EVerifyLevel.High; - - /// - /// 查询服务类 - /// - public IQueryServices QueryServices = null; - } - - 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; - private static EditorSimulateModeImpl _editorSimulateModeImpl; - private static OfflinePlayModeImpl _offlinePlayModeImpl; - private static HostPlayModeImpl _hostPlayModeImpl; - + private static readonly List _packages = new List(); /// - /// 是否已经初始化 + /// 初始化 /// - public static bool IsInitialized + public static void Initialize() { - get { return _isInitialize; } - } - - /// - /// 异步初始化 - /// - public static InitializationOperation InitializeAsync(InitializeParameters parameters) - { - if (parameters == null) - throw new Exception($"YooAsset create parameters is null."); - -#if !UNITY_EDITOR - if (parameters is EditorSimulateModeParameters) - throw new Exception($"Editor simulate mode only support unity editor."); -#endif - - // 鉴定运行模式 - if (parameters is EditorSimulateModeParameters) - _playMode = EPlayMode.EditorSimulateMode; - else if (parameters is OfflinePlayModeParameters) - _playMode = EPlayMode.OfflinePlayMode; - else if (parameters is HostPlayModeParameters) - _playMode = EPlayMode.HostPlayMode; - else - throw new NotImplementedException(); - - if (parameters.LocationServices == null) - throw new Exception($"{nameof(IBundleServices)} is null."); - - if (_playMode == EPlayMode.OfflinePlayMode) - { - var playModeParameters = parameters as OfflinePlayModeParameters; - if (string.IsNullOrEmpty(playModeParameters.BuildinPackageName)) - throw new Exception($"{nameof(playModeParameters.BuildinPackageName)} is empty."); - } - - if (_playMode == EPlayMode.HostPlayMode) - { - var playModeParameters = parameters as HostPlayModeParameters; - if (playModeParameters.QueryServices == null) - throw new Exception($"{nameof(IQueryServices)} is null."); - } - if (_isInitialize) - { - throw new Exception("YooAsset is initialized yet."); - } - - _locationServices = parameters.LocationServices; - - // 检测参数范围 - if (parameters.AssetLoadingMaxNumber < 1) - { - parameters.AssetLoadingMaxNumber = 1; - YooLogger.Warning($"{nameof(parameters.AssetLoadingMaxNumber)} minimum value is 1"); - } - if (parameters.OperationSystemMaxTimeSlice < 30) - { - parameters.OperationSystemMaxTimeSlice = 30; - YooLogger.Warning($"{nameof(parameters.OperationSystemMaxTimeSlice)} minimum value is 30 milliseconds"); - } + throw new Exception("YooAssets is initialized !"); // 创建驱动器 if (_isInitialize == false) @@ -212,164 +30,84 @@ namespace YooAsset #if DEBUG driverGo.AddComponent(); #endif - } - // 初始化异步系统 - OperationSystem.Initialize(parameters.OperationSystemMaxTimeSlice); - - // 初始化下载系统 - if (_playMode == EPlayMode.HostPlayMode) - { - var hostPlayModeParameters = parameters as HostPlayModeParameters; - CacheSystem.Initialize(hostPlayModeParameters.VerifyLevel); - DownloadSystem.Initialize(hostPlayModeParameters.BreakpointResumeFileSize); - } - else - { - CacheSystem.Initialize(EVerifyLevel.Low); - DownloadSystem.Initialize(int.MaxValue); - } - - // 初始化资源系统 - InitializationOperation initializeOperation; - if (_playMode == EPlayMode.EditorSimulateMode) - { - _editorSimulateModeImpl = new EditorSimulateModeImpl(); - _bundleServices = _editorSimulateModeImpl; - AssetSystem.Initialize(true, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices); - var simulateModeParameters = parameters as EditorSimulateModeParameters; - initializeOperation = _editorSimulateModeImpl.InitializeAsync( - simulateModeParameters.LocationToLower, - simulateModeParameters.SimulatePatchManifestPath); - } - else if (_playMode == EPlayMode.OfflinePlayMode) - { - _offlinePlayModeImpl = new OfflinePlayModeImpl(); - _bundleServices = _offlinePlayModeImpl; - AssetSystem.Initialize(false, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices); - var playModeParameters = parameters as OfflinePlayModeParameters; - initializeOperation = _offlinePlayModeImpl.InitializeAsync( - playModeParameters.LocationToLower, - playModeParameters.BuildinPackageName); - } - else if (_playMode == EPlayMode.HostPlayMode) - { - _hostPlayModeImpl = new HostPlayModeImpl(); - _bundleServices = _hostPlayModeImpl; - AssetSystem.Initialize(false, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices); - var playModeParameters = parameters as HostPlayModeParameters; - initializeOperation = _hostPlayModeImpl.InitializeAsync( - playModeParameters.LocationToLower, - playModeParameters.DefaultHostServer, - playModeParameters.FallbackHostServer, - playModeParameters.QueryServices); - } - else - { - throw new NotImplementedException(); - } - - // 监听初始化结果 - initializeOperation.Completed += InitializeOperation_Completed; - return initializeOperation; - } - private static void InitializeOperation_Completed(AsyncOperationBase op) - { - _initializeStatus = op.Status; - _initializeError = op.Error; - } - - /// - /// 向网络端请求静态资源版本 - /// - /// 更新的资源包裹名称 - /// 超时时间(默认值:60秒) - public static UpdateStaticVersionOperation UpdateStaticVersionAsync(string packageName, int timeout = 60) - { - DebugCheckInitialize(); - if (_playMode == EPlayMode.EditorSimulateMode) - { - var operation = new EditorPlayModeUpdateStaticVersionOperation(); - OperationSystem.StartOperation(operation); - return operation; - } - else if (_playMode == EPlayMode.OfflinePlayMode) - { - var operation = new OfflinePlayModeUpdateStaticVersionOperation(); - OperationSystem.StartOperation(operation); - return operation; - } - else if (_playMode == EPlayMode.HostPlayMode) - { - return _hostPlayModeImpl.UpdateStaticVersionAsync(packageName, timeout); - } - else - { - throw new NotImplementedException(); + // 初始化异步系统 + OperationSystem.Initialize(); } } /// - /// 向网络端请求并更新补丁清单 + /// 更新 /// - /// 更新的资源包裹名称 - /// 更新的资源包裹版本 - /// 超时时间(默认值:60秒) - public static UpdateManifestOperation UpdateManifestAsync(string packageName, string packageCRC, int timeout = 60) + internal static void Update() { - DebugCheckInitialize(); - DebugCheckUpdateManifest(); - if (_playMode == EPlayMode.EditorSimulateMode) + if (_isInitialize) { - var operation = new EditorPlayModeUpdateManifestOperation(); - OperationSystem.StartOperation(operation); - return operation; - } - else if (_playMode == EPlayMode.OfflinePlayMode) - { - var operation = new OfflinePlayModeUpdateManifestOperation(); - OperationSystem.StartOperation(operation); - return operation; - } - else if (_playMode == EPlayMode.HostPlayMode) - { - return _hostPlayModeImpl.UpdatePatchManifestAsync(packageName, packageCRC, timeout); - } - else - { - throw new NotImplementedException(); + OperationSystem.Update(); + DownloadSystem.Update(); + + foreach (var package in _packages) + { + package.UpdatePackage(); + } } } /// - /// 弱联网情况下加载补丁清单 - /// 注意:当指定版本内容验证失败后会返回失败。 + /// 销毁 /// - /// 指定的资源包裹名称 - /// 指定的资源包裹版本 - public static UpdateManifestOperation WeaklyUpdateManifestAsync(string packageName, string packageCRC) + internal static void Destroy() { - DebugCheckInitialize(); - if (_playMode == EPlayMode.EditorSimulateMode) + if (_isInitialize) { - var operation = new EditorPlayModeUpdateManifestOperation(); - OperationSystem.StartOperation(operation); - return operation; + OperationSystem.DestroyAll(); + DownloadSystem.DestroyAll(); + CacheSystem.ClearAll(); + + foreach (var package in _packages) + { + package.DestroyPackage(); + } + _packages.Clear(); + + _isInitialize = false; + YooLogger.Log("YooAssets destroy all !"); } - else if (_playMode == EPlayMode.OfflinePlayMode) + } + + + /// + /// 创建资源包 + /// + /// 资源包名称 + public static YooAssetPackage CreateAssetPackage(string packageName) + { + if (_isInitialize == false) + throw new Exception("YooAssets not initialize !"); + + if (string.IsNullOrEmpty(packageName)) + throw new Exception("PackageName is null or empty !"); + + if (HasAssetPackage(packageName)) + throw new Exception($"Package {packageName} already existed !"); + + YooAssetPackage component = new YooAssetPackage(packageName); + _packages.Add(component); + return component; + } + + /// + /// 检测资源包是否存在 + /// + /// 资源包名称 + public static bool HasAssetPackage(string packageName) + { + foreach (var package in _packages) { - var operation = new OfflinePlayModeUpdateManifestOperation(); - OperationSystem.StartOperation(operation); - return operation; - } - else if (_playMode == EPlayMode.HostPlayMode) - { - return _hostPlayModeImpl.WeaklyUpdatePatchManifestAsync(packageName, packageCRC); - } - else - { - throw new NotImplementedException(); + if (package.PackageName == packageName) + return true; } + return false; } /// @@ -381,637 +119,34 @@ namespace YooAsset OperationSystem.StartOperation(operation); } + #region 系统参数 /// - /// 资源回收(卸载引用计数为零的资源) + /// 启用下载系统的断点续传功能的文件大小 /// - public static void UnloadUnusedAssets() + public static void SetDownloadSystemBreakpointResumeFileSize(int fileBytes) { - if (_isInitialize) - { - AssetSystem.Update(); - AssetSystem.UnloadUnusedAssets(); - } + DownloadSystem.BreakpointResumeFileSize = fileBytes; } /// - /// 强制回收所有资源 + /// 设置异步系统的每帧允许运行的最大时间切片(单位:毫秒) /// - public static void ForceUnloadAllAssets() + public static void SetOperationSystemMaxTimeSlice(long milliseconds) { - if (_isInitialize) + if (milliseconds < 30) { - AssetSystem.ForceUnloadAllAssets(); + milliseconds = 30; + YooLogger.Warning($"MaxTimeSlice minimum value is 30 milliseconds."); } - } - - - #region 资源信息 - /// - /// 是否需要从远端更新下载 - /// - /// 资源的定位地址 - public static bool IsNeedDownloadFromRemote(string location) - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null); - if (assetInfo.IsInvalid) - return false; - - BundleInfo bundleInfo = _bundleServices.GetBundleInfo(assetInfo); - if (bundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote) - return true; - else - return false; + OperationSystem.MaxTimeSlice = milliseconds; } /// - /// 是否需要从远端更新下载 + /// 设置缓存系统的已经缓存文件的校验等级 /// - /// 资源的定位地址 - public static bool IsNeedDownloadFromRemote(AssetInfo assetInfo) + public static void SetCacheSystemCachedFileVerifyLevel(EVerifyLevel verifyLevel) { - DebugCheckInitialize(); - if (assetInfo.IsInvalid) - { - YooLogger.Warning(assetInfo.Error); - return false; - } - - BundleInfo bundleInfo = _bundleServices.GetBundleInfo(assetInfo); - if (bundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote) - return true; - else - return false; - } - - /// - /// 获取资源信息列表 - /// - /// 资源标签 - public static AssetInfo[] GetAssetInfos(string tag) - { - DebugCheckInitialize(); - string[] tags = new string[] { tag }; - return _bundleServices.GetAssetInfos(tags); - } - - /// - /// 获取资源信息列表 - /// - /// 资源标签列表 - public static AssetInfo[] GetAssetInfos(string[] tags) - { - DebugCheckInitialize(); - return _bundleServices.GetAssetInfos(tags); - } - - /// - /// 获取资源信息 - /// - /// 资源的定位地址 - public static AssetInfo GetAssetInfo(string location) - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null); - return assetInfo; - } - - /// - /// 获取资源路径 - /// - /// 资源的定位地址 - /// 如果location地址无效,则返回空字符串 - public static string GetAssetPath(string location) - { - DebugCheckInitialize(); - return _locationServices.ConvertLocationToAssetPath(location); - } - #endregion - - #region 原生文件 - /// - /// 异步获取原生文件 - /// - /// 资源的定位地址 - /// 拷贝路径 - public static RawFileOperation GetRawFileAsync(string location, string copyPath = null) - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null); - return GetRawFileInternal(assetInfo, copyPath); - } - - /// - /// 异步获取原生文件 - /// - /// 资源信息 - /// 拷贝路径 - public static RawFileOperation GetRawFileAsync(AssetInfo assetInfo, string copyPath = null) - { - DebugCheckInitialize(); - if (assetInfo.IsInvalid) - YooLogger.Warning(assetInfo.Error); - return GetRawFileInternal(assetInfo, copyPath); - } - - - private static RawFileOperation GetRawFileInternal(AssetInfo assetInfo, string copyPath) - { - if (assetInfo.IsInvalid) - { - RawFileOperation operation = new CompletedRawFileOperation(assetInfo.Error, copyPath); - OperationSystem.StartOperation(operation); - return operation; - } - - BundleInfo bundleInfo = _bundleServices.GetBundleInfo(assetInfo); - -#if UNITY_EDITOR - if (bundleInfo.Bundle.IsRawFile == false) - { - string error = $"Cannot load asset bundle file using {nameof(GetRawFileAsync)} method !"; - YooLogger.Error(error); - RawFileOperation operation = new CompletedRawFileOperation(error, copyPath); - OperationSystem.StartOperation(operation); - return operation; - } -#endif - - if (_playMode == EPlayMode.EditorSimulateMode) - { - RawFileOperation operation = new EditorPlayModeRawFileOperation(bundleInfo, copyPath); - OperationSystem.StartOperation(operation); - return operation; - } - else if (_playMode == EPlayMode.OfflinePlayMode) - { - RawFileOperation operation = new OfflinePlayModeRawFileOperation(bundleInfo, copyPath); - OperationSystem.StartOperation(operation); - return operation; - } - else if (_playMode == EPlayMode.HostPlayMode) - { - RawFileOperation operation = new HostPlayModeRawFileOperation(bundleInfo, copyPath); - OperationSystem.StartOperation(operation); - return operation; - } - else - { - throw new NotImplementedException(); - } - } - #endregion - - #region 场景加载 - /// - /// 异步加载场景 - /// - /// 场景的定位地址 - /// 场景加载模式 - /// 加载完毕时是否主动激活 - /// 优先级 - public static SceneOperationHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool activateOnLoad = true, int priority = 100) - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null); - var handle = AssetSystem.LoadSceneAsync(assetInfo, sceneMode, activateOnLoad, priority); - return handle; - } - - /// - /// 异步加载场景 - /// - /// 场景的资源信息 - /// 场景加载模式 - /// 加载完毕时是否主动激活 - /// 优先级 - public static SceneOperationHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool activateOnLoad = true, int priority = 100) - { - DebugCheckInitialize(); - if (assetInfo.IsInvalid) - YooLogger.Warning(assetInfo.Error); - var handle = AssetSystem.LoadSceneAsync(assetInfo, sceneMode, activateOnLoad, priority); - return handle; - } - #endregion - - #region 资源加载 - /// - /// 同步加载资源对象 - /// - /// 资源信息 - public static AssetOperationHandle LoadAssetSync(AssetInfo assetInfo) - { - DebugCheckInitialize(); - if (assetInfo.IsInvalid) - YooLogger.Warning(assetInfo.Error); - return LoadAssetInternal(assetInfo, true); - } - - /// - /// 同步加载资源对象 - /// - /// 资源类型 - /// 资源的定位地址 - public static AssetOperationHandle LoadAssetSync(string location) where TObject : UnityEngine.Object - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject)); - return LoadAssetInternal(assetInfo, true); - } - - /// - /// 同步加载资源对象 - /// - /// 资源的定位地址 - /// 资源类型 - public static AssetOperationHandle LoadAssetSync(string location, System.Type type) - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); - return LoadAssetInternal(assetInfo, true); - } - - - /// - /// 异步加载资源对象 - /// - /// 资源信息 - public static AssetOperationHandle LoadAssetAsync(AssetInfo assetInfo) - { - DebugCheckInitialize(); - if (assetInfo.IsInvalid) - YooLogger.Warning(assetInfo.Error); - return LoadAssetInternal(assetInfo, false); - } - - /// - /// 异步加载资源对象 - /// - /// 资源类型 - /// 资源的定位地址 - public static AssetOperationHandle LoadAssetAsync(string location) where TObject : UnityEngine.Object - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject)); - return LoadAssetInternal(assetInfo, false); - } - - /// - /// 异步加载资源对象 - /// - /// 资源的定位地址 - /// 资源类型 - public static AssetOperationHandle LoadAssetAsync(string location, System.Type type) - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); - return LoadAssetInternal(assetInfo, false); - } - - - private static AssetOperationHandle LoadAssetInternal(AssetInfo assetInfo, bool waitForAsyncComplete) - { -#if UNITY_EDITOR - BundleInfo bundleInfo = _bundleServices.GetBundleInfo(assetInfo); - if (bundleInfo.Bundle.IsRawFile) - { - string error = $"Cannot load raw file using LoadAsset method !"; - YooLogger.Error(error); - CompletedProvider completedProvider = new CompletedProvider(assetInfo); - completedProvider.SetCompleted(error); - return completedProvider.CreateHandle(); - } -#endif - - var handle = AssetSystem.LoadAssetAsync(assetInfo); - if (waitForAsyncComplete) - handle.WaitForAsyncComplete(); - return handle; - } - #endregion - - #region 资源加载 - /// - /// 同步加载子资源对象 - /// - /// 资源信息 - public static SubAssetsOperationHandle LoadSubAssetsSync(AssetInfo assetInfo) - { - DebugCheckInitialize(); - if (assetInfo.IsInvalid) - YooLogger.Warning(assetInfo.Error); - return LoadSubAssetsInternal(assetInfo, true); - } - - /// - /// 同步加载子资源对象 - /// - /// 资源类型 - /// 资源的定位地址 - public static SubAssetsOperationHandle LoadSubAssetsSync(string location) where TObject : UnityEngine.Object - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject)); - return LoadSubAssetsInternal(assetInfo, true); - } - - /// - /// 同步加载子资源对象 - /// - /// 资源的定位地址 - /// 子对象类型 - public static SubAssetsOperationHandle LoadSubAssetsSync(string location, System.Type type) - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); - return LoadSubAssetsInternal(assetInfo, true); - } - - - /// - /// 异步加载子资源对象 - /// - /// 资源信息 - public static SubAssetsOperationHandle LoadSubAssetsAsync(AssetInfo assetInfo) - { - DebugCheckInitialize(); - if (assetInfo.IsInvalid) - YooLogger.Warning(assetInfo.Error); - return LoadSubAssetsInternal(assetInfo, false); - } - - /// - /// 异步加载子资源对象 - /// - /// 资源类型 - /// 资源的定位地址 - public static SubAssetsOperationHandle LoadSubAssetsAsync(string location) where TObject : UnityEngine.Object - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject)); - return LoadSubAssetsInternal(assetInfo, false); - } - - /// - /// 异步加载子资源对象 - /// - /// 资源的定位地址 - /// 子对象类型 - public static SubAssetsOperationHandle LoadSubAssetsAsync(string location, System.Type type) - { - DebugCheckInitialize(); - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type); - return LoadSubAssetsInternal(assetInfo, false); - } - - - private static SubAssetsOperationHandle LoadSubAssetsInternal(AssetInfo assetInfo, bool waitForAsyncComplete) - { -#if UNITY_EDITOR - BundleInfo bundleInfo = _bundleServices.GetBundleInfo(assetInfo); - if (bundleInfo.Bundle.IsRawFile) - { - string error = $"Cannot load raw file using LoadSubAssets method !"; - YooLogger.Error(error); - CompletedProvider completedProvider = new CompletedProvider(assetInfo); - completedProvider.SetCompleted(error); - return completedProvider.CreateHandle(); - } -#endif - - var handle = AssetSystem.LoadSubAssetsAsync(assetInfo); - if (waitForAsyncComplete) - handle.WaitForAsyncComplete(); - return handle; - } - #endregion - - #region 资源下载 - /// - /// 创建补丁下载器,用于下载更新资源标签指定的资源包文件 - /// - /// 资源标签 - /// 同时下载的最大文件数 - /// 下载失败的重试次数 - public static PatchDownloaderOperation CreatePatchDownloader(string tag, int downloadingMaxNumber, int failedTryAgain) - { - DebugCheckInitialize(); - return CreatePatchDownloader(new string[] { tag }, downloadingMaxNumber, failedTryAgain); - } - - /// - /// 创建补丁下载器,用于下载更新资源标签指定的资源包文件 - /// - /// 资源标签列表 - /// 同时下载的最大文件数 - /// 下载失败的重试次数 - public static PatchDownloaderOperation CreatePatchDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain) - { - DebugCheckInitialize(); - if (_playMode == EPlayMode.EditorSimulateMode || _playMode == EPlayMode.OfflinePlayMode) - { - List downloadList = new List(); - var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain); - return operation; - } - else if (_playMode == EPlayMode.HostPlayMode) - { - return _hostPlayModeImpl.CreatePatchDownloaderByTags(tags, downloadingMaxNumber, failedTryAgain); - } - else - { - throw new NotImplementedException(); - } - } - - /// - /// 创建补丁下载器,用于下载更新当前资源版本所有的资源包文件 - /// - /// 同时下载的最大文件数 - /// 下载失败的重试次数 - public static PatchDownloaderOperation CreatePatchDownloader(int downloadingMaxNumber, int failedTryAgain) - { - DebugCheckInitialize(); - if (_playMode == EPlayMode.EditorSimulateMode || _playMode == EPlayMode.OfflinePlayMode) - { - List downloadList = new List(); - var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain); - return operation; - } - else if (_playMode == EPlayMode.HostPlayMode) - { - return _hostPlayModeImpl.CreatePatchDownloaderByAll(downloadingMaxNumber, failedTryAgain); - } - else - { - throw new NotImplementedException(); - } - } - - - /// - /// 创建补丁下载器,用于下载更新指定的资源列表依赖的资源包文件 - /// - /// 资源定位列表 - /// 同时下载的最大文件数 - /// 下载失败的重试次数 - public static PatchDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain) - { - DebugCheckInitialize(); - if (_playMode == EPlayMode.EditorSimulateMode || _playMode == EPlayMode.OfflinePlayMode) - { - List downloadList = new List(); - var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain); - return operation; - } - else if (_playMode == EPlayMode.HostPlayMode) - { - List assetInfos = new List(locations.Length); - foreach (var location in locations) - { - AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null); - assetInfos.Add(assetInfo); - } - return _hostPlayModeImpl.CreatePatchDownloaderByPaths(assetInfos.ToArray(), downloadingMaxNumber, failedTryAgain); - } - else - { - throw new NotImplementedException(); - } - } - - /// - /// 创建补丁下载器,用于下载更新指定的资源列表依赖的资源包文件 - /// - /// 资源信息列表 - /// 同时下载的最大文件数 - /// 下载失败的重试次数 - public static PatchDownloaderOperation CreateBundleDownloader(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain) - { - DebugCheckInitialize(); - if (_playMode == EPlayMode.EditorSimulateMode || _playMode == EPlayMode.OfflinePlayMode) - { - List downloadList = new List(); - var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain); - return operation; - } - else if (_playMode == EPlayMode.HostPlayMode) - { - return _hostPlayModeImpl.CreatePatchDownloaderByPaths(assetInfos, downloadingMaxNumber, failedTryAgain); - } - else - { - throw new NotImplementedException(); - } - } - #endregion - - #region 资源解压 - /// - /// 创建补丁解压器 - /// - /// 资源标签 - /// 同时解压的最大文件数 - /// 解压失败的重试次数 - public static PatchUnpackerOperation CreatePatchUnpacker(string tag, int unpackingMaxNumber, int failedTryAgain) - { - DebugCheckInitialize(); - return CreatePatchUnpacker(new string[] { tag }, unpackingMaxNumber, failedTryAgain); - } - - /// - /// 创建补丁解压器 - /// - /// 资源标签列表 - /// 同时解压的最大文件数 - /// 解压失败的重试次数 - public static PatchUnpackerOperation CreatePatchUnpacker(string[] tags, int unpackingMaxNumber, int failedTryAgain) - { - DebugCheckInitialize(); - if (_playMode == EPlayMode.EditorSimulateMode) - { - List downloadList = new List(); - var operation = new PatchUnpackerOperation(downloadList, unpackingMaxNumber, failedTryAgain); - return operation; - } - else if (_playMode == EPlayMode.OfflinePlayMode) - { - List downloadList = new List(); - var operation = new PatchUnpackerOperation(downloadList, unpackingMaxNumber, failedTryAgain); - return operation; - } - else if (_playMode == EPlayMode.HostPlayMode) - { - return _hostPlayModeImpl.CreatePatchUnpackerByTags(tags, unpackingMaxNumber, failedTryAgain); - } - else - { - throw new NotImplementedException(); - } - } - - /// - /// 创建补丁解压器 - /// - /// 同时解压的最大文件数 - /// 解压失败的重试次数 - public static PatchUnpackerOperation CreatePatchUnpacker(int unpackingMaxNumber, int failedTryAgain) - { - DebugCheckInitialize(); - if (_playMode == EPlayMode.EditorSimulateMode) - { - List downloadList = new List(); - var operation = new PatchUnpackerOperation(downloadList, unpackingMaxNumber, failedTryAgain); - return operation; - } - else if (_playMode == EPlayMode.OfflinePlayMode) - { - List downloadList = new List(); - var operation = new PatchUnpackerOperation(downloadList, unpackingMaxNumber, failedTryAgain); - return operation; - } - else if (_playMode == EPlayMode.HostPlayMode) - { - return _hostPlayModeImpl.CreatePatchUnpackerByAll(unpackingMaxNumber, failedTryAgain); - } - else - { - throw new NotImplementedException(); - } - } - #endregion - - #region 包裹更新 - /// - /// 创建资源包裹下载器,用于下载更新指定资源版本所有的资源包文件 - /// - /// 指定更新的资源包裹名称 - /// 指定更新的资源包裹版本 - /// 超时时间 - public static UpdatePackageOperation UpdatePackageAsync(string packageName, string packageCRC, int timeout = 60) - { - DebugCheckInitialize(); - if (_playMode == EPlayMode.EditorSimulateMode) - { - var operation = new EditorPlayModeUpdatePackageOperation(); - OperationSystem.StartOperation(operation); - return operation; - } - else if (_playMode == EPlayMode.OfflinePlayMode) - { - var operation = new OfflinePlayModeUpdatePackageOperation(); - OperationSystem.StartOperation(operation); - return operation; - } - else if (_playMode == EPlayMode.HostPlayMode) - { - return _hostPlayModeImpl.UpdatePackageAsync(packageName, packageCRC, timeout); - } - else - { - throw new NotImplementedException(); - } + CacheSystem.InitVerifyLevel = verifyLevel; } #endregion @@ -1039,141 +174,23 @@ namespace YooAsset { SandboxHelper.DeleteCacheFolder(); } - - /// - /// 清空未被使用的缓存文件 - /// - public static ClearUnusedCacheFilesOperation ClearUnusedCacheFiles() - { - DebugCheckInitialize(); - if (_playMode == EPlayMode.EditorSimulateMode) - { - var operation = new EditorPlayModeClearUnusedCacheFilesOperation(); - OperationSystem.StartOperation(operation); - return operation; - } - else if (_playMode == EPlayMode.OfflinePlayMode) - { - var operation = new OfflinePlayModeClearUnusedCacheFilesOperation(); - OperationSystem.StartOperation(operation); - return operation; - } - else if (_playMode == EPlayMode.HostPlayMode) - { - var operation = new HostPlayModeClearUnusedCacheFilesOperation(_hostPlayModeImpl); - OperationSystem.StartOperation(operation); - return operation; - } - else - { - throw new NotImplementedException(); - } - } #endregion - #region 内部方法 - internal static void InternalDestroy() + #region 调试信息 + internal static DebugReport GetDebugReport() { - if (_isInitialize) + DebugReport report = new DebugReport(); + report.FrameCount = Time.frameCount; + + foreach (var package in _packages) { - _isInitialize = false; - _initializeError = string.Empty; - _initializeStatus = EOperationStatus.None; - - _bundleServices = null; - _locationServices = null; - _editorSimulateModeImpl = null; - _offlinePlayModeImpl = null; - _hostPlayModeImpl = null; - - OperationSystem.DestroyAll(); - DownloadSystem.DestroyAll(); - CacheSystem.DestroyAll(); - AssetSystem.DestroyAll(); - YooLogger.Log("YooAssets destroy all !"); + var result = package.GetDebugReportInfos(); + report.ProviderInfos.AddRange(result); } - } - internal static void InternalUpdate() - { - OperationSystem.Update(); - DownloadSystem.Update(); - AssetSystem.Update(); - } - /// - /// 资源定位地址转换为资源完整路径 - /// - internal static string MappingToAssetPath(string location) - { - return _bundleServices.MappingToAssetPath(location); - } - #endregion - - #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) == false) - { - // 检查路径末尾是否有空格 - int index = location.LastIndexOf(" "); - if (index != -1) - { - if (location.Length == index + 1) - YooLogger.Warning($"Found blank character in location : \"{location}\""); - } - - if (location.IndexOfAny(System.IO.Path.GetInvalidPathChars()) >= 0) - YooLogger.Warning($"Found illegal character in location : \"{location}\""); - } - } - - [Conditional("DEBUG")] - private static void DebugCheckUpdateManifest() - { - var loadedBundleInfos = AssetSystem.GetLoadedBundleInfos(); - if (loadedBundleInfos.Count > 0) - { - YooLogger.Warning($"Found loaded bundle before update manifest ! Recommended to call the {nameof(ForceUnloadAllAssets)} method to release loaded bundle !"); - } - } - #endregion - - #region 私有方法 - /// - /// 资源定位地址转换为资源信息类,失败时内部会发出错误日志。 - /// - /// 如果转换失败会返回一个无效的资源信息类 - private static AssetInfo ConvertLocationToAssetInfo(string location, System.Type assetType) - { - DebugCheckLocation(location); - string assetPath = _locationServices.ConvertLocationToAssetPath(location); - PatchAsset patchAsset = _bundleServices.TryGetPatchAsset(assetPath); - if (patchAsset != null) - { - AssetInfo assetInfo = new AssetInfo(patchAsset, assetType); - return assetInfo; - } - else - { - string error; - if (string.IsNullOrEmpty(location)) - error = $"The location is null or empty !"; - else - error = $"The location is invalid : {location}"; - YooLogger.Error(error); - AssetInfo assetInfo = new AssetInfo(error); - return assetInfo; - } + // 重新排序 + report.ProviderInfos.Sort(); + return report; } #endregion } diff --git a/Assets/YooAsset/Runtime/YooAssetsExtension.cs b/Assets/YooAsset/Runtime/YooAssetsExtension.cs new file mode 100644 index 0000000..5e7e34e --- /dev/null +++ b/Assets/YooAsset/Runtime/YooAssetsExtension.cs @@ -0,0 +1,409 @@ +using System; +using System.Diagnostics; +using System.Collections; +using System.Collections.Generic; +using UnityEngine.SceneManagement; + +namespace YooAsset +{ + public static partial class YooAssets + { + private static YooAssetPackage _mainPackage; + + /// + /// 是否已经初始化 + /// + public static bool IsInitialized + { + get { return _mainPackage.IsInitialized; } + } + + /// + /// 异步初始化 + /// + public static InitializationOperation InitializeAsync(string packageName, InitializeParameters parameters) + { + 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 资源信息 + /// + /// 是否需要从远端更新下载 + /// + /// 资源的定位地址 + public static bool IsNeedDownloadFromRemote(string location) + { + return _mainPackage.IsNeedDownloadFromRemote(location); + } + + /// + /// 是否需要从远端更新下载 + /// + /// 资源的定位地址 + public static bool IsNeedDownloadFromRemote(AssetInfo assetInfo) + { + return _mainPackage.IsNeedDownloadFromRemote(assetInfo); + } + + /// + /// 获取资源信息列表 + /// + /// 资源标签 + public static AssetInfo[] GetAssetInfos(string tag) + { + return _mainPackage.GetAssetInfos(tag); + } + + /// + /// 获取资源信息列表 + /// + /// 资源标签列表 + public static AssetInfo[] GetAssetInfos(string[] tags) + { + return _mainPackage.GetAssetInfos(tags); + } + + /// + /// 获取资源信息 + /// + /// 资源的定位地址 + public static AssetInfo GetAssetInfo(string location) + { + return _mainPackage.GetAssetInfo(location); + } + + /// + /// 获取资源路径 + /// + /// 资源的定位地址 + /// 如果location地址无效,则返回空字符串 + public static string GetAssetPath(string location) + { + return _mainPackage.GetAssetPath(location); + } + #endregion + + #region 原生文件 + /// + /// 异步获取原生文件 + /// + /// 资源的定位地址 + /// 拷贝路径 + public static RawFileOperation GetRawFileAsync(string location, string copyPath = null) + { + return _mainPackage.GetRawFileAsync(location, copyPath); + } + + /// + /// 异步获取原生文件 + /// + /// 资源信息 + /// 拷贝路径 + public static RawFileOperation GetRawFileAsync(AssetInfo assetInfo, string copyPath = null) + { + return _mainPackage.GetRawFileAsync(assetInfo, copyPath); + } + #endregion + + #region 场景加载 + /// + /// 异步加载场景 + /// + /// 场景的定位地址 + /// 场景加载模式 + /// 加载完毕时是否主动激活 + /// 优先级 + public static SceneOperationHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool activateOnLoad = true, int priority = 100) + { + return _mainPackage.LoadSceneAsync(location, sceneMode, activateOnLoad, priority); + } + + /// + /// 异步加载场景 + /// + /// 场景的资源信息 + /// 场景加载模式 + /// 加载完毕时是否主动激活 + /// 优先级 + public static SceneOperationHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool activateOnLoad = true, int priority = 100) + { + return _mainPackage.LoadSceneAsync(assetInfo, sceneMode, activateOnLoad, priority); + } + #endregion + + #region 资源加载 + /// + /// 同步加载资源对象 + /// + /// 资源信息 + public static AssetOperationHandle LoadAssetSync(AssetInfo assetInfo) + { + return _mainPackage.LoadAssetSync(assetInfo); + } + + /// + /// 同步加载资源对象 + /// + /// 资源类型 + /// 资源的定位地址 + public static AssetOperationHandle LoadAssetSync(string location) where TObject : UnityEngine.Object + { + return _mainPackage.LoadAssetSync(location); + } + + /// + /// 同步加载资源对象 + /// + /// 资源的定位地址 + /// 资源类型 + public static AssetOperationHandle LoadAssetSync(string location, System.Type type) + { + return _mainPackage.LoadAssetSync(location, type); + } + + + /// + /// 异步加载资源对象 + /// + /// 资源信息 + public static AssetOperationHandle LoadAssetAsync(AssetInfo assetInfo) + { + return _mainPackage.LoadAssetAsync(assetInfo); + } + + /// + /// 异步加载资源对象 + /// + /// 资源类型 + /// 资源的定位地址 + public static AssetOperationHandle LoadAssetAsync(string location) where TObject : UnityEngine.Object + { + return _mainPackage.LoadAssetAsync(location); + } + + /// + /// 异步加载资源对象 + /// + /// 资源的定位地址 + /// 资源类型 + public static AssetOperationHandle LoadAssetAsync(string location, System.Type type) + { + return _mainPackage.LoadAssetAsync(location, type); + } + #endregion + + #region 资源加载 + /// + /// 同步加载子资源对象 + /// + /// 资源信息 + public static SubAssetsOperationHandle LoadSubAssetsSync(AssetInfo assetInfo) + { + return _mainPackage.LoadSubAssetsSync(assetInfo); + } + + /// + /// 同步加载子资源对象 + /// + /// 资源类型 + /// 资源的定位地址 + public static SubAssetsOperationHandle LoadSubAssetsSync(string location) where TObject : UnityEngine.Object + { + return _mainPackage.LoadSubAssetsSync(location); + } + + /// + /// 同步加载子资源对象 + /// + /// 资源的定位地址 + /// 子对象类型 + public static SubAssetsOperationHandle LoadSubAssetsSync(string location, System.Type type) + { + return _mainPackage.LoadSubAssetsSync(location, type); + } + + + /// + /// 异步加载子资源对象 + /// + /// 资源信息 + public static SubAssetsOperationHandle LoadSubAssetsAsync(AssetInfo assetInfo) + { + return _mainPackage.LoadSubAssetsAsync(assetInfo); + } + + /// + /// 异步加载子资源对象 + /// + /// 资源类型 + /// 资源的定位地址 + public static SubAssetsOperationHandle LoadSubAssetsAsync(string location) where TObject : UnityEngine.Object + { + return _mainPackage.LoadSubAssetsAsync(location); + } + + /// + /// 异步加载子资源对象 + /// + /// 资源的定位地址 + /// 子对象类型 + public static SubAssetsOperationHandle LoadSubAssetsAsync(string location, System.Type type) + { + return _mainPackage.LoadSubAssetsAsync(location, type); + } + #endregion + + #region 资源下载 + /// + /// 创建补丁下载器,用于下载更新资源标签指定的资源包文件 + /// + /// 资源标签 + /// 同时下载的最大文件数 + /// 下载失败的重试次数 + public static PatchDownloaderOperation CreatePatchDownloader(string tag, int downloadingMaxNumber, int failedTryAgain) + { + return _mainPackage.CreatePatchDownloader(new string[] { tag }, downloadingMaxNumber, failedTryAgain); + } + + /// + /// 创建补丁下载器,用于下载更新资源标签指定的资源包文件 + /// + /// 资源标签列表 + /// 同时下载的最大文件数 + /// 下载失败的重试次数 + public static PatchDownloaderOperation CreatePatchDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain) + { + return _mainPackage.CreatePatchDownloader(tags, downloadingMaxNumber, failedTryAgain); + } + + /// + /// 创建补丁下载器,用于下载更新当前资源版本所有的资源包文件 + /// + /// 同时下载的最大文件数 + /// 下载失败的重试次数 + public static PatchDownloaderOperation CreatePatchDownloader(int downloadingMaxNumber, int failedTryAgain) + { + return _mainPackage.CreatePatchDownloader(downloadingMaxNumber, failedTryAgain); + } + + + /// + /// 创建补丁下载器,用于下载更新指定的资源列表依赖的资源包文件 + /// + /// 资源定位列表 + /// 同时下载的最大文件数 + /// 下载失败的重试次数 + public static PatchDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain) + { + return _mainPackage.CreateBundleDownloader(locations, downloadingMaxNumber, failedTryAgain); + } + + /// + /// 创建补丁下载器,用于下载更新指定的资源列表依赖的资源包文件 + /// + /// 资源信息列表 + /// 同时下载的最大文件数 + /// 下载失败的重试次数 + public static PatchDownloaderOperation CreateBundleDownloader(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain) + { + return _mainPackage.CreateBundleDownloader(assetInfos, downloadingMaxNumber, failedTryAgain); + } + #endregion + + #region 资源解压 + /// + /// 创建补丁解压器 + /// + /// 资源标签 + /// 同时解压的最大文件数 + /// 解压失败的重试次数 + public static PatchUnpackerOperation CreatePatchUnpacker(string tag, int unpackingMaxNumber, int failedTryAgain) + { + return _mainPackage.CreatePatchUnpacker(tag, unpackingMaxNumber, failedTryAgain); + } + + /// + /// 创建补丁解压器 + /// + /// 资源标签列表 + /// 同时解压的最大文件数 + /// 解压失败的重试次数 + public static PatchUnpackerOperation CreatePatchUnpacker(string[] tags, int unpackingMaxNumber, int failedTryAgain) + { + return _mainPackage.CreatePatchUnpacker(tags, unpackingMaxNumber, failedTryAgain); + } + + /// + /// 创建补丁解压器 + /// + /// 同时解压的最大文件数 + /// 解压失败的重试次数 + public static PatchUnpackerOperation CreatePatchUnpacker(int unpackingMaxNumber, int failedTryAgain) + { + return _mainPackage.CreatePatchUnpacker(unpackingMaxNumber, failedTryAgain); + } + #endregion + + #region 包裹更新 + /// + /// 创建资源包裹下载器,用于下载更新指定资源版本所有的资源包文件 + /// + /// 指定更新的资源包裹版本 + /// 超时时间 + public static UpdatePackageOperation UpdatePackageAsync(string packageCRC, int timeout = 60) + { + return _mainPackage.UpdatePackageAsync(packageCRC, timeout); + } + #endregion + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/ClearUnusedCacheFilesOperation.cs.meta b/Assets/YooAsset/Runtime/YooAssetsExtension.cs.meta similarity index 83% rename from Assets/YooAsset/Runtime/PatchSystem/Operations/ClearUnusedCacheFilesOperation.cs.meta rename to Assets/YooAsset/Runtime/YooAssetsExtension.cs.meta index 41bfef3..68145ca 100644 --- a/Assets/YooAsset/Runtime/PatchSystem/Operations/ClearUnusedCacheFilesOperation.cs.meta +++ b/Assets/YooAsset/Runtime/YooAssetsExtension.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 5d188c50fd00bf941b2eeebb374dc0d1 +guid: 2db868c2aaaee8e42a7f035117e747c4 MonoImporter: externalObjects: {} serializedVersion: 2