12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178 |
- using System;
- using System.Diagnostics;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine.SceneManagement;
- namespace YooAsset
- {
- public static partial class YooAssets
- {
- /// <summary>
- /// 运行模式
- /// </summary>
- public enum EPlayMode
- {
- /// <summary>
- /// 编辑器下的模拟模式
- /// 注意:在初始化的时候自动构建真机模拟环境。
- /// </summary>
- EditorSimulateMode,
- /// <summary>
- /// 离线运行模式
- /// </summary>
- OfflinePlayMode,
- /// <summary>
- /// 联机运行模式
- /// </summary>
- HostPlayMode,
- }
- /// <summary>
- /// 初始化参数
- /// </summary>
- public abstract class InitializeParameters
- {
- /// <summary>
- /// 资源定位地址大小写不敏感
- /// </summary>
- public bool LocationToLower = false;
- /// <summary>
- /// 资源定位服务接口
- /// </summary>
- public ILocationServices LocationServices = null;
- /// <summary>
- /// 文件解密服务接口
- /// </summary>
- public IDecryptionServices DecryptionServices = null;
- /// <summary>
- /// 资源加载的最大数量
- /// </summary>
- public int AssetLoadingMaxNumber = int.MaxValue;
- /// <summary>
- /// 异步操作系统每帧允许运行的最大时间切片(单位:毫秒)
- /// </summary>
- public long OperationSystemMaxTimeSlice = long.MaxValue;
- }
- /// <summary>
- /// 编辑器下模拟运行模式的初始化参数
- /// </summary>
- public class EditorSimulateModeParameters : InitializeParameters
- {
- /// <summary>
- /// 用于模拟运行的资源清单路径
- /// 注意:如果路径为空,会自动重新构建补丁清单。
- /// </summary>
- public string SimulatePatchManifestPath;
- }
- /// <summary>
- /// 离线运行模式的初始化参数
- /// </summary>
- public class OfflinePlayModeParameters : InitializeParameters
- {
- }
- /// <summary>
- /// 联机运行模式的初始化参数
- /// </summary>
- public class HostPlayModeParameters : InitializeParameters
- {
- /// <summary>
- /// 默认的资源服务器下载地址
- /// </summary>
- public string DefaultHostServer;
- /// <summary>
- /// 备用的资源服务器下载地址
- /// </summary>
- public string FallbackHostServer;
- /// <summary>
- /// 当缓存池被污染的时候清理缓存池
- /// </summary>
- public bool ClearCacheWhenDirty = false;
- #if UNITY_WEBGL
- /// <summary>
- /// WEBGL模式不支持多线程下载
- /// </summary>
- internal int BreakpointResumeFileSize = int.MaxValue;
- #else
- /// <summary>
- /// 启用断点续传功能的文件大小
- /// </summary>
- public int BreakpointResumeFileSize = int.MaxValue;
- #endif
- /// <summary>
- /// 下载文件校验等级
- /// </summary>
- public EVerifyLevel VerifyLevel = EVerifyLevel.High;
- }
- 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;
- /// <summary>
- /// 是否已经初始化
- /// </summary>
- public static bool IsInitialized
- {
- get { return _isInitialize; }
- }
- /// <summary>
- /// 异步初始化
- /// </summary>
- public static InitializationOperation InitializeAsync(InitializeParameters parameters)
- {
- if (parameters == null)
- throw new Exception($"YooAsset create parameters is null.");
- if (parameters.LocationServices == null)
- throw new Exception($"{nameof(IBundleServices)} is null.");
- else
- _locationServices = parameters.LocationServices;
- #if !UNITY_EDITOR
- if (parameters is EditorSimulateModeParameters)
- throw new Exception($"Editor simulate mode only support unity editor.");
- #endif
- // 创建驱动器
- if (_isInitialize == false)
- {
- _isInitialize = true;
- UnityEngine.GameObject driverGo = new UnityEngine.GameObject("[YooAsset]");
- driverGo.AddComponent<YooAssetDriver>();
- UnityEngine.Object.DontDestroyOnLoad(driverGo);
- #if DEBUG
- driverGo.AddComponent<RemoteDebuggerInRuntime>();
- #endif
- }
- else
- {
- throw new Exception("YooAsset is initialized yet.");
- }
- // 检测参数范围
- 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 (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();
- // 初始化异步操作系统
- 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 editorSimulateModeParameters = parameters as EditorSimulateModeParameters;
- initializeOperation = _editorSimulateModeImpl.InitializeAsync(
- editorSimulateModeParameters.LocationToLower,
- editorSimulateModeParameters.SimulatePatchManifestPath);
- }
- else if (_playMode == EPlayMode.OfflinePlayMode)
- {
- _offlinePlayModeImpl = new OfflinePlayModeImpl();
- _bundleServices = _offlinePlayModeImpl;
- AssetSystem.Initialize(false, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices);
- initializeOperation = _offlinePlayModeImpl.InitializeAsync(parameters.LocationToLower);
- }
- else if (_playMode == EPlayMode.HostPlayMode)
- {
- _hostPlayModeImpl = new HostPlayModeImpl();
- _bundleServices = _hostPlayModeImpl;
- AssetSystem.Initialize(false, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices);
- var hostPlayModeParameters = parameters as HostPlayModeParameters;
- initializeOperation = _hostPlayModeImpl.InitializeAsync(
- hostPlayModeParameters.LocationToLower,
- hostPlayModeParameters.ClearCacheWhenDirty,
- hostPlayModeParameters.DefaultHostServer,
- hostPlayModeParameters.FallbackHostServer);
- }
- else
- {
- throw new NotImplementedException();
- }
- // 监听初始化结果
- initializeOperation.Completed += InitializeOperation_Completed;
- return initializeOperation;
- }
- private static void InitializeOperation_Completed(AsyncOperationBase op)
- {
- _initializeStatus = op.Status;
- _initializeError = op.Error;
- }
- /// <summary>
- /// 向网络端请求静态资源版本
- /// </summary>
- /// <param name="timeout">超时时间(默认值:60秒)</param>
- public static UpdateStaticVersionOperation UpdateStaticVersionAsync(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(timeout);
- }
- else
- {
- throw new NotImplementedException();
- }
- }
- /// <summary>
- /// 向网络端请求并更新补丁清单
- /// </summary>
- /// <param name="resourceVersion">更新的资源版本</param>
- /// <param name="timeout">超时时间(默认值:60秒)</param>
- public static UpdateManifestOperation UpdateManifestAsync(int resourceVersion, int timeout = 60)
- {
- DebugCheckInitialize();
- DebugCheckUpdateManifest();
- if (_playMode == EPlayMode.EditorSimulateMode)
- {
- 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(resourceVersion, timeout);
- }
- else
- {
- throw new NotImplementedException();
- }
- }
- /// <summary>
- /// 弱联网情况下加载补丁清单
- /// 注意:当指定版本内容验证失败后会返回失败。
- /// </summary>
- /// <param name="resourceVersion">指定的资源版本</param>
- public static UpdateManifestOperation WeaklyUpdateManifestAsync(int resourceVersion)
- {
- DebugCheckInitialize();
- if (_playMode == EPlayMode.EditorSimulateMode)
- {
- 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.WeaklyUpdatePatchManifestAsync(resourceVersion);
- }
- else
- {
- throw new NotImplementedException();
- }
- }
- /// <summary>
- /// 开启一个异步操作
- /// </summary>
- /// <param name="operation">异步操作对象</param>
- public static void StartOperation(GameAsyncOperation operation)
- {
- OperationSystem.StartOperation(operation);
- }
- /// <summary>
- /// 获取资源版本号
- /// </summary>
- public static int GetResourceVersion()
- {
- DebugCheckInitialize();
- if (_playMode == EPlayMode.EditorSimulateMode)
- {
- return _editorSimulateModeImpl.GetResourceVersion();
- }
- else if (_playMode == EPlayMode.OfflinePlayMode)
- {
- return _offlinePlayModeImpl.GetResourceVersion();
- }
- else if (_playMode == EPlayMode.HostPlayMode)
- {
- return _hostPlayModeImpl.GetResourceVersion();
- }
- else
- {
- throw new NotImplementedException();
- }
- }
- /// <summary>
- /// 资源回收(卸载引用计数为零的资源)
- /// </summary>
- public static void UnloadUnusedAssets()
- {
- if (_isInitialize)
- {
- AssetSystem.Update();
- AssetSystem.UnloadUnusedAssets();
- }
- }
- /// <summary>
- /// 强制回收所有资源
- /// </summary>
- public static void ForceUnloadAllAssets()
- {
- if (_isInitialize)
- {
- AssetSystem.ForceUnloadAllAssets();
- }
- }
- #region 资源信息
- /// <summary>
- /// 是否需要从远端更新下载
- /// </summary>
- /// <param name="location">资源的定位地址</param>
- 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;
- }
- /// <summary>
- /// 是否需要从远端更新下载
- /// </summary>
- /// <param name="location">资源的定位地址</param>
- public static bool IsNeedDownloadFromRemote(AssetInfo assetInfo)
- {
- 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;
- }
- /// <summary>
- /// 获取资源信息列表
- /// </summary>
- /// <param name="tag">资源标签</param>
- public static AssetInfo[] GetAssetInfos(string tag)
- {
- DebugCheckInitialize();
- string[] tags = new string[] { tag };
- return _bundleServices.GetAssetInfos(tags);
- }
- /// <summary>
- /// 获取资源信息列表
- /// </summary>
- /// <param name="tags">资源标签列表</param>
- public static AssetInfo[] GetAssetInfos(string[] tags)
- {
- DebugCheckInitialize();
- return _bundleServices.GetAssetInfos(tags);
- }
- /// <summary>
- /// 获取资源信息
- /// </summary>
- /// <param name="location">资源的定位地址</param>
- public static AssetInfo GetAssetInfo(string location)
- {
- DebugCheckInitialize();
- AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
- return assetInfo;
- }
- /// <summary>
- /// 获取资源路径
- /// </summary>
- /// <param name="location">资源的定位地址</param>
- /// <returns>如果location地址无效,则返回空字符串</returns>
- public static string GetAssetPath(string location)
- {
- DebugCheckInitialize();
- return _locationServices.ConvertLocationToAssetPath(location);
- }
- #endregion
- #region 原生文件
- /// <summary>
- /// 异步获取原生文件
- /// </summary>
- /// <param name="location">资源的定位地址</param>
- /// <param name="copyPath">拷贝路径</param>
- public static RawFileOperation GetRawFileAsync(string location, string copyPath = null)
- {
- DebugCheckInitialize();
- AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
- return GetRawFileInternal(assetInfo, copyPath);
- }
- /// <summary>
- /// 异步获取原生文件
- /// </summary>
- /// <param name="assetInfo">资源信息</param>
- /// <param name="copyPath">拷贝路径</param>
- 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 场景加载
- /// <summary>
- /// 异步加载场景
- /// </summary>
- /// <param name="location">场景的定位地址</param>
- /// <param name="sceneMode">场景加载模式</param>
- /// <param name="activateOnLoad">加载完毕时是否主动激活</param>
- /// <param name="priority">优先级</param>
- 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;
- }
- /// <summary>
- /// 异步加载场景
- /// </summary>
- /// <param name="assetInfo">场景的资源信息</param>
- /// <param name="sceneMode">场景加载模式</param>
- /// <param name="activateOnLoad">加载完毕时是否主动激活</param>
- /// <param name="priority">优先级</param>
- 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 资源加载
- /// <summary>
- /// 同步加载资源对象
- /// </summary>
- /// <param name="assetInfo">资源信息</param>
- public static AssetOperationHandle LoadAssetSync(AssetInfo assetInfo)
- {
- DebugCheckInitialize();
- if (assetInfo.IsInvalid)
- YooLogger.Warning(assetInfo.Error);
- return LoadAssetInternal(assetInfo, true);
- }
- /// <summary>
- /// 同步加载资源对象
- /// </summary>
- /// <typeparam name="TObject">资源类型</typeparam>
- /// <param name="location">资源的定位地址</param>
- public static AssetOperationHandle LoadAssetSync<TObject>(string location) where TObject : UnityEngine.Object
- {
- DebugCheckInitialize();
- AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject));
- return LoadAssetInternal(assetInfo, true);
- }
- /// <summary>
- /// 同步加载资源对象
- /// </summary>
- /// <param name="location">资源的定位地址</param>
- /// <param name="type">资源类型</param>
- public static AssetOperationHandle LoadAssetSync(string location, System.Type type)
- {
- DebugCheckInitialize();
- AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
- return LoadAssetInternal(assetInfo, true);
- }
- /// <summary>
- /// 异步加载资源对象
- /// </summary>
- /// <param name="assetInfo">资源信息</param>
- public static AssetOperationHandle LoadAssetAsync(AssetInfo assetInfo)
- {
- DebugCheckInitialize();
- if (assetInfo.IsInvalid)
- YooLogger.Warning(assetInfo.Error);
- return LoadAssetInternal(assetInfo, false);
- }
- /// <summary>
- /// 异步加载资源对象
- /// </summary>
- /// <typeparam name="TObject">资源类型</typeparam>
- /// <param name="location">资源的定位地址</param>
- public static AssetOperationHandle LoadAssetAsync<TObject>(string location) where TObject : UnityEngine.Object
- {
- DebugCheckInitialize();
- AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject));
- return LoadAssetInternal(assetInfo, false);
- }
- /// <summary>
- /// 异步加载资源对象
- /// </summary>
- /// <param name="location">资源的定位地址</param>
- /// <param name="type">资源类型</param>
- 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<AssetOperationHandle>();
- }
- #endif
- var handle = AssetSystem.LoadAssetAsync(assetInfo);
- if (waitForAsyncComplete)
- handle.WaitForAsyncComplete();
- return handle;
- }
- #endregion
- #region 资源加载
- /// <summary>
- /// 同步加载子资源对象
- /// </summary>
- /// <param name="assetInfo">资源信息</param>
- public static SubAssetsOperationHandle LoadSubAssetsSync(AssetInfo assetInfo)
- {
- DebugCheckInitialize();
- if (assetInfo.IsInvalid)
- YooLogger.Warning(assetInfo.Error);
- return LoadSubAssetsInternal(assetInfo, true);
- }
- /// <summary>
- /// 同步加载子资源对象
- /// </summary>
- /// <typeparam name="TObject">资源类型</typeparam>
- /// <param name="location">资源的定位地址</param>
- public static SubAssetsOperationHandle LoadSubAssetsSync<TObject>(string location) where TObject : UnityEngine.Object
- {
- DebugCheckInitialize();
- AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject));
- return LoadSubAssetsInternal(assetInfo, true);
- }
- /// <summary>
- /// 同步加载子资源对象
- /// </summary>
- /// <param name="location">资源的定位地址</param>
- /// <param name="type">子对象类型</param>
- public static SubAssetsOperationHandle LoadSubAssetsSync(string location, System.Type type)
- {
- DebugCheckInitialize();
- AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
- return LoadSubAssetsInternal(assetInfo, true);
- }
- /// <summary>
- /// 异步加载子资源对象
- /// </summary>
- /// <param name="assetInfo">资源信息</param>
- public static SubAssetsOperationHandle LoadSubAssetsAsync(AssetInfo assetInfo)
- {
- DebugCheckInitialize();
- if (assetInfo.IsInvalid)
- YooLogger.Warning(assetInfo.Error);
- return LoadSubAssetsInternal(assetInfo, false);
- }
- /// <summary>
- /// 异步加载子资源对象
- /// </summary>
- /// <typeparam name="TObject">资源类型</typeparam>
- /// <param name="location">资源的定位地址</param>
- public static SubAssetsOperationHandle LoadSubAssetsAsync<TObject>(string location) where TObject : UnityEngine.Object
- {
- DebugCheckInitialize();
- AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject));
- return LoadSubAssetsInternal(assetInfo, false);
- }
- /// <summary>
- /// 异步加载子资源对象
- /// </summary>
- /// <param name="location">资源的定位地址</param>
- /// <param name="type">子对象类型</param>
- 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<SubAssetsOperationHandle>();
- }
- #endif
- var handle = AssetSystem.LoadSubAssetsAsync(assetInfo);
- if (waitForAsyncComplete)
- handle.WaitForAsyncComplete();
- return handle;
- }
- #endregion
- #region 资源下载
- /// <summary>
- /// 创建补丁下载器,用于下载更新资源标签指定的资源包文件
- /// </summary>
- /// <param name="tag">资源标签</param>
- /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
- /// <param name="failedTryAgain">下载失败的重试次数</param>
- public static PatchDownloaderOperation CreatePatchDownloader(string tag, int downloadingMaxNumber, int failedTryAgain)
- {
- DebugCheckInitialize();
- return CreatePatchDownloader(new string[] { tag }, downloadingMaxNumber, failedTryAgain);
- }
- /// <summary>
- /// 创建补丁下载器,用于下载更新资源标签指定的资源包文件
- /// </summary>
- /// <param name="tags">资源标签列表</param>
- /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
- /// <param name="failedTryAgain">下载失败的重试次数</param>
- public static PatchDownloaderOperation CreatePatchDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain)
- {
- DebugCheckInitialize();
- if (_playMode == EPlayMode.EditorSimulateMode || _playMode == EPlayMode.OfflinePlayMode)
- {
- List<BundleInfo> downloadList = new List<BundleInfo>();
- var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain);
- return operation;
- }
- else if (_playMode == EPlayMode.HostPlayMode)
- {
- return _hostPlayModeImpl.CreatePatchDownloaderByTags(tags, downloadingMaxNumber, failedTryAgain);
- }
- else
- {
- throw new NotImplementedException();
- }
- }
- /// <summary>
- /// 创建补丁下载器,用于下载更新当前资源版本所有的资源包文件
- /// </summary>
- /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
- /// <param name="failedTryAgain">下载失败的重试次数</param>
- public static PatchDownloaderOperation CreatePatchDownloader(int downloadingMaxNumber, int failedTryAgain)
- {
- DebugCheckInitialize();
- if (_playMode == EPlayMode.EditorSimulateMode || _playMode == EPlayMode.OfflinePlayMode)
- {
- List<BundleInfo> downloadList = new List<BundleInfo>();
- var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain);
- return operation;
- }
- else if (_playMode == EPlayMode.HostPlayMode)
- {
- return _hostPlayModeImpl.CreatePatchDownloaderByAll(downloadingMaxNumber, failedTryAgain);
- }
- else
- {
- throw new NotImplementedException();
- }
- }
- /// <summary>
- /// 创建补丁下载器,用于下载更新指定的资源列表依赖的资源包文件
- /// </summary>
- /// <param name="locations">资源定位列表</param>
- /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
- /// <param name="failedTryAgain">下载失败的重试次数</param>
- public static PatchDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain)
- {
- DebugCheckInitialize();
- if (_playMode == EPlayMode.EditorSimulateMode || _playMode == EPlayMode.OfflinePlayMode)
- {
- List<BundleInfo> downloadList = new List<BundleInfo>();
- var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain);
- return operation;
- }
- else if (_playMode == EPlayMode.HostPlayMode)
- {
- List<AssetInfo> assetInfos = new List<AssetInfo>(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();
- }
- }
- /// <summary>
- /// 创建补丁下载器,用于下载更新指定的资源列表依赖的资源包文件
- /// </summary>
- /// <param name="assetInfos">资源信息列表</param>
- /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
- /// <param name="failedTryAgain">下载失败的重试次数</param>
- public static PatchDownloaderOperation CreateBundleDownloader(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain)
- {
- DebugCheckInitialize();
- if (_playMode == EPlayMode.EditorSimulateMode || _playMode == EPlayMode.OfflinePlayMode)
- {
- List<BundleInfo> downloadList = new List<BundleInfo>();
- 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 资源解压
- /// <summary>
- /// 创建补丁解压器
- /// </summary>
- /// <param name="tag">资源标签</param>
- /// <param name="unpackingMaxNumber">同时解压的最大文件数</param>
- /// <param name="failedTryAgain">解压失败的重试次数</param>
- public static PatchUnpackerOperation CreatePatchUnpacker(string tag, int unpackingMaxNumber, int failedTryAgain)
- {
- DebugCheckInitialize();
- return CreatePatchUnpacker(new string[] { tag }, unpackingMaxNumber, failedTryAgain);
- }
- /// <summary>
- /// 创建补丁解压器
- /// </summary>
- /// <param name="tags">资源标签列表</param>
- /// <param name="unpackingMaxNumber">同时解压的最大文件数</param>
- /// <param name="failedTryAgain">解压失败的重试次数</param>
- public static PatchUnpackerOperation CreatePatchUnpacker(string[] tags, int unpackingMaxNumber, int failedTryAgain)
- {
- DebugCheckInitialize();
- if (_playMode == EPlayMode.EditorSimulateMode)
- {
- List<BundleInfo> downloadList = new List<BundleInfo>();
- var operation = new PatchUnpackerOperation(downloadList, unpackingMaxNumber, failedTryAgain);
- return operation;
- }
- else if (_playMode == EPlayMode.OfflinePlayMode)
- {
- List<BundleInfo> downloadList = new List<BundleInfo>();
- var operation = new PatchUnpackerOperation(downloadList, unpackingMaxNumber, failedTryAgain);
- return operation;
- }
- else if (_playMode == EPlayMode.HostPlayMode)
- {
- return _hostPlayModeImpl.CreatePatchUnpackerByTags(tags, unpackingMaxNumber, failedTryAgain);
- }
- else
- {
- throw new NotImplementedException();
- }
- }
- /// <summary>
- /// 创建补丁解压器
- /// </summary>
- /// <param name="unpackingMaxNumber">同时解压的最大文件数</param>
- /// <param name="failedTryAgain">解压失败的重试次数</param>
- public static PatchUnpackerOperation CreatePatchUnpacker(int unpackingMaxNumber, int failedTryAgain)
- {
- DebugCheckInitialize();
- if (_playMode == EPlayMode.EditorSimulateMode)
- {
- List<BundleInfo> downloadList = new List<BundleInfo>();
- var operation = new PatchUnpackerOperation(downloadList, unpackingMaxNumber, failedTryAgain);
- return operation;
- }
- else if (_playMode == EPlayMode.OfflinePlayMode)
- {
- List<BundleInfo> downloadList = new List<BundleInfo>();
- 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 包裹更新
- /// <summary>
- /// 创建资源包裹下载器,用于下载更新指定资源版本所有的资源包文件
- /// </summary>
- /// <param name="resourceVersion">指定更新的资源版本</param>
- /// <param name="timeout">超时时间</param>
- public static UpdatePackageOperation UpdatePackageAsync(int resourceVersion, 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(resourceVersion, timeout);
- }
- else
- {
- throw new NotImplementedException();
- }
- }
- #endregion
- #region 沙盒相关
- /// <summary>
- /// 获取沙盒的根路径
- /// </summary>
- public static string GetSandboxRoot()
- {
- return PathHelper.MakePersistentRootPath();
- }
- /// <summary>
- /// 清空沙盒目录
- /// </summary>
- public static void ClearSandbox()
- {
- SandboxHelper.DeleteSandbox();
- }
- /// <summary>
- /// 清空所有的缓存文件
- /// </summary>
- public static void ClearAllCacheFiles()
- {
- SandboxHelper.DeleteCacheFolder();
- }
- /// <summary>
- /// 清空未被使用的缓存文件
- /// </summary>
- 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()
- {
- 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 static void InternalUpdate()
- {
- OperationSystem.Update();
- DownloadSystem.Update();
- AssetSystem.Update();
- }
- /// <summary>
- /// 资源定位地址转换为资源完整路径
- /// </summary>
- 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 私有方法
- /// <summary>
- /// 资源定位地址转换为资源信息类,失败时内部会发出错误日志。
- /// </summary>
- /// <returns>如果转换失败会返回一个无效的资源信息类</returns>
- 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;
- }
- }
- #endregion
- }
- }
|