Files
YooAsset/Assets/YooAsset/Runtime/YooAssets.cs

974 lines
30 KiB
C#
Raw Normal View History

2022-03-01 10:44:12 +08:00
using System;
using System.Diagnostics;
2022-03-01 10:44:12 +08:00
using System.Collections;
using System.Collections.Generic;
2022-03-21 23:43:11 +08:00
using UnityEngine.SceneManagement;
2022-03-01 10:44:12 +08:00
namespace YooAsset
{
public static class YooAssets
{
/// <summary>
/// 运行模式
/// </summary>
public enum EPlayMode
2022-03-01 10:44:12 +08:00
{
/// <summary>
/// 编辑器下的模拟模式
/// 注意:在初始化的时候自动构建真机模拟环境。
2022-03-01 10:44:12 +08:00
/// </summary>
EditorSimulateMode,
2022-03-01 10:44:12 +08:00
/// <summary>
/// 离线运行模式
2022-03-01 10:44:12 +08:00
/// </summary>
OfflinePlayMode,
/// <summary>
/// 网络运行模式
2022-03-01 10:44:12 +08:00
/// </summary>
HostPlayMode,
}
/// <summary>
/// 初始化参数
/// </summary>
2022-03-01 10:44:12 +08:00
public abstract class CreateParameters
{
/// <summary>
2022-05-08 22:09:44 +08:00
/// 资源定位地址大小写不敏感
/// </summary>
public bool LocationToLower = false;
2022-03-01 10:44:12 +08:00
/// <summary>
/// 资源定位服务接口
2022-03-01 10:44:12 +08:00
/// </summary>
public ILocationServices LocationServices = null;
2022-03-01 10:44:12 +08:00
/// <summary>
/// 文件解密服务接口
2022-03-01 10:44:12 +08:00
/// </summary>
2022-03-09 23:57:04 +08:00
public IDecryptionServices DecryptionServices = null;
2022-03-01 10:44:12 +08:00
/// <summary>
/// 资源加载的最大数量
/// </summary>
public int AssetLoadingMaxNumber = int.MaxValue;
2022-03-24 00:20:37 +08:00
/// <summary>
/// 异步操作系统每帧允许运行的最大时间切片(单位:毫秒)
/// </summary>
public long OperationSystemMaxTimeSlice = long.MaxValue;
2022-03-01 10:44:12 +08:00
}
/// <summary>
/// 编辑器下模拟运行模式的初始化参数
2022-03-01 10:44:12 +08:00
/// </summary>
public class EditorSimulateModeParameters : CreateParameters
2022-03-01 10:44:12 +08:00
{
/// <summary>
/// 用于模拟运行的资源清单路径
/// 注意:如果路径为空,会自动重新构建补丁清单。
/// </summary>
public string SimulatePatchManifestPath;
2022-03-01 10:44:12 +08:00
}
/// <summary>
/// 离线运行模式的初始化参数
2022-03-01 10:44:12 +08:00
/// </summary>
public class OfflinePlayModeParameters : CreateParameters
{
}
/// <summary>
/// 网络运行模式的初始化参数
2022-03-01 10:44:12 +08:00
/// </summary>
public class HostPlayModeParameters : CreateParameters
{
/// <summary>
/// 当缓存池被污染的时候清理缓存池
/// </summary>
public bool ClearCacheWhenDirty;
/// <summary>
/// 默认的资源服务器下载地址
/// </summary>
public string DefaultHostServer;
/// <summary>
/// 备用的资源服务器下载地址
/// </summary>
public string FallbackHostServer;
2022-04-04 22:43:47 +08:00
/// <summary>
/// 启用断点续传功能的文件大小
/// </summary>
public int BreakpointResumeFileSize = int.MaxValue;
2022-03-01 10:44:12 +08:00
}
2022-03-03 18:07:58 +08:00
private static bool _isInitialize = false;
private static string _initializeError = string.Empty;
private static EOperationStatus _initializeStatus = EOperationStatus.None;
2022-03-01 10:44:12 +08:00
private static EPlayMode _playMode;
private static IBundleServices _bundleServices;
private static ILocationServices _locationServices;
private static EditorSimulateModeImpl _editorSimulateModeImpl;
2022-03-01 10:44:12 +08:00
private static OfflinePlayModeImpl _offlinePlayModeImpl;
private static HostPlayModeImpl _hostPlayModeImpl;
/// <summary>
/// 异步初始化
/// </summary>
public static InitializationOperation InitializeAsync(CreateParameters parameters)
{
if (parameters == null)
2022-03-24 00:20:37 +08:00
throw new Exception($"YooAsset create parameters is null.");
2022-03-01 10:44:12 +08:00
if (parameters.LocationServices == null)
throw new Exception($"{nameof(IBundleServices)} is null.");
else
_locationServices = parameters.LocationServices;
2022-03-01 10:44:12 +08:00
#if !UNITY_EDITOR
if (parameters is EditorSimulateModeParameters)
throw new Exception($"Editor simulate mode only support unity editor.");
2022-03-01 10:44:12 +08:00
#endif
2022-03-03 18:07:58 +08:00
// 创建驱动器
if (_isInitialize == false)
{
_isInitialize = true;
2022-03-07 14:37:50 +08:00
UnityEngine.GameObject driverGo = new UnityEngine.GameObject("[YooAsset]");
driverGo.AddComponent<YooAssetDriver>();
2022-03-03 18:07:58 +08:00
UnityEngine.Object.DontDestroyOnLoad(driverGo);
}
else
{
throw new Exception("YooAsset is initialized yet.");
}
2022-03-24 00:20:37 +08:00
if (parameters.AssetLoadingMaxNumber < 1)
2022-03-01 10:44:12 +08:00
{
2022-03-24 00:20:37 +08:00
parameters.AssetLoadingMaxNumber = 1;
YooLogger.Warning($"{nameof(parameters.AssetLoadingMaxNumber)} minimum value is 1");
2022-03-01 10:44:12 +08:00
}
if (parameters.OperationSystemMaxTimeSlice < 30)
2022-03-01 10:44:12 +08:00
{
parameters.OperationSystemMaxTimeSlice = 30;
2022-03-24 00:20:37 +08:00
YooLogger.Warning($"{nameof(parameters.OperationSystemMaxTimeSlice)} minimum value is 33 milliseconds");
2022-03-01 10:44:12 +08:00
}
// 运行模式
if (parameters is EditorSimulateModeParameters)
_playMode = EPlayMode.EditorSimulateMode;
2022-03-01 10:44:12 +08:00
else if (parameters is OfflinePlayModeParameters)
_playMode = EPlayMode.OfflinePlayMode;
else if (parameters is HostPlayModeParameters)
_playMode = EPlayMode.HostPlayMode;
else
throw new NotImplementedException();
2022-03-24 00:20:37 +08:00
// 初始化异步操作系统
OperationSystem.Initialize(parameters.OperationSystemMaxTimeSlice);
2022-04-04 22:43:47 +08:00
// 初始化下载系统
if (_playMode == EPlayMode.HostPlayMode)
{
#if UNITY_WEBGL
throw new Exception($"{EPlayMode.HostPlayMode} not supports WebGL platform !");
#else
2022-04-04 22:43:47 +08:00
var hostPlayModeParameters = parameters as HostPlayModeParameters;
DownloadSystem.Initialize(hostPlayModeParameters.BreakpointResumeFileSize);
#endif
2022-04-04 22:43:47 +08:00
}
2022-03-24 00:20:37 +08:00
// 初始化资源系统
InitializationOperation initializeOperation;
if (_playMode == EPlayMode.EditorSimulateMode)
2022-03-01 10:44:12 +08:00
{
_editorSimulateModeImpl = new EditorSimulateModeImpl();
_bundleServices = _editorSimulateModeImpl;
2022-03-09 23:57:04 +08:00
AssetSystem.Initialize(true, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices);
var editorSimulateModeParameters = parameters as EditorSimulateModeParameters;
initializeOperation = _editorSimulateModeImpl.InitializeAsync(
editorSimulateModeParameters.LocationToLower,
editorSimulateModeParameters.SimulatePatchManifestPath);
2022-03-01 10:44:12 +08:00
}
else if (_playMode == EPlayMode.OfflinePlayMode)
{
_offlinePlayModeImpl = new OfflinePlayModeImpl();
_bundleServices = _offlinePlayModeImpl;
2022-03-09 23:57:04 +08:00
AssetSystem.Initialize(false, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices);
initializeOperation = _offlinePlayModeImpl.InitializeAsync(parameters.LocationToLower);
2022-03-01 10:44:12 +08:00
}
else if (_playMode == EPlayMode.HostPlayMode)
{
_hostPlayModeImpl = new HostPlayModeImpl();
_bundleServices = _hostPlayModeImpl;
2022-03-09 23:57:04 +08:00
AssetSystem.Initialize(false, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices);
2022-03-01 10:44:12 +08:00
var hostPlayModeParameters = parameters as HostPlayModeParameters;
initializeOperation = _hostPlayModeImpl.InitializeAsync(
hostPlayModeParameters.LocationToLower,
2022-03-01 10:44:12 +08:00
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;
2022-03-01 10:44:12 +08:00
}
/// <summary>
/// 向网络端请求静态资源版本
/// </summary>
/// <param name="timeout">超时时间默认值60秒</param>
public static UpdateStaticVersionOperation UpdateStaticVersionAsync(int timeout = 60)
{
DebugCheckInitialize();
if (_playMode == EPlayMode.EditorSimulateMode)
{
var operation = new EditorPlayModeUpdateStaticVersionOperation();
OperationSystem.ProcessOperaiton(operation);
return operation;
}
else if (_playMode == EPlayMode.OfflinePlayMode)
{
var operation = new OfflinePlayModeUpdateStaticVersionOperation();
OperationSystem.ProcessOperaiton(operation);
return operation;
}
else if (_playMode == EPlayMode.HostPlayMode)
{
if (_hostPlayModeImpl == null)
throw new Exception("YooAsset is not initialized.");
return _hostPlayModeImpl.UpdateStaticVersionAsync(timeout);
}
else
{
throw new NotImplementedException();
}
}
/// <summary>
2022-03-01 10:44:12 +08:00
/// 向网络端请求并更新补丁清单
/// </summary>
/// <param name="resourceVersion">更新的资源版本</param>
2022-03-10 16:54:56 +08:00
/// <param name="timeout">超时时间默认值60秒</param>
public static UpdateManifestOperation UpdateManifestAsync(int resourceVersion, int timeout = 60)
2022-03-01 10:44:12 +08:00
{
DebugCheckInitialize();
if (_playMode == EPlayMode.EditorSimulateMode)
2022-03-01 10:44:12 +08:00
{
var operation = new EditorPlayModeUpdateManifestOperation();
2022-03-09 21:53:01 +08:00
OperationSystem.ProcessOperaiton(operation);
2022-03-01 10:44:12 +08:00
return operation;
}
else if (_playMode == EPlayMode.OfflinePlayMode)
{
var operation = new OfflinePlayModeUpdateManifestOperation();
2022-03-09 21:53:01 +08:00
OperationSystem.ProcessOperaiton(operation);
2022-03-01 10:44:12 +08:00
return operation;
}
else if (_playMode == EPlayMode.HostPlayMode)
{
if (_hostPlayModeImpl == null)
throw new Exception("YooAsset is not initialized.");
return _hostPlayModeImpl.UpdatePatchManifestAsync(resourceVersion, timeout);
2022-03-01 10:44:12 +08:00
}
else
{
throw new NotImplementedException();
}
}
/// <summary>
/// 开启一个异步操作
/// </summary>
/// <param name="operation">异步操作对象</param>
public static void ProcessOperaiton(GameAsyncOperation operation)
{
OperationSystem.ProcessOperaiton(operation);
}
2022-03-01 10:44:12 +08:00
/// <summary>
/// 获取资源版本号
/// </summary>
public static int GetResourceVersion()
{
DebugCheckInitialize();
if (_playMode == EPlayMode.EditorSimulateMode)
2022-03-01 10:44:12 +08:00
{
if (_editorSimulateModeImpl == null)
2022-03-01 10:44:12 +08:00
throw new Exception("YooAsset is not initialized.");
return _editorSimulateModeImpl.GetResourceVersion();
2022-03-01 10:44:12 +08:00
}
else if (_playMode == EPlayMode.OfflinePlayMode)
{
if (_offlinePlayModeImpl == null)
throw new Exception("YooAsset is not initialized.");
return _offlinePlayModeImpl.GetResourceVersion();
}
else if (_playMode == EPlayMode.HostPlayMode)
{
if (_hostPlayModeImpl == null)
throw new Exception("YooAsset is not initialized.");
return _hostPlayModeImpl.GetResourceVersion();
}
else
{
throw new NotImplementedException();
}
}
/// <summary>
/// 资源回收(卸载引用计数为零的资源)
/// </summary>
public static void UnloadUnusedAssets()
{
AssetSystem.Update();
AssetSystem.UnloadUnusedAssets();
}
/// <summary>
/// 强制回收所有资源
/// </summary>
public static void ForceUnloadAllAssets()
{
AssetSystem.ForceUnloadAllAssets();
}
/// <summary>
/// 获取调试信息
/// </summary>
internal static void GetDebugReport(DebugReport report)
{
if (report == null)
YooLogger.Error($"{nameof(DebugReport)} is null");
AssetSystem.GetDebugReport(report);
}
#region
2022-03-01 10:44:12 +08:00
/// <summary>
/// 获取资源包信息
/// </summary>
/// <param name="location">资源的定位地址</param>
2022-03-07 21:20:58 +08:00
public static BundleInfo GetBundleInfo(string location)
2022-03-01 10:44:12 +08:00
{
DebugCheckInitialize();
string assetPath = _locationServices.ConvertLocationToAssetPath(location);
2022-03-07 21:20:58 +08:00
string bundleName = _bundleServices.GetBundleName(assetPath);
return _bundleServices.GetBundleInfo(bundleName);
2022-03-01 10:44:12 +08:00
}
/// <summary>
/// 获取资源包信息
2022-03-01 10:44:12 +08:00
/// </summary>
/// <param name="assetInfo">资源信息</param>
public static BundleInfo GetBundleInfo(AssetInfo assetInfo)
2022-03-01 10:44:12 +08:00
{
DebugCheckInitialize();
string bundleName = _bundleServices.GetBundleName(assetInfo.AssetPath);
return _bundleServices.GetBundleInfo(bundleName);
2022-03-01 10:44:12 +08:00
}
/// <summary>
/// 获取资源信息列表
/// </summary>
/// <param name="bundleInfo">资源包信息</param>
public static AssetInfo[] GetAssetInfos(BundleInfo bundleInfo)
{
DebugCheckInitialize();
return _bundleServices.GetAssetInfos(bundleInfo.BundleName);
}
2022-03-01 10:44:12 +08:00
/// <summary>
/// 获取资源信息列表
2022-03-01 10:44:12 +08:00
/// </summary>
/// <param name="tag">资源标签</param>
public static AssetInfo[] GetAssetInfos(string tag)
2022-03-01 10:44:12 +08:00
{
DebugCheckInitialize();
string[] tags = new string[] { tag };
return _bundleServices.GetAssetInfos(tags);
2022-03-01 10:44:12 +08:00
}
/// <summary>
/// 获取资源信息列表
/// </summary>
/// <param name="tags">资源标签列表</param>
public static AssetInfo[] GetAssetInfos(string[] tags)
{
DebugCheckInitialize();
return _bundleServices.GetAssetInfos(tags);
}
#endregion
2022-03-01 10:44:12 +08:00
#region
2022-03-21 23:43:11 +08:00
/// <summary>
/// 异步加载场景
/// </summary>
/// <param name="location">场景的定位地址</param>
2022-03-22 20:15:59 +08:00
/// <param name="sceneMode">场景加载模式</param>
2022-03-21 23:43:11 +08:00
/// <param name="activateOnLoad">加载完毕时是否主动激活</param>
2022-03-22 20:15:59 +08:00
/// <param name="priority">优先级</param>
public static SceneOperationHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool activateOnLoad = true, int priority = 100)
2022-03-21 23:43:11 +08:00
{
DebugCheckInitialize();
string scenePath = _locationServices.ConvertLocationToAssetPath(location);
2022-03-22 20:15:59 +08:00
var handle = AssetSystem.LoadSceneAsync(scenePath, sceneMode, activateOnLoad, priority);
2022-03-21 23:43:11 +08:00
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();
string scenePath = assetInfo.AssetPath;
var handle = AssetSystem.LoadSceneAsync(scenePath, sceneMode, activateOnLoad, priority);
return handle;
}
2022-03-21 23:43:11 +08:00
#endregion
2022-03-01 10:44:12 +08:00
#region
2022-03-23 00:29:23 +08:00
/// <summary>
/// 异步获取原生文件
2022-03-23 00:29:23 +08:00
/// </summary>
/// <param name="location">资源的定位地址</param>
/// <param name="copyPath">拷贝路径</param>
public static RawFileOperation GetRawFileAsync(string location, string copyPath = null)
2022-03-23 00:29:23 +08:00
{
DebugCheckInitialize();
string assetPath = _locationServices.ConvertLocationToAssetPath(location);
return GetRawFileInternal(assetPath, copyPath);
}
/// <summary>
/// 异步获取原生文件
/// </summary>
/// <param name="assetInfo">资源信息</param>
/// <param name="copyPath">拷贝路径</param>
public static RawFileOperation GetRawFileAsync(AssetInfo assetInfo, string copyPath = null)
{
DebugCheckInitialize();
return GetRawFileInternal(assetInfo.AssetPath, copyPath);
2022-03-23 00:29:23 +08:00
}
2022-03-23 00:29:23 +08:00
private static RawFileOperation GetRawFileInternal(string assetPath, string copyPath)
{
string bundleName = _bundleServices.GetBundleName(assetPath);
BundleInfo bundleInfo = _bundleServices.GetBundleInfo(bundleName);
if (_playMode == EPlayMode.EditorSimulateMode)
{
RawFileOperation operation = new EditorPlayModeRawFileOperation(bundleInfo, copyPath);
OperationSystem.ProcessOperaiton(operation);
return operation;
}
else if (_playMode == EPlayMode.OfflinePlayMode)
{
RawFileOperation operation = new OfflinePlayModeRawFileOperation(bundleInfo, copyPath);
OperationSystem.ProcessOperaiton(operation);
return operation;
}
else if (_playMode == EPlayMode.HostPlayMode)
{
RawFileOperation operation = new HostPlayModeRawFileOperation(bundleInfo, copyPath);
OperationSystem.ProcessOperaiton(operation);
return operation;
}
else
{
throw new NotImplementedException();
}
}
#endregion
#region
/// <summary>
/// 同步加载资源对象
/// </summary>
/// <param name="assetInfo">资源信息</param>
public static AssetOperationHandle LoadAssetSync(AssetInfo assetInfo)
{
DebugCheckInitialize();
return LoadAssetInternal(assetInfo.AssetPath, assetInfo.AssetType, true);
}
2022-03-01 10:44:12 +08:00
/// <summary>
/// 同步加载资源对象
/// </summary>
2022-03-21 23:43:11 +08:00
/// <typeparam name="TObject">资源类型</typeparam>
/// <param name="location">资源的定位地址</param>
2022-03-01 10:44:12 +08:00
public static AssetOperationHandle LoadAssetSync<TObject>(string location) where TObject : class
{
DebugCheckInitialize();
string assetPath = _locationServices.ConvertLocationToAssetPath(location);
return LoadAssetInternal(assetPath, typeof(TObject), true);
2022-03-01 10:44:12 +08:00
}
2022-03-21 23:43:11 +08:00
/// <summary>
/// 同步加载资源对象
/// </summary>
/// <param name="location">资源的定位地址</param>
2022-03-21 23:43:11 +08:00
/// <param name="type">资源类型</param>
public static AssetOperationHandle LoadAssetSync(string location, System.Type type)
2022-03-01 10:44:12 +08:00
{
DebugCheckInitialize();
string assetPath = _locationServices.ConvertLocationToAssetPath(location);
return LoadAssetInternal(assetPath, type, true);
}
/// <summary>
/// 异步加载资源对象
/// </summary>
/// <param name="assetInfo">资源信息</param>
public static AssetOperationHandle LoadAssetAsync(AssetInfo assetInfo)
{
DebugCheckInitialize();
return LoadAssetInternal(assetInfo.AssetPath, assetInfo.AssetType, false);
2022-03-01 10:44:12 +08:00
}
/// <summary>
/// 异步加载资源对象
2022-03-01 10:44:12 +08:00
/// </summary>
2022-03-21 23:43:11 +08:00
/// <typeparam name="TObject">资源类型</typeparam>
/// <param name="location">资源的定位地址</param>
public static AssetOperationHandle LoadAssetAsync<TObject>(string location)
2022-03-01 10:44:12 +08:00
{
DebugCheckInitialize();
string assetPath = _locationServices.ConvertLocationToAssetPath(location);
return LoadAssetInternal(assetPath, typeof(TObject), false);
2022-03-01 10:44:12 +08:00
}
/// <summary>
/// 异步加载资源对象
2022-03-01 10:44:12 +08:00
/// </summary>
/// <param name="location">资源的定位地址</param>
/// <param name="type">资源类型</param>
public static AssetOperationHandle LoadAssetAsync(string location, System.Type type)
2022-03-01 10:44:12 +08:00
{
DebugCheckInitialize();
string assetPath = _locationServices.ConvertLocationToAssetPath(location);
return LoadAssetInternal(assetPath, type, false);
2022-03-01 10:44:12 +08:00
}
2022-03-21 23:43:11 +08:00
private static AssetOperationHandle LoadAssetInternal(string assetPath, System.Type assetType, bool waitForAsyncComplete)
{
var handle = AssetSystem.LoadAssetAsync(assetPath, assetType);
if (waitForAsyncComplete)
handle.WaitForAsyncComplete();
return handle;
}
#endregion
#region
/// <summary>
/// 同步加载子资源对象
/// </summary>
/// <param name="assetInfo">资源信息</param>
public static SubAssetsOperationHandle LoadSubAssetsSync(AssetInfo assetInfo)
{
DebugCheckInitialize();
return LoadSubAssetsInternal(assetInfo.AssetPath, assetInfo.AssetType, true);
}
2022-03-01 10:44:12 +08:00
/// <summary>
/// 同步加载子资源对象
2022-03-01 10:44:12 +08:00
/// </summary>
2022-03-21 23:43:11 +08:00
/// <typeparam name="TObject">资源类型</typeparam>
/// <param name="location">资源的定位地址</param>
public static SubAssetsOperationHandle LoadSubAssetsSync<TObject>(string location)
2022-03-01 10:44:12 +08:00
{
DebugCheckInitialize();
string assetPath = _locationServices.ConvertLocationToAssetPath(location);
return LoadSubAssetsInternal(assetPath, typeof(TObject), true);
2022-03-01 10:44:12 +08:00
}
2022-03-21 23:43:11 +08:00
/// <summary>
/// 同步加载子资源对象
2022-03-21 23:43:11 +08:00
/// </summary>
/// <param name="location">资源的定位地址</param>
/// <param name="type">子对象类型</param>
public static SubAssetsOperationHandle LoadSubAssetsSync(string location, System.Type type)
2022-03-01 10:44:12 +08:00
{
DebugCheckInitialize();
string assetPath = _locationServices.ConvertLocationToAssetPath(location);
return LoadSubAssetsInternal(assetPath, type, true);
}
/// <summary>
/// 异步加载子资源对象
/// </summary>
/// <param name="assetInfo">资源信息</param>
public static SubAssetsOperationHandle LoadSubAssetsAsync(AssetInfo assetInfo)
{
DebugCheckInitialize();
return LoadSubAssetsInternal(assetInfo.AssetPath, assetInfo.AssetType, false);
2022-03-01 10:44:12 +08:00
}
/// <summary>
2022-03-21 23:43:11 +08:00
/// 异步加载子资源对象
2022-03-01 10:44:12 +08:00
/// </summary>
2022-03-21 23:43:11 +08:00
/// <typeparam name="TObject">资源类型</typeparam>
/// <param name="location">资源的定位地址</param>
2022-03-22 20:15:59 +08:00
public static SubAssetsOperationHandle LoadSubAssetsAsync<TObject>(string location)
2022-03-01 10:44:12 +08:00
{
DebugCheckInitialize();
string assetPath = _locationServices.ConvertLocationToAssetPath(location);
return LoadSubAssetsInternal(assetPath, typeof(TObject), false);
2022-03-01 10:44:12 +08:00
}
2022-03-21 23:43:11 +08:00
/// <summary>
/// 异步加载子资源对象
/// </summary>
/// <param name="location">资源的定位地址</param>
2022-03-21 23:43:11 +08:00
/// <param name="type">子对象类型</param>
2022-03-22 20:15:59 +08:00
public static SubAssetsOperationHandle LoadSubAssetsAsync(string location, System.Type type)
2022-03-01 10:44:12 +08:00
{
DebugCheckInitialize();
string assetPath = _locationServices.ConvertLocationToAssetPath(location);
return LoadSubAssetsInternal(assetPath, type, false);
2022-03-01 10:44:12 +08:00
}
2022-03-24 00:20:37 +08:00
2022-03-01 10:44:12 +08:00
private static SubAssetsOperationHandle LoadSubAssetsInternal(string assetPath, System.Type assetType, bool waitForAsyncComplete)
2022-03-01 10:44:12 +08:00
{
var handle = AssetSystem.LoadSubAssetsAsync(assetPath, assetType);
2022-03-01 10:44:12 +08:00
if (waitForAsyncComplete)
handle.WaitForAsyncComplete();
return handle;
}
#endregion
#region
2022-03-01 10:44:12 +08:00
/// <summary>
/// 创建补丁下载器,用于下载更新资源标签指定的资源包文件
2022-03-01 10:44:12 +08:00
/// </summary>
2022-03-03 18:07:58 +08:00
/// <param name="tag">资源标签</param>
2022-03-01 10:44:12 +08:00
/// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
/// <param name="failedTryAgain">下载失败的重试次数</param>
public static PatchDownloaderOperation CreatePatchDownloader(string tag, int downloadingMaxNumber, int failedTryAgain)
2022-03-01 10:44:12 +08:00
{
DebugCheckInitialize();
2022-03-03 18:07:58 +08:00
return CreatePatchDownloader(new string[] { tag }, downloadingMaxNumber, failedTryAgain);
2022-03-01 10:44:12 +08:00
}
/// <summary>
/// 创建补丁下载器,用于下载更新资源标签指定的资源包文件
2022-03-01 10:44:12 +08:00
/// </summary>
2022-03-03 18:07:58 +08:00
/// <param name="tags">资源标签列表</param>
2022-03-01 10:44:12 +08:00
/// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
/// <param name="failedTryAgain">下载失败的重试次数</param>
public static PatchDownloaderOperation CreatePatchDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain)
2022-03-01 10:44:12 +08:00
{
DebugCheckInitialize();
if (_playMode == EPlayMode.EditorSimulateMode || _playMode == EPlayMode.OfflinePlayMode)
2022-03-01 10:44:12 +08:00
{
2022-03-07 21:20:58 +08:00
List<BundleInfo> downloadList = new List<BundleInfo>();
var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain);
2022-03-03 18:07:58 +08:00
return operation;
2022-03-01 10:44:12 +08:00
}
else if (_playMode == EPlayMode.HostPlayMode)
{
if (_hostPlayModeImpl == null)
throw new Exception("YooAsset is not initialized.");
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)
2022-03-01 10:44:12 +08:00
{
2022-03-07 21:20:58 +08:00
List<BundleInfo> downloadList = new List<BundleInfo>();
var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain);
2022-03-03 18:07:58 +08:00
return operation;
2022-03-01 10:44:12 +08:00
}
else if (_playMode == EPlayMode.HostPlayMode)
{
if (_hostPlayModeImpl == null)
throw new Exception("YooAsset is not initialized.");
return _hostPlayModeImpl.CreatePatchDownloaderByAll(downloadingMaxNumber, failedTryAgain);
2022-03-01 10:44:12 +08:00
}
else
{
throw new NotImplementedException();
}
}
2022-03-01 10:44:12 +08:00
/// <summary>
/// 创建补丁下载器,用于下载更新指定的资源列表依赖的资源包文件
2022-03-01 10:44:12 +08:00
/// </summary>
/// <param name="locations">资源定位列表</param>
2022-03-01 10:44:12 +08:00
/// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
/// <param name="failedTryAgain">下载失败的重试次数</param>
public static PatchDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain)
2022-03-01 10:44:12 +08:00
{
DebugCheckInitialize();
if (_playMode == EPlayMode.EditorSimulateMode || _playMode == EPlayMode.OfflinePlayMode)
2022-03-01 10:44:12 +08:00
{
2022-03-07 21:20:58 +08:00
List<BundleInfo> downloadList = new List<BundleInfo>();
var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain);
2022-03-03 18:07:58 +08:00
return operation;
2022-03-01 10:44:12 +08:00
}
else if (_playMode == EPlayMode.HostPlayMode)
{
if (_hostPlayModeImpl == null)
throw new Exception("YooAsset is not initialized.");
List<string> assetPaths = new List<string>(locations.Length);
foreach (var location in locations)
{
string assetPath = _locationServices.ConvertLocationToAssetPath(location);
2022-03-01 10:44:12 +08:00
assetPaths.Add(assetPath);
}
return _hostPlayModeImpl.CreatePatchDownloaderByPaths(assetPaths, downloadingMaxNumber, failedTryAgain);
2022-03-01 10:44:12 +08:00
}
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)
{
if (_hostPlayModeImpl == null)
throw new Exception("YooAsset is not initialized.");
List<string> assetPaths = new List<string>(assetInfos.Length);
foreach (var assetInfo in assetInfos)
{
assetPaths.Add(assetInfo.AssetPath);
}
return _hostPlayModeImpl.CreatePatchDownloaderByPaths(assetPaths, downloadingMaxNumber, failedTryAgain);
}
else
{
throw new NotImplementedException();
}
}
2022-03-01 10:44:12 +08:00
#endregion
#region
2022-03-07 20:13:39 +08:00
/// <summary>
/// 创建补丁解压器
/// </summary>
/// <param name="tag">资源标签</param>
/// <param name="unpackingMaxNumber">同时解压的最大文件数</param>
/// <param name="failedTryAgain">解压失败的重试次数</param>
public static PatchUnpackerOperation CreatePatchUnpacker(string tag, int unpackingMaxNumber, int failedTryAgain)
2022-03-07 20:13:39 +08:00
{
DebugCheckInitialize();
2022-03-07 20:13:39 +08:00
return CreatePatchUnpacker(new string[] { tag }, unpackingMaxNumber, failedTryAgain);
}
2022-03-24 00:20:37 +08:00
2022-03-07 20:13:39 +08:00
/// <summary>
/// 创建补丁解压器
/// </summary>
/// <param name="tags">资源标签列表</param>
/// <param name="unpackingMaxNumber">同时解压的最大文件数</param>
/// <param name="failedTryAgain">解压失败的重试次数</param>
public static PatchUnpackerOperation CreatePatchUnpacker(string[] tags, int unpackingMaxNumber, int failedTryAgain)
2022-03-07 20:13:39 +08:00
{
DebugCheckInitialize();
if (_playMode == EPlayMode.EditorSimulateMode)
2022-03-07 20:13:39 +08:00
{
2022-03-07 21:20:58 +08:00
List<BundleInfo> downloadList = new List<BundleInfo>();
var operation = new PatchUnpackerOperation(downloadList, unpackingMaxNumber, failedTryAgain);
2022-03-07 20:13:39 +08:00
return operation;
}
else if (_playMode == EPlayMode.OfflinePlayMode)
{
2022-03-25 15:02:21 +08:00
if (_offlinePlayModeImpl == null)
throw new Exception("YooAsset is not initialized.");
return _offlinePlayModeImpl.CreatePatchUnpackerByTags(tags, unpackingMaxNumber, failedTryAgain);
2022-03-07 20:13:39 +08:00
}
else if (_playMode == EPlayMode.HostPlayMode)
{
if (_hostPlayModeImpl == null)
throw new Exception("YooAsset is not initialized.");
return _hostPlayModeImpl.CreatePatchUnpackerByTags(tags, 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.ProcessOperaiton(operation);
return operation;
}
else if (_playMode == EPlayMode.OfflinePlayMode)
{
var operation = new OfflinePlayModeUpdatePackageOperation();
OperationSystem.ProcessOperaiton(operation);
return operation;
}
else if (_playMode == EPlayMode.HostPlayMode)
{
if (_hostPlayModeImpl == null)
throw new Exception("YooAsset is not initialized.");
return _hostPlayModeImpl.UpdatePackageAsync(resourceVersion, timeout);
2022-03-07 20:13:39 +08:00
}
else
{
throw new NotImplementedException();
}
}
#endregion
2022-03-01 10:44:12 +08:00
#region
/// <summary>
/// 获取沙盒的根路径
/// </summary>
public static string GetSandboxRoot()
{
return PathHelper.MakePersistentRootPath();
}
2022-03-01 10:44:12 +08:00
/// <summary>
/// 清空沙盒目录
/// </summary>
public static void ClearSandbox()
{
SandboxHelper.DeleteSandbox();
2022-03-01 10:44:12 +08:00
}
/// <summary>
/// 清空所有的缓存文件
2022-03-01 10:44:12 +08:00
/// </summary>
public static void ClearAllCacheFiles()
2022-03-01 10:44:12 +08:00
{
SandboxHelper.DeleteCacheFolder();
}
/// <summary>
/// 清空未被使用的缓存文件
/// </summary>
public static void ClearUnusedCacheFiles()
{
if (_playMode == EPlayMode.HostPlayMode)
_hostPlayModeImpl.ClearUnusedCacheFiles();
2022-03-01 10:44:12 +08:00
}
#endregion
#region
2022-03-07 14:37:50 +08:00
internal static void InternalUpdate()
{
// 更新异步请求操作
2022-03-09 21:53:01 +08:00
OperationSystem.Update();
2022-03-07 14:37:50 +08:00
// 更新下载管理系统
DownloadSystem.Update();
// 轮询更新资源系统
AssetSystem.Update();
}
/// <summary>
/// 资源定位地址转换为资源完整路径
/// </summary>
internal static string MappingToAssetPath(string location)
{
DebugCheckLocation(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))
{
YooLogger.Error("location param is null or empty!");
}
else
{
// 检查路径末尾是否有空格
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}\"");
}
}
2022-03-01 10:44:12 +08:00
#endregion
}
}