Update runtime code

pull/51/head
hevinci 2022-09-29 18:40:43 +08:00
parent 46c219505f
commit a71921cdd1
36 changed files with 844 additions and 1625 deletions

View File

@ -6,24 +6,26 @@ using UnityEngine.SceneManagement;
namespace YooAsset
{
internal static class AssetSystem
internal class AssetSystemImpl
{
private static readonly List<AssetBundleLoaderBase> _loaders = new List<AssetBundleLoaderBase>(1000);
private static readonly List<ProviderBase> _providers = new List<ProviderBase>(1000);
private static readonly Dictionary<string, SceneOperationHandle> _sceneHandles = new Dictionary<string, SceneOperationHandle>(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<AssetBundleLoaderBase> _loaders = new List<AssetBundleLoaderBase>(1000);
private readonly List<ProviderBase> _providers = new List<ProviderBase>(1000);
private readonly Dictionary<string, SceneOperationHandle> _sceneHandles = new Dictionary<string, SceneOperationHandle>(100);
private long _sceneCreateCount = 0;
private bool _simulationOnEditor;
private int _loadingMaxNumber;
public IDecryptionServices DecryptionServices { private set; get; }
public IBundleServices BundleServices { private set; get; }
/// <summary>
/// 初始化
/// 注意在使用AssetSystem之前需要初始化
/// </summary>
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
/// <summary>
/// 更新
/// </summary>
public static void Update()
public void Update()
{
// 更新加载器
foreach (var loader in _loaders)
@ -67,7 +69,7 @@ namespace YooAsset
/// <summary>
/// 销毁
/// </summary>
public static void DestroyAll()
public void DestroyAll()
{
_loaders.Clear();
_providers.Clear();
@ -80,7 +82,7 @@ namespace YooAsset
/// <summary>
/// 资源回收(卸载引用计数为零的资源)
/// </summary>
public static void UnloadUnusedAssets()
public void UnloadUnusedAssets()
{
if (_simulationOnEditor)
{
@ -115,7 +117,7 @@ namespace YooAsset
/// <summary>
/// 强制回收所有资源
/// </summary>
public static void ForceUnloadAllAssets()
public void ForceUnloadAllAssets()
{
foreach (var provider in _providers)
{
@ -136,7 +138,7 @@ namespace YooAsset
/// <summary>
/// 加载场景
/// </summary>
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<SceneOperationHandle>();
}
// 注意:场景只允许运行在一个资源包内
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<SceneOperationHandle>();
}
// 如果加载的是主场景,则卸载所有缓存的场景
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
/// <summary>
/// 加载资源对象
/// </summary>
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
/// <summary>
/// 加载子资源对象
/// </summary>
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<SubAssetsOperationHandle>();
}
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<AssetBundleLoaderBase> CreateDependAssetBundleLoaders(AssetInfo assetInfo)
internal List<AssetBundleLoaderBase> CreateDependAssetBundleLoaders(AssetInfo assetInfo)
{
BundleInfo[] depends = BundleServices.GetAllDependBundleInfos(assetInfo);
List<AssetBundleLoaderBase> result = new List<AssetBundleLoaderBase>(depends.Length);
@ -262,7 +277,7 @@ namespace YooAsset
}
return result;
}
internal static void RemoveBundleProviders(List<ProviderBase> providers)
internal void RemoveBundleProviders(List<ProviderBase> 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<DebugProviderInfo> GetDebugReportInfos()
{
DebugReport report = new DebugReport();
report.FrameCount = Time.frameCount;
report.BundleCount = _loaders.Count;
report.AssetCount = _providers.Count;
List<DebugProviderInfo> result = new List<DebugProviderInfo>(_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<DebugBundleInfo>();
report.ProviderInfos.Add(providerInfo);
providerInfo.DependBundleInfos = new List<DebugBundleInfo>();
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<BundleInfo> GetLoadedBundleInfos()
internal List<BundleInfo> GetLoadedBundleInfos()
{
List<BundleInfo> result = new List<BundleInfo>(100);
foreach (var bundleLoader in _loaders)

View File

@ -118,7 +118,7 @@ namespace YooAsset
// 卸载子场景
Scene sceneObject = SceneObject;
AssetSystem.UnloadSubScene(Provider);
Provider.Impl.UnloadSubScene(Provider);
{
var operation = new UnloadSceneOperation(sceneObject);
OperationSystem.StartOperation(operation);

View File

@ -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

View File

@ -14,6 +14,11 @@ namespace YooAsset
Failed
}
/// <summary>
/// 所属资源系统
/// </summary>
public AssetSystemImpl Impl { private set; get; }
/// <summary>
/// 资源包文件信息
/// </summary>
@ -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;
@ -148,7 +154,7 @@ namespace YooAsset
}
// 从列表里移除Providers
AssetSystem.RemoveBundleProviders(_providers);
Impl.RemoveBundleProviders(_providers);
_providers.Clear();
}

View File

@ -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

View File

@ -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()

View File

@ -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();
}

View File

@ -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);

View File

@ -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()

View File

@ -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()

View File

@ -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()

View File

@ -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;

View File

@ -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()

View File

@ -22,6 +22,11 @@ namespace YooAsset
/// </summary>
public string ProviderGUID { private set; get; }
/// <summary>
/// 所属资源系统
/// </summary>
public AssetSystemImpl Impl { private set; get; }
/// <summary>
/// 资源信息
/// </summary>
@ -90,8 +95,9 @@ namespace YooAsset
private readonly List<OperationHandleBase> _handles = new List<OperationHandleBase>();
public ProviderBase(string providerGUID, AssetInfo assetInfo)
public ProviderBase(AssetSystemImpl impl, string providerGUID, AssetInfo assetInfo)
{
Impl = impl;
ProviderGUID = providerGUID;
MainAssetInfo = assetInfo;
}

View File

@ -13,13 +13,13 @@ namespace YooAsset
/// <summary>
/// 初始化时的验证级别
/// </summary>
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()
/// <summary>
/// 清空所有数据
/// </summary>
public static void ClearAll()
{
_cachedDic.Clear();
}

View File

@ -7,6 +7,11 @@ namespace YooAsset
[Serializable]
internal class DebugProviderInfo : IComparer<DebugProviderInfo>, IComparable<DebugProviderInfo>
{
/// <summary>
/// 所属的资源包裹
/// </summary>
public string PackageName;
/// <summary>
/// 资源对象路径
/// </summary>
@ -35,7 +40,7 @@ namespace YooAsset
/// <summary>
/// 依赖的资源包列表
/// </summary>
public List<DebugBundleInfo> BundleInfos;
public List<DebugBundleInfo> DependBundleInfos;
public int CompareTo(DebugProviderInfo other)
{

View File

@ -19,17 +19,6 @@ namespace YooAsset
/// </summary>
public int FrameCount;
/// <summary>
/// 资源包总数
/// </summary>
public int BundleCount;
/// <summary>
/// 资源对象总数
/// </summary>
public int AssetCount;
/// <summary>
/// 序列化
/// </summary>

View File

@ -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);
}

View File

@ -14,16 +14,11 @@ namespace YooAsset
{
private static readonly Dictionary<string, DownloaderBase> _downloaderDic = new Dictionary<string, DownloaderBase>();
private static readonly List<string> _removeList = new List<string>(100);
private static int _breakpointResumeFileSize = int.MaxValue;
/// <summary>
/// 初始化
/// 启用断点续传的文件大小
/// </summary>
public static void Initialize(int breakpointResumeFileSize)
{
_breakpointResumeFileSize = breakpointResumeFileSize;
}
public static int BreakpointResumeFileSize { set; get; } = int.MaxValue;
/// <summary>
/// 更新所有下载器
@ -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);

View File

@ -2,6 +2,28 @@
namespace YooAsset
{
/// <summary>
/// 运行模式
/// </summary>
public enum EPlayMode
{
/// <summary>
/// 编辑器下的模拟模式
/// 注意:在初始化的时候自动构建真机模拟环境。
/// </summary>
EditorSimulateMode,
/// <summary>
/// 离线运行模式
/// </summary>
OfflinePlayMode,
/// <summary>
/// 联机运行模式
/// </summary>
HostPlayMode,
}
/// <summary>
/// 初始化参数
/// </summary>
@ -9,6 +31,7 @@ namespace YooAsset
{
/// <summary>
/// 资源定位地址大小写不敏感
/// 注意默认值为False
/// </summary>
public bool LocationToLower = false;
@ -24,13 +47,9 @@ namespace YooAsset
/// <summary>
/// 资源加载的最大数量
/// 注意默认值为MaxValue
/// </summary>
public int AssetLoadingMaxNumber = int.MaxValue;
/// <summary>
/// 异步操作系统每帧允许运行的最大时间切片(单位:毫秒)
/// </summary>
public long OperationSystemMaxTimeSlice = long.MaxValue;
}
/// <summary>
@ -42,7 +61,7 @@ namespace YooAsset
/// 用于模拟运行的资源清单路径
/// 注意:如果路径为空,会自动重新构建补丁清单。
/// </summary>
public string SimulatePatchManifestPath;
public string SimulatePatchManifestPath = string.Empty;
}
/// <summary>
@ -50,10 +69,6 @@ namespace YooAsset
/// </summary>
public class OfflinePlayModeParameters : InitializeParameters
{
/// <summary>
/// 内置的资源包裹名称
/// </summary>
public string BuildinPackageName = string.Empty;
}
/// <summary>
@ -64,29 +79,12 @@ namespace YooAsset
/// <summary>
/// 默认的资源服务器下载地址
/// </summary>
public string DefaultHostServer;
public string DefaultHostServer = string.Empty;
/// <summary>
/// 备用的资源服务器下载地址
/// </summary>
public string FallbackHostServer;
#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;
public string FallbackHostServer = string.Empty;
/// <summary>
/// 查询服务类

View File

@ -10,9 +10,13 @@ namespace YooAsset
// 计时器相关
private static Stopwatch _watch;
private static long _maxTimeSlice;
private static long _frameTime;
/// <summary>
/// 异步操作的最小时间片段
/// </summary>
public static long MaxTimeSlice { set; get; } = long.MaxValue;
/// <summary>
/// 处理器是否繁忙
/// </summary>
@ -20,7 +24,7 @@ namespace YooAsset
{
get
{
return _watch.ElapsedMilliseconds - _frameTime >= _maxTimeSlice;
return _watch.ElapsedMilliseconds - _frameTime >= MaxTimeSlice;
}
}
@ -28,9 +32,8 @@ namespace YooAsset
/// <summary>
/// 初始化异步操作系统
/// </summary>
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;
}
/// <summary>

View File

@ -1,111 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace YooAsset
{
/// <summary>
/// 清理未使用的缓存资源操作类
/// </summary>
public abstract class ClearUnusedCacheFilesOperation : AsyncOperationBase
{
}
/// <summary>
/// 编辑器模式
/// </summary>
internal sealed class EditorPlayModeClearUnusedCacheFilesOperation : ClearUnusedCacheFilesOperation
{
internal override void Start()
{
Status = EOperationStatus.Succeed;
}
internal override void Update()
{
}
}
/// <summary>
/// 离线模式
/// </summary>
internal sealed class OfflinePlayModeClearUnusedCacheFilesOperation : ClearUnusedCacheFilesOperation
{
internal override void Start()
{
Status = EOperationStatus.Succeed;
}
internal override void Update()
{
}
}
/// <summary>
/// 联机模式
/// </summary>
internal sealed class HostPlayModeClearUnusedCacheFilesOperation : ClearUnusedCacheFilesOperation
{
private enum ESteps
{
None,
GetUnusedCacheFiles,
ClearUnusedCacheFiles,
Done,
}
private ESteps _steps = ESteps.None;
private List<string> _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;
}
}
}
}
}

View File

@ -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;
}
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)

View File

@ -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()
/// <summary>
/// 编辑器下模拟构建补丁清单
/// </summary>
public static string SimulateBuild(string packageName, bool enableAddressable)
{
if (_classType == null)
_classType = Assembly.Load("YooAsset.Editor").GetType("YooAsset.Editor.AssetBundleSimulateBuilder");
InvokePublicStaticMethod(_classType, "SimulateBuild");
return GetPatchManifestFilePath();
}
private static string GetPatchManifestFilePath()
{
return (string)InvokePublicStaticMethod(_classType, "GetPatchManifestPath");
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 !"); }
/// <summary>
/// 编辑器下模拟构建补丁清单
/// </summary>
public static string SimulateBuild(string packageName, bool enableAddressable) { throw new System.Exception("Only support in unity editor !"); }
}
}
#endif

View File

@ -57,6 +57,10 @@ namespace YooAsset
{
return _simulatePatchManifest.MappingToAssetPath(location);
}
string IBundleServices.GetPackageName()
{
return _simulatePatchManifest.PackageName;
}
#endregion
}
}

View File

@ -71,35 +71,6 @@ namespace YooAsset
return operation;
}
/// <summary>
/// 获取未被使用的缓存文件路径集合
/// </summary>
public List<string> ClearUnusedCacheFilePaths()
{
string cacheFolderPath = SandboxHelper.GetCacheFolderPath();
if (Directory.Exists(cacheFolderPath) == false)
return new List<string>();
DirectoryInfo directoryInfo = new DirectoryInfo(cacheFolderPath);
FileInfo[] fileInfos = directoryInfo.GetFiles();
List<string> result = new List<string>(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;
}
/// <summary>
/// 创建下载器
/// </summary>
@ -379,6 +350,10 @@ namespace YooAsset
{
return LocalPatchManifest.MappingToAssetPath(location);
}
string IBundleServices.GetPackageName()
{
return LocalPatchManifest.PackageName;
}
#endregion
}
}

View File

@ -85,6 +85,10 @@ namespace YooAsset
{
return _appPatchManifest.MappingToAssetPath(location);
}
string IBundleServices.GetPackageName()
{
return _appPatchManifest.PackageName;
}
#endregion
}
}

View File

@ -27,5 +27,10 @@ namespace YooAsset
/// 映射为资源路径
/// </summary>
string MappingToAssetPath(string location);
/// <summary>
/// 获取所属的包裹名
/// </summary>
string GetPackageName();
}
}

View File

@ -6,6 +6,6 @@ namespace YooAsset
/// <summary>
/// 定位地址转换为资源路径
/// </summary>
string ConvertLocationToAssetPath(string location);
string ConvertLocationToAssetPath(YooAssetPackage package, string location);
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -6,40 +6,23 @@ using UnityEngine.SceneManagement;
namespace YooAsset
{
public class YooAssetComponent
public class YooAssetPackage
{
/// <summary>
/// 运行模式
/// </summary>
public enum EPlayMode
{
/// <summary>
/// 编辑器下的模拟模式
/// 注意:在初始化的时候自动构建真机模拟环境。
/// </summary>
EditorSimulateMode,
/// <summary>
/// 离线运行模式
/// </summary>
OfflinePlayMode,
/// <summary>
/// 联机运行模式
/// </summary>
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;
/// <summary>
/// 包裹名
/// </summary>
public string PackageName { private set; get; }
/// <summary>
/// 是否已经初始化
@ -49,19 +32,141 @@ namespace YooAsset
get { return _isInitialize; }
}
/// <summary>
/// 拒绝外部实例化
/// </summary>
internal YooAssetPackage()
{
}
internal YooAssetPackage(string packageName)
{
PackageName = packageName;
}
/// <summary>
/// 更新资源包裹
/// </summary>
internal void UpdatePackage()
{
if (_assetSystemImpl != null)
_assetSystemImpl.Update();
}
/// <summary>
/// 销毁资源包裹
/// </summary>
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 !");
}
}
/// <summary>
/// 异步初始化
/// </summary>
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<YooAssetDriver>();
UnityEngine.Object.DontDestroyOnLoad(driverGo);
#if DEBUG
driverGo.AddComponent<RemoteDebuggerInRuntime>();
#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
/// <summary>
/// 向网络端请求静态资源版本
/// </summary>
/// <param name="packageName">更新的资源包裹名称</param>
/// <param name="timeout">超时时间默认值60秒</param>
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
/// <summary>
/// 向网络端请求并更新补丁清单
/// </summary>
/// <param name="packageName">更新的资源包裹名称</param>
/// <param name="packageCRC">更新的资源包裹版本</param>
/// <param name="timeout">超时时间默认值60秒</param>
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
/// 弱联网情况下加载补丁清单
/// 注意:当指定版本内容验证失败后会返回失败。
/// </summary>
/// <param name="packageName">指定的资源包裹名称</param>
/// <param name="packageCRC">指定的资源包裹版本</param>
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
}
}
/// <summary>
/// 开启一个异步操作
/// </summary>
/// <param name="operation">异步操作对象</param>
public void StartOperation(GameAsyncOperation operation)
{
OperationSystem.StartOperation(operation);
}
/// <summary>
/// 资源回收(卸载引用计数为零的资源)
/// </summary>
@ -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
/// <summary>
/// 创建资源包裹下载器,用于下载更新指定资源版本所有的资源包文件
/// </summary>
/// <param name="packageName">指定更新的资源包裹名称</param>
/// <param name="packageCRC">指定更新的资源包裹版本</param>
/// <param name="timeout">超时时间</param>
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 沙盒相关
/// <summary>
/// 获取沙盒的根路径
/// </summary>
public string GetSandboxRoot()
{
return PathHelper.MakePersistentRootPath();
}
/// <summary>
/// 清空沙盒目录
/// </summary>
public void ClearSandbox()
{
SandboxHelper.DeleteSandbox();
}
/// <summary>
/// 清空所有的缓存文件
/// </summary>
public void ClearAllCacheFiles()
{
SandboxHelper.DeleteCacheFolder();
}
/// <summary>
/// 清空未被使用的缓存文件
/// </summary>
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();
}
/// <summary>
/// 资源定位地址转换为资源完整路径
/// </summary>
@ -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<DebugProviderInfo> GetDebugReportInfos()
{
return _assetSystemImpl.GetDebugReportInfos();
}
#endregion
#region 私有方法
/// <summary>
/// 资源定位地址转换为资源信息类,失败时内部会发出错误日志。
@ -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)
{

View File

@ -6,17 +6,17 @@ namespace YooAsset
{
void Update()
{
YooAssets.InternalUpdate();
YooAssets.Update();
}
void OnDestroy()
{
YooAssets.InternalDestroy();
YooAssets.Destroy();
}
void OnApplicationQuit()
{
YooAssets.InternalDestroy();
YooAssets.Destroy();
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -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;
/// <summary>
/// 是否已经初始化
/// </summary>
public static bool IsInitialized
{
get { return _mainPackage.IsInitialized; }
}
/// <summary>
/// 异步初始化
/// </summary>
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);
}
/// <summary>
/// 向网络端请求静态资源版本
/// </summary>
/// <param name="timeout">超时时间默认值60秒</param>
public static UpdateStaticVersionOperation UpdateStaticVersionAsync(int timeout = 60)
{
return _mainPackage.UpdateStaticVersionAsync(timeout);
}
/// <summary>
/// 向网络端请求并更新补丁清单
/// </summary>
/// <param name="packageCRC">更新的资源包裹版本</param>
/// <param name="timeout">超时时间默认值60秒</param>
public static UpdateManifestOperation UpdateManifestAsync(string packageCRC, int timeout = 60)
{
return _mainPackage.UpdateManifestAsync(packageCRC, timeout);
}
/// <summary>
/// 弱联网情况下加载补丁清单
/// 注意:当指定版本内容验证失败后会返回失败。
/// </summary>
/// <param name="packageCRC">指定的资源包裹版本</param>
public static UpdateManifestOperation WeaklyUpdateManifestAsync(string packageCRC)
{
return _mainPackage.WeaklyUpdateManifestAsync(packageCRC);
}
/// <summary>
/// 资源回收(卸载引用计数为零的资源)
/// </summary>
public static void UnloadUnusedAssets()
{
_mainPackage.UnloadUnusedAssets();
}
/// <summary>
/// 强制回收所有资源
/// </summary>
public static void ForceUnloadAllAssets()
{
_mainPackage.ForceUnloadAllAssets();
}
#region 资源信息
/// <summary>
/// 是否需要从远端更新下载
/// </summary>
/// <param name="location">资源的定位地址</param>
public static bool IsNeedDownloadFromRemote(string location)
{
return _mainPackage.IsNeedDownloadFromRemote(location);
}
/// <summary>
/// 是否需要从远端更新下载
/// </summary>
/// <param name="location">资源的定位地址</param>
public static bool IsNeedDownloadFromRemote(AssetInfo assetInfo)
{
return _mainPackage.IsNeedDownloadFromRemote(assetInfo);
}
/// <summary>
/// 获取资源信息列表
/// </summary>
/// <param name="tag">资源标签</param>
public static AssetInfo[] GetAssetInfos(string tag)
{
return _mainPackage.GetAssetInfos(tag);
}
/// <summary>
/// 获取资源信息列表
/// </summary>
/// <param name="tags">资源标签列表</param>
public static AssetInfo[] GetAssetInfos(string[] tags)
{
return _mainPackage.GetAssetInfos(tags);
}
/// <summary>
/// 获取资源信息
/// </summary>
/// <param name="location">资源的定位地址</param>
public static AssetInfo GetAssetInfo(string location)
{
return _mainPackage.GetAssetInfo(location);
}
/// <summary>
/// 获取资源路径
/// </summary>
/// <param name="location">资源的定位地址</param>
/// <returns>如果location地址无效则返回空字符串</returns>
public static string GetAssetPath(string location)
{
return _mainPackage.GetAssetPath(location);
}
#endregion
#region 原生文件
/// <summary>
/// 异步获取原生文件
/// </summary>
/// <param name="location">资源的定位地址</param>
/// <param name="copyPath">拷贝路径</param>
public static RawFileOperation GetRawFileAsync(string location, string copyPath = null)
{
return _mainPackage.GetRawFileAsync(location, copyPath);
}
/// <summary>
/// 异步获取原生文件
/// </summary>
/// <param name="assetInfo">资源信息</param>
/// <param name="copyPath">拷贝路径</param>
public static RawFileOperation GetRawFileAsync(AssetInfo assetInfo, string copyPath = null)
{
return _mainPackage.GetRawFileAsync(assetInfo, copyPath);
}
#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)
{
return _mainPackage.LoadSceneAsync(location, sceneMode, activateOnLoad, priority);
}
/// <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)
{
return _mainPackage.LoadSceneAsync(assetInfo, sceneMode, activateOnLoad, priority);
}
#endregion
#region 资源加载
/// <summary>
/// 同步加载资源对象
/// </summary>
/// <param name="assetInfo">资源信息</param>
public static AssetOperationHandle LoadAssetSync(AssetInfo assetInfo)
{
return _mainPackage.LoadAssetSync(assetInfo);
}
/// <summary>
/// 同步加载资源对象
/// </summary>
/// <typeparam name="TObject">资源类型</typeparam>
/// <param name="location">资源的定位地址</param>
public static AssetOperationHandle LoadAssetSync<TObject>(string location) where TObject : UnityEngine.Object
{
return _mainPackage.LoadAssetSync<TObject>(location);
}
/// <summary>
/// 同步加载资源对象
/// </summary>
/// <param name="location">资源的定位地址</param>
/// <param name="type">资源类型</param>
public static AssetOperationHandle LoadAssetSync(string location, System.Type type)
{
return _mainPackage.LoadAssetSync(location, type);
}
/// <summary>
/// 异步加载资源对象
/// </summary>
/// <param name="assetInfo">资源信息</param>
public static AssetOperationHandle LoadAssetAsync(AssetInfo assetInfo)
{
return _mainPackage.LoadAssetAsync(assetInfo);
}
/// <summary>
/// 异步加载资源对象
/// </summary>
/// <typeparam name="TObject">资源类型</typeparam>
/// <param name="location">资源的定位地址</param>
public static AssetOperationHandle LoadAssetAsync<TObject>(string location) where TObject : UnityEngine.Object
{
return _mainPackage.LoadAssetAsync<TObject>(location);
}
/// <summary>
/// 异步加载资源对象
/// </summary>
/// <param name="location">资源的定位地址</param>
/// <param name="type">资源类型</param>
public static AssetOperationHandle LoadAssetAsync(string location, System.Type type)
{
return _mainPackage.LoadAssetAsync(location, type);
}
#endregion
#region 资源加载
/// <summary>
/// 同步加载子资源对象
/// </summary>
/// <param name="assetInfo">资源信息</param>
public static SubAssetsOperationHandle LoadSubAssetsSync(AssetInfo assetInfo)
{
return _mainPackage.LoadSubAssetsSync(assetInfo);
}
/// <summary>
/// 同步加载子资源对象
/// </summary>
/// <typeparam name="TObject">资源类型</typeparam>
/// <param name="location">资源的定位地址</param>
public static SubAssetsOperationHandle LoadSubAssetsSync<TObject>(string location) where TObject : UnityEngine.Object
{
return _mainPackage.LoadSubAssetsSync<TObject>(location);
}
/// <summary>
/// 同步加载子资源对象
/// </summary>
/// <param name="location">资源的定位地址</param>
/// <param name="type">子对象类型</param>
public static SubAssetsOperationHandle LoadSubAssetsSync(string location, System.Type type)
{
return _mainPackage.LoadSubAssetsSync(location, type);
}
/// <summary>
/// 异步加载子资源对象
/// </summary>
/// <param name="assetInfo">资源信息</param>
public static SubAssetsOperationHandle LoadSubAssetsAsync(AssetInfo assetInfo)
{
return _mainPackage.LoadSubAssetsAsync(assetInfo);
}
/// <summary>
/// 异步加载子资源对象
/// </summary>
/// <typeparam name="TObject">资源类型</typeparam>
/// <param name="location">资源的定位地址</param>
public static SubAssetsOperationHandle LoadSubAssetsAsync<TObject>(string location) where TObject : UnityEngine.Object
{
return _mainPackage.LoadSubAssetsAsync<TObject>(location);
}
/// <summary>
/// 异步加载子资源对象
/// </summary>
/// <param name="location">资源的定位地址</param>
/// <param name="type">子对象类型</param>
public static SubAssetsOperationHandle LoadSubAssetsAsync(string location, System.Type type)
{
return _mainPackage.LoadSubAssetsAsync(location, type);
}
#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)
{
return _mainPackage.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)
{
return _mainPackage.CreatePatchDownloader(tags, downloadingMaxNumber, failedTryAgain);
}
/// <summary>
/// 创建补丁下载器,用于下载更新当前资源版本所有的资源包文件
/// </summary>
/// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
/// <param name="failedTryAgain">下载失败的重试次数</param>
public static PatchDownloaderOperation CreatePatchDownloader(int downloadingMaxNumber, int failedTryAgain)
{
return _mainPackage.CreatePatchDownloader(downloadingMaxNumber, failedTryAgain);
}
/// <summary>
/// 创建补丁下载器,用于下载更新指定的资源列表依赖的资源包文件
/// </summary>
/// <param name="locations">资源定位列表</param>
/// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
/// <param name="failedTryAgain">下载失败的重试次数</param>
public static PatchDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain)
{
return _mainPackage.CreateBundleDownloader(locations, downloadingMaxNumber, failedTryAgain);
}
/// <summary>
/// 创建补丁下载器,用于下载更新指定的资源列表依赖的资源包文件
/// </summary>
/// <param name="assetInfos">资源信息列表</param>
/// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
/// <param name="failedTryAgain">下载失败的重试次数</param>
public static PatchDownloaderOperation CreateBundleDownloader(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain)
{
return _mainPackage.CreateBundleDownloader(assetInfos, downloadingMaxNumber, failedTryAgain);
}
#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)
{
return _mainPackage.CreatePatchUnpacker(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)
{
return _mainPackage.CreatePatchUnpacker(tags, unpackingMaxNumber, failedTryAgain);
}
/// <summary>
/// 创建补丁解压器
/// </summary>
/// <param name="unpackingMaxNumber">同时解压的最大文件数</param>
/// <param name="failedTryAgain">解压失败的重试次数</param>
public static PatchUnpackerOperation CreatePatchUnpacker(int unpackingMaxNumber, int failedTryAgain)
{
return _mainPackage.CreatePatchUnpacker(unpackingMaxNumber, failedTryAgain);
}
#endregion
#region 包裹更新
/// <summary>
/// 创建资源包裹下载器,用于下载更新指定资源版本所有的资源包文件
/// </summary>
/// <param name="packageCRC">指定更新的资源包裹版本</param>
/// <param name="timeout">超时时间</param>
public static UpdatePackageOperation UpdatePackageAsync(string packageCRC, int timeout = 60)
{
return _mainPackage.UpdatePackageAsync(packageCRC, timeout);
}
#endregion
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 5d188c50fd00bf941b2eeebb374dc0d1
guid: 2db868c2aaaee8e42a7f035117e747c4
MonoImporter:
externalObjects: {}
serializedVersion: 2