Initial commit
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
public class AssetBundleInfo
|
||||
{
|
||||
private readonly PatchBundle _patchBundle;
|
||||
|
||||
/// <summary>
|
||||
/// 资源包名称
|
||||
/// </summary>
|
||||
public string BundleName { private set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 本地存储的路径
|
||||
/// </summary>
|
||||
public string LocalPath { private set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 远端下载地址
|
||||
/// </summary>
|
||||
public string RemoteMainURL { private set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 远端下载备用地址
|
||||
/// </summary>
|
||||
public string RemoteFallbackURL { private set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 文件哈希值
|
||||
/// </summary>
|
||||
public string Hash
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_patchBundle == null)
|
||||
return string.Empty;
|
||||
else
|
||||
return _patchBundle.Hash;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 校验的CRC
|
||||
/// </summary>
|
||||
public string CRC
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_patchBundle == null)
|
||||
return string.Empty;
|
||||
else
|
||||
return _patchBundle.CRC;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 文件大小
|
||||
/// </summary>
|
||||
public long SizeBytes
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_patchBundle == null)
|
||||
return 0;
|
||||
else
|
||||
return _patchBundle.SizeBytes;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 资源版本
|
||||
/// </summary>
|
||||
public int Version
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_patchBundle == null)
|
||||
return 0;
|
||||
else
|
||||
return _patchBundle.Version;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否为加密文件
|
||||
/// </summary>
|
||||
public bool IsEncrypted
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_patchBundle == null)
|
||||
return false;
|
||||
else
|
||||
return _patchBundle.IsEncrypted;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否为原生文件
|
||||
/// </summary>
|
||||
public bool IsRawFile
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_patchBundle == null)
|
||||
return false;
|
||||
else
|
||||
return _patchBundle.IsRawFile;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private AssetBundleInfo()
|
||||
{
|
||||
}
|
||||
internal AssetBundleInfo(PatchBundle patchBundle, string localPath, string mainURL, string fallbackURL)
|
||||
{
|
||||
_patchBundle = patchBundle;
|
||||
BundleName = patchBundle.BundleName;
|
||||
LocalPath = localPath;
|
||||
RemoteMainURL = mainURL;
|
||||
RemoteFallbackURL = fallbackURL;
|
||||
}
|
||||
internal AssetBundleInfo(PatchBundle patchBundle, string localPath)
|
||||
{
|
||||
_patchBundle = patchBundle;
|
||||
BundleName = patchBundle.BundleName;
|
||||
LocalPath = localPath;
|
||||
RemoteMainURL = string.Empty;
|
||||
RemoteFallbackURL = string.Empty;
|
||||
}
|
||||
internal AssetBundleInfo(string bundleName, string localPath)
|
||||
{
|
||||
_patchBundle = null;
|
||||
BundleName = bundleName;
|
||||
LocalPath = localPath;
|
||||
RemoteMainURL = string.Empty;
|
||||
RemoteFallbackURL = string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否为JAR包内文件
|
||||
/// </summary>
|
||||
public bool IsBuildinJarFile()
|
||||
{
|
||||
return LocalPath.StartsWith("jar:");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2939a2b9862f05545969cc5fcb378bd8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,185 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
public struct AssetOperationHandle : IEnumerator
|
||||
{
|
||||
private IAssetProvider _provider;
|
||||
|
||||
internal AssetOperationHandle(IAssetProvider provider)
|
||||
{
|
||||
_provider = provider;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 句柄是否有效(AssetFileLoader销毁会导致所有句柄失效)
|
||||
/// </summary>
|
||||
public bool IsValid
|
||||
{
|
||||
get
|
||||
{
|
||||
return _provider != null && _provider.IsValid;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 当前的加载状态
|
||||
/// </summary>
|
||||
public EAssetStates States
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsValid == false)
|
||||
return EAssetStates.None;
|
||||
return _provider.States;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载进度
|
||||
/// </summary>
|
||||
public float Progress
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsValid == false)
|
||||
return 0;
|
||||
return _provider.Progress;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否加载完毕
|
||||
/// </summary>
|
||||
public bool IsDone
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsValid == false)
|
||||
return false;
|
||||
return _provider.IsDone;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 完成委托
|
||||
/// </summary>
|
||||
public event System.Action<AssetOperationHandle> Completed
|
||||
{
|
||||
add
|
||||
{
|
||||
if (IsValid == false)
|
||||
throw new System.Exception($"{nameof(AssetOperationHandle)} is invalid");
|
||||
if (_provider.IsDone)
|
||||
value.Invoke(this);
|
||||
else
|
||||
_provider.Callback += value;
|
||||
}
|
||||
remove
|
||||
{
|
||||
if (IsValid == false)
|
||||
throw new System.Exception($"{nameof(AssetOperationHandle)} is invalid");
|
||||
_provider.Callback -= value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 资源对象
|
||||
/// </summary>
|
||||
public UnityEngine.Object AssetObject
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsValid == false)
|
||||
return null;
|
||||
return _provider.AssetObject;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 资源对象集合
|
||||
/// </summary>
|
||||
public UnityEngine.Object[] AllAssets
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsValid == false)
|
||||
return null;
|
||||
return _provider.AllAssets;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 扩展的实例对象
|
||||
/// </summary>
|
||||
public IAssetInstance AssetInstance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsValid == false)
|
||||
return null;
|
||||
return _provider.AssetInstance;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化的游戏对象(只限于请求的资源对象类型为GameObject)
|
||||
/// </summary>
|
||||
public GameObject InstantiateObject
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsValid == false)
|
||||
return null;
|
||||
if (_provider.AssetObject == null)
|
||||
return null;
|
||||
return UnityEngine.Object.Instantiate(_provider.AssetObject as GameObject);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放资源句柄
|
||||
/// </summary>
|
||||
public void Release()
|
||||
{
|
||||
if (IsValid == false)
|
||||
return;
|
||||
_provider.Release();
|
||||
_provider = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 等待异步执行完毕
|
||||
/// </summary>
|
||||
public void WaitForAsyncComplete()
|
||||
{
|
||||
if (IsValid == false)
|
||||
return;
|
||||
_provider.WaitForAsyncComplete();
|
||||
}
|
||||
|
||||
#region 异步操作相关
|
||||
/// <summary>
|
||||
/// 异步操作任务
|
||||
/// </summary>
|
||||
public System.Threading.Tasks.Task<object> Task
|
||||
{
|
||||
get { return _provider.Task; }
|
||||
}
|
||||
|
||||
// 协程相关
|
||||
bool IEnumerator.MoveNext()
|
||||
{
|
||||
return !IsDone;
|
||||
}
|
||||
void IEnumerator.Reset()
|
||||
{
|
||||
}
|
||||
object IEnumerator.Current
|
||||
{
|
||||
get { return AssetObject; }
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6b766125ab790154ca132adb5c77333d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,123 @@
|
||||
using System.IO;
|
||||
using YooAsset.Utility;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal static class AssetPathHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取规范化的路径
|
||||
/// </summary>
|
||||
public static string GetRegularPath(string path)
|
||||
{
|
||||
return path.Replace('\\', '/').Replace("\\", "/"); //替换为Linux路径格式
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取文件所在的目录路径(Linux格式)
|
||||
/// </summary>
|
||||
public static string GetDirectory(string filePath)
|
||||
{
|
||||
string directory = Path.GetDirectoryName(filePath);
|
||||
return GetRegularPath(directory);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取基于流文件夹的加载路径
|
||||
/// </summary>
|
||||
public static string MakeStreamingLoadPath(string path)
|
||||
{
|
||||
return StringUtility.Format("{0}/{1}", UnityEngine.Application.streamingAssetsPath, path);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取基于沙盒文件夹的加载路径
|
||||
/// </summary>
|
||||
public static string MakePersistentLoadPath(string path)
|
||||
{
|
||||
string root = MakePersistentRootPath();
|
||||
return StringUtility.Format("{0}/{1}", root, path);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取沙盒文件夹路径
|
||||
/// </summary>
|
||||
public static string MakePersistentRootPath()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
// 注意:为了方便调试查看,编辑器下把存储目录放到项目里
|
||||
string projectPath = GetDirectory(UnityEngine.Application.dataPath);
|
||||
return StringUtility.Format("{0}/Sandbox", projectPath);
|
||||
#else
|
||||
return StringUtility.Format("{0}/Sandbox", UnityEngine.Application.persistentDataPath);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取网络资源加载路径
|
||||
/// </summary>
|
||||
public static string ConvertToWWWPath(string path)
|
||||
{
|
||||
// 注意:WWW加载方式,必须要在路径前面加file://
|
||||
#if UNITY_EDITOR
|
||||
return StringUtility.Format("file:///{0}", path);
|
||||
#elif UNITY_IPHONE
|
||||
return StringUtility.Format("file://{0}", path);
|
||||
#elif UNITY_ANDROID
|
||||
return path;
|
||||
#elif UNITY_STANDALONE
|
||||
return StringUtility.Format("file:///{0}", path);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 合并资源路径
|
||||
/// </summary>
|
||||
internal static string CombineAssetPath(string root, string location)
|
||||
{
|
||||
if (string.IsNullOrEmpty(root))
|
||||
return location;
|
||||
else
|
||||
return $"{root}/{location}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取AssetDatabase的加载路径
|
||||
/// </summary>
|
||||
internal static string FindDatabaseAssetPath(string filePath)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (File.Exists(filePath))
|
||||
return filePath;
|
||||
|
||||
// AssetDatabase加载资源需要提供文件后缀格式,然而资源定位地址并没有文件格式信息。
|
||||
// 所以我们通过查找该文件所在文件夹内同名的首个文件来确定AssetDatabase的加载路径。
|
||||
// 注意:AssetDatabase.FindAssets() 返回文件内包括递归文件夹内所有资源的GUID
|
||||
string fileName = Path.GetFileName(filePath);
|
||||
string directory = GetDirectory(filePath);
|
||||
string[] guids = UnityEditor.AssetDatabase.FindAssets(string.Empty, new[] { directory });
|
||||
for (int i = 0; i < guids.Length; i++)
|
||||
{
|
||||
string assetPath = UnityEditor.AssetDatabase.GUIDToAssetPath(guids[i]);
|
||||
|
||||
if (UnityEditor.AssetDatabase.IsValidFolder(assetPath))
|
||||
continue;
|
||||
|
||||
string assetDirectory = GetDirectory(assetPath);
|
||||
if (assetDirectory != directory)
|
||||
continue;
|
||||
|
||||
string assetName = Path.GetFileNameWithoutExtension(assetPath);
|
||||
if (assetName == fileName)
|
||||
return assetPath;
|
||||
}
|
||||
|
||||
// 没有找到同名的资源文件
|
||||
Logger.Warning($"Not found asset : {filePath}");
|
||||
return filePath;
|
||||
#else
|
||||
throw new System.NotImplementedException();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9581e7c53fe081749ab849ec6e2be3d6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,326 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal static class AssetSystem
|
||||
{
|
||||
private static readonly List<BundleFileLoader> _loaders = new List<BundleFileLoader>(1000);
|
||||
private static readonly List<AssetProviderBase> _providers = new List<AssetProviderBase>(1000);
|
||||
|
||||
/// <summary>
|
||||
/// 在编辑器下模拟运行
|
||||
/// </summary>
|
||||
public static bool SimulationOnEditor { private set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 运行时的最大加载个数
|
||||
/// </summary>
|
||||
public static int AssetLoadingMaxNumber { private set; get; }
|
||||
|
||||
public static IDecryptServices DecryptServices { private set; get; }
|
||||
public static IBundleServices BundleServices { private set; get; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 初始化资源系统
|
||||
/// 注意:在使用AssetSystem之前需要初始化
|
||||
/// </summary>
|
||||
public static void Initialize(bool simulationOnEditor, int assetLoadingMaxNumber, IDecryptServices decryptServices, IBundleServices bundleServices)
|
||||
{
|
||||
SimulationOnEditor = simulationOnEditor;
|
||||
AssetLoadingMaxNumber = assetLoadingMaxNumber;
|
||||
DecryptServices = decryptServices;
|
||||
BundleServices = bundleServices;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 轮询更新
|
||||
/// </summary>
|
||||
public static void UpdatePoll()
|
||||
{
|
||||
// 更新加载器
|
||||
foreach (var loader in _loaders)
|
||||
{
|
||||
loader.Update();
|
||||
}
|
||||
|
||||
// 更新资源提供者
|
||||
// 注意:循环更新的时候,可能会扩展列表
|
||||
// 注意:不能限制场景对象的加载
|
||||
int loadingCount = 0;
|
||||
for (int i = 0; i < _providers.Count; i++)
|
||||
{
|
||||
var provider = _providers[i];
|
||||
if (provider.IsSceneProvider())
|
||||
{
|
||||
provider.Update();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (loadingCount < AssetLoadingMaxNumber)
|
||||
provider.Update();
|
||||
|
||||
if (provider.IsDone == false)
|
||||
loadingCount++;
|
||||
}
|
||||
}
|
||||
|
||||
// 注意:需要立刻卸载场景
|
||||
if (SimulationOnEditor)
|
||||
{
|
||||
for (int i = _providers.Count - 1; i >= 0; i--)
|
||||
{
|
||||
AssetProviderBase provider = _providers[i];
|
||||
if (provider.IsSceneProvider() && provider.CanDestroy())
|
||||
{
|
||||
provider.Destory();
|
||||
_providers.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = _loaders.Count - 1; i >= 0; i--)
|
||||
{
|
||||
BundleFileLoader loader = _loaders[i];
|
||||
if (loader.IsSceneLoader())
|
||||
{
|
||||
loader.TryDestroyAllProviders();
|
||||
}
|
||||
}
|
||||
for (int i = _loaders.Count - 1; i >= 0; i--)
|
||||
{
|
||||
BundleFileLoader loader = _loaders[i];
|
||||
if (loader.IsSceneLoader() && loader.CanDestroy())
|
||||
{
|
||||
loader.Destroy(false);
|
||||
_loaders.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 资源回收(卸载引用计数为零的资源)
|
||||
/// </summary>
|
||||
public static void UnloadUnusedAssets()
|
||||
{
|
||||
if (SimulationOnEditor)
|
||||
{
|
||||
for (int i = _providers.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (_providers[i].CanDestroy())
|
||||
{
|
||||
_providers[i].Destory();
|
||||
_providers.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = _loaders.Count - 1; i >= 0; i--)
|
||||
{
|
||||
BundleFileLoader loader = _loaders[i];
|
||||
loader.TryDestroyAllProviders();
|
||||
}
|
||||
for (int i = _loaders.Count - 1; i >= 0; i--)
|
||||
{
|
||||
BundleFileLoader loader = _loaders[i];
|
||||
if (loader.CanDestroy())
|
||||
{
|
||||
loader.Destroy(false);
|
||||
_loaders.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 强制回收所有资源
|
||||
/// </summary>
|
||||
public static void ForceUnloadAllAssets()
|
||||
{
|
||||
foreach (var provider in _providers)
|
||||
{
|
||||
provider.Destory();
|
||||
}
|
||||
_providers.Clear();
|
||||
|
||||
foreach (var loader in _loaders)
|
||||
{
|
||||
loader.Destroy(true);
|
||||
}
|
||||
_loaders.Clear();
|
||||
|
||||
// 注意:调用底层接口释放所有资源
|
||||
Resources.UnloadUnusedAssets();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载场景
|
||||
/// </summary>
|
||||
/// <param name="scenePath">场景名称</param>
|
||||
public static AssetOperationHandle LoadSceneAsync(string scenePath, SceneInstanceParam instanceParam)
|
||||
{
|
||||
AssetProviderBase provider = TryGetAssetProvider(scenePath);
|
||||
if (provider == null)
|
||||
{
|
||||
if (SimulationOnEditor)
|
||||
provider = new DatabaseSceneProvider(scenePath, instanceParam);
|
||||
else
|
||||
provider = new BundledSceneProvider(scenePath, instanceParam);
|
||||
_providers.Add(provider);
|
||||
}
|
||||
|
||||
// 引用计数增加
|
||||
provider.Reference();
|
||||
return provider.Handle;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载资源对象
|
||||
/// </summary>
|
||||
/// <param name="assetPath">资源路径</param>
|
||||
/// <param name="assetType">资源类型</param>
|
||||
public static AssetOperationHandle LoadAssetAsync(string assetPath, System.Type assetType)
|
||||
{
|
||||
AssetProviderBase provider = TryGetAssetProvider(assetPath);
|
||||
if (provider == null)
|
||||
{
|
||||
if (SimulationOnEditor)
|
||||
provider = new DatabaseAssetProvider(assetPath, assetType);
|
||||
else
|
||||
provider = new BundledAssetProvider(assetPath, assetType);
|
||||
_providers.Add(provider);
|
||||
}
|
||||
|
||||
// 引用计数增加
|
||||
provider.Reference();
|
||||
return provider.Handle;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载所有子资源对象
|
||||
/// </summary>
|
||||
/// <param name="assetPath">资源路径</param>
|
||||
/// <param name="assetType">资源类型</param>、
|
||||
public static AssetOperationHandle LoadSubAssetsAsync(string assetPath, System.Type assetType)
|
||||
{
|
||||
AssetProviderBase provider = TryGetAssetProvider(assetPath);
|
||||
if (provider == null)
|
||||
{
|
||||
if (SimulationOnEditor)
|
||||
provider = new DatabaseSubAssetsProvider(assetPath, assetType);
|
||||
else
|
||||
provider = new BundledSubAssetsProvider(assetPath, assetType);
|
||||
_providers.Add(provider);
|
||||
}
|
||||
|
||||
// 引用计数增加
|
||||
provider.Reference();
|
||||
return provider.Handle;
|
||||
}
|
||||
|
||||
|
||||
internal static BundleFileLoader CreateOwnerBundleLoader(string assetPath)
|
||||
{
|
||||
string bundleName = BundleServices.GetAssetBundleName(assetPath);
|
||||
AssetBundleInfo bundleInfo = BundleServices.GetAssetBundleInfo(bundleName);
|
||||
return CreateBundleFileLoaderInternal(bundleInfo);
|
||||
}
|
||||
internal static List<BundleFileLoader> CreateDependBundleLoaders(string assetPath)
|
||||
{
|
||||
List<BundleFileLoader> result = new List<BundleFileLoader>();
|
||||
string[] depends = BundleServices.GetAllDependencies(assetPath);
|
||||
if (depends != null)
|
||||
{
|
||||
foreach (var dependBundleName in depends)
|
||||
{
|
||||
AssetBundleInfo dependBundleInfo = BundleServices.GetAssetBundleInfo(dependBundleName);
|
||||
BundleFileLoader dependLoader = CreateBundleFileLoaderInternal(dependBundleInfo);
|
||||
result.Add(dependLoader);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
internal static void RemoveBundleProviders(List<AssetProviderBase> providers)
|
||||
{
|
||||
foreach (var provider in providers)
|
||||
{
|
||||
_providers.Remove(provider);
|
||||
}
|
||||
}
|
||||
|
||||
private static BundleFileLoader CreateBundleFileLoaderInternal(AssetBundleInfo bundleInfo)
|
||||
{
|
||||
// 如果加载器已经存在
|
||||
BundleFileLoader loader = TryGetBundleFileLoader(bundleInfo.BundleName);
|
||||
if (loader != null)
|
||||
return loader;
|
||||
|
||||
// 新增下载需求
|
||||
loader = new BundleFileLoader(bundleInfo);
|
||||
_loaders.Add(loader);
|
||||
return loader;
|
||||
}
|
||||
private static BundleFileLoader TryGetBundleFileLoader(string bundleName)
|
||||
{
|
||||
BundleFileLoader loader = null;
|
||||
for (int i = 0; i < _loaders.Count; i++)
|
||||
{
|
||||
BundleFileLoader temp = _loaders[i];
|
||||
if (temp.BundleInfo.BundleName.Equals(bundleName))
|
||||
{
|
||||
loader = temp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return loader;
|
||||
}
|
||||
private static AssetProviderBase TryGetAssetProvider(string assetPath)
|
||||
{
|
||||
AssetProviderBase provider = null;
|
||||
for (int i = 0; i < _providers.Count; i++)
|
||||
{
|
||||
AssetProviderBase temp = _providers[i];
|
||||
if (temp.AssetPath.Equals(assetPath))
|
||||
{
|
||||
provider = temp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return provider;
|
||||
}
|
||||
|
||||
#region 调试专属方法
|
||||
internal static void GetDebugSummy(DebugSummy summy)
|
||||
{
|
||||
summy.ClearAll();
|
||||
summy.BundleCount = _loaders.Count;
|
||||
summy.AssetCount = _providers.Count;
|
||||
|
||||
foreach (var provider in _providers)
|
||||
{
|
||||
DebugSummy.ProviderInfo providerInfo = new DebugSummy.ProviderInfo();
|
||||
providerInfo.AssetPath = provider.AssetPath;
|
||||
providerInfo.RefCount = provider.RefCount;
|
||||
providerInfo.States = provider.States;
|
||||
providerInfo.BundleInfos.Clear();
|
||||
summy.ProviderInfos.Add(providerInfo);
|
||||
|
||||
if (provider is BundledProvider)
|
||||
{
|
||||
BundledProvider temp = provider as BundledProvider;
|
||||
temp.GetBundleDebugInfos(providerInfo.BundleInfos);
|
||||
}
|
||||
}
|
||||
|
||||
// 重新排序
|
||||
summy.ProviderInfos.Sort();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3ecc5913758e8234fbba5ef2fd192f86
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,19 @@
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 解密方法
|
||||
/// </summary>
|
||||
public enum EDecryptMethod
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取解密的数据偏移
|
||||
/// </summary>
|
||||
GetDecryptOffset,
|
||||
|
||||
/// <summary>
|
||||
/// 获取解密的字节数据
|
||||
/// </summary>
|
||||
GetDecryptBinary,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6a446add12cd4554a9bd178a7ef2db55
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,21 @@
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
public interface IBundleServices
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取AssetBundle的信息
|
||||
/// </summary>
|
||||
AssetBundleInfo GetAssetBundleInfo(string bundleName);
|
||||
|
||||
/// <summary>
|
||||
/// 获取资源所属的资源包名称
|
||||
/// </summary>
|
||||
string GetAssetBundleName(string assetPath);
|
||||
|
||||
/// <summary>
|
||||
/// 获取资源依赖的所有AssetBundle列表
|
||||
/// </summary>
|
||||
string[] GetAllDependencies(string assetPath);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 70420213c551a2b4b8cf014067699b07
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,18 @@
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
public interface IDecryptServices
|
||||
{
|
||||
EDecryptMethod DecryptType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取解密的数据偏移
|
||||
/// </summary>
|
||||
ulong GetDecryptOffset(AssetBundleInfo bundleInfo);
|
||||
|
||||
/// <summary>
|
||||
/// 获取解密的字节数据
|
||||
/// </summary>
|
||||
byte[] GetDecryptBinary(AssetBundleInfo bundleInfo);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3444518ef1b082a46a9855fef4f69c86
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a9b18d6c47ddf9b4296619d3d50ff826
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,7 @@
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
public interface IAssetInstance
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e1d0cfc6a2d5b1f4d9b9ac8f93682497
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,7 @@
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
public interface IAssetParam
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a8995b1e1e511714b948f7fa6275028e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,63 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 扩展的场景实例对象
|
||||
/// </summary>
|
||||
public class SceneInstance : IAssetInstance
|
||||
{
|
||||
private readonly AsyncOperation _asyncOp;
|
||||
|
||||
public SceneInstance(AsyncOperation op)
|
||||
{
|
||||
_asyncOp = op;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// UnityEngine场景对象
|
||||
/// </summary>
|
||||
public UnityEngine.SceneManagement.Scene Scene { internal set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 激活场景
|
||||
/// </summary>
|
||||
public bool Activate()
|
||||
{
|
||||
if (Scene == null)
|
||||
return false;
|
||||
|
||||
if (Scene.IsValid() && Scene.isLoaded)
|
||||
{
|
||||
return SceneManager.SetActiveScene(Scene);
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Warning($"Scene is invalid or not loaded : {Scene.name}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载场景实例对象需要提供的参数类
|
||||
/// </summary>
|
||||
public class SceneInstanceParam : IAssetParam
|
||||
{
|
||||
/// <summary>
|
||||
/// 加载模式
|
||||
/// </summary>
|
||||
public LoadSceneMode LoadMode { set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 物理模式
|
||||
/// </summary>
|
||||
//public LocalPhysicsMode PhysicsMode { set; get;}
|
||||
|
||||
/// <summary>
|
||||
/// 加载完毕时是否主动激活
|
||||
/// </summary>
|
||||
public bool ActivateOnLoad { set; get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0ab6e7cef318cff4684ed733679bca0e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 58462a7dcef164e43878a037395d4417
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,319 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal class BundleFileLoader
|
||||
{
|
||||
/// <summary>
|
||||
/// 资源文件信息
|
||||
/// </summary>
|
||||
public AssetBundleInfo BundleInfo { private set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 引用计数
|
||||
/// </summary>
|
||||
public int RefCount { private set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 加载状态
|
||||
/// </summary>
|
||||
public ELoaderStates States { private set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否已经销毁
|
||||
/// </summary>
|
||||
public bool IsDestroyed { private set; get; } = false;
|
||||
|
||||
private readonly List<AssetProviderBase> _providers = new List<AssetProviderBase>(100);
|
||||
private bool _isWaitForAsyncComplete = false;
|
||||
private bool _isShowWaitForAsyncError = false;
|
||||
private FileDownloader _fileDownloader;
|
||||
private AssetBundleCreateRequest _cacheRequest;
|
||||
internal AssetBundle CacheBundle { private set; get; }
|
||||
|
||||
|
||||
public BundleFileLoader(AssetBundleInfo bundleInfo)
|
||||
{
|
||||
BundleInfo = bundleInfo;
|
||||
RefCount = 0;
|
||||
States = ELoaderStates.None;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否为场景加载器
|
||||
/// </summary>
|
||||
public bool IsSceneLoader()
|
||||
{
|
||||
foreach (var provider in _providers)
|
||||
{
|
||||
if (provider is BundledSceneProvider)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加附属的资源提供者
|
||||
/// </summary>
|
||||
public void AddProvider(AssetProviderBase provider)
|
||||
{
|
||||
if (_providers.Contains(provider) == false)
|
||||
_providers.Add(provider);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 引用(引用计数递加)
|
||||
/// </summary>
|
||||
public void Reference()
|
||||
{
|
||||
RefCount++;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放(引用计数递减)
|
||||
/// </summary>
|
||||
public void Release()
|
||||
{
|
||||
RefCount--;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 轮询更新
|
||||
/// </summary>
|
||||
public void Update()
|
||||
{
|
||||
// 如果资源文件加载完毕
|
||||
if (IsDone())
|
||||
return;
|
||||
|
||||
if (States == ELoaderStates.None)
|
||||
{
|
||||
// 检测加载地址是否为空
|
||||
if (string.IsNullOrEmpty(BundleInfo.LocalPath))
|
||||
{
|
||||
States = ELoaderStates.Fail;
|
||||
return;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(BundleInfo.RemoteMainURL))
|
||||
States = ELoaderStates.LoadFile;
|
||||
else
|
||||
States = ELoaderStates.Download;
|
||||
}
|
||||
|
||||
// 1. 从服务器下载
|
||||
if (States == ELoaderStates.Download)
|
||||
{
|
||||
int failedTryAgain = int.MaxValue;
|
||||
_fileDownloader = DownloadSystem.BeginDownload(BundleInfo, failedTryAgain);
|
||||
States = ELoaderStates.CheckDownload;
|
||||
}
|
||||
|
||||
// 2. 检测服务器下载结果
|
||||
if (States == ELoaderStates.CheckDownload)
|
||||
{
|
||||
if (_fileDownloader.IsDone() == false)
|
||||
return;
|
||||
|
||||
if (_fileDownloader.HasError())
|
||||
{
|
||||
_fileDownloader.ReportError();
|
||||
States = ELoaderStates.Fail;
|
||||
}
|
||||
else
|
||||
{
|
||||
States = ELoaderStates.LoadFile;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 加载AssetBundle
|
||||
if (States == ELoaderStates.LoadFile)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
// 注意:Unity2017.4编辑器模式下,如果AssetBundle文件不存在会导致编辑器崩溃,这里做了预判。
|
||||
if (System.IO.File.Exists(BundleInfo.LocalPath) == false)
|
||||
{
|
||||
Logger.Warning($"Not found assetBundle file : {BundleInfo.LocalPath}");
|
||||
States = ELoaderStates.Fail;
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Load assetBundle file
|
||||
if (BundleInfo.IsEncrypted)
|
||||
{
|
||||
if (AssetSystem.DecryptServices == null)
|
||||
throw new Exception($"{nameof(BundleFileLoader)} need IDecryptServices : {BundleInfo.BundleName}");
|
||||
|
||||
EDecryptMethod decryptType = AssetSystem.DecryptServices.DecryptType;
|
||||
if (decryptType == EDecryptMethod.GetDecryptOffset)
|
||||
{
|
||||
ulong offset = AssetSystem.DecryptServices.GetDecryptOffset(BundleInfo);
|
||||
if (_isWaitForAsyncComplete)
|
||||
CacheBundle = AssetBundle.LoadFromFile(BundleInfo.LocalPath, 0, offset);
|
||||
else
|
||||
_cacheRequest = AssetBundle.LoadFromFileAsync(BundleInfo.LocalPath, 0, offset);
|
||||
}
|
||||
else if (decryptType == EDecryptMethod.GetDecryptBinary)
|
||||
{
|
||||
byte[] binary = AssetSystem.DecryptServices.GetDecryptBinary(BundleInfo);
|
||||
if (_isWaitForAsyncComplete)
|
||||
CacheBundle = AssetBundle.LoadFromMemory(binary);
|
||||
else
|
||||
_cacheRequest = AssetBundle.LoadFromMemoryAsync(binary);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotImplementedException($"{decryptType}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_isWaitForAsyncComplete)
|
||||
CacheBundle = AssetBundle.LoadFromFile(BundleInfo.LocalPath);
|
||||
else
|
||||
_cacheRequest = AssetBundle.LoadFromFileAsync(BundleInfo.LocalPath);
|
||||
}
|
||||
States = ELoaderStates.CheckFile;
|
||||
}
|
||||
|
||||
// 4. 检测AssetBundle加载结果
|
||||
if (States == ELoaderStates.CheckFile)
|
||||
{
|
||||
if (_cacheRequest != null)
|
||||
{
|
||||
if (_isWaitForAsyncComplete)
|
||||
{
|
||||
// 强制挂起主线程(注意:该操作会很耗时)
|
||||
Logger.Warning("Suspend the main thread to load unity bundle.");
|
||||
CacheBundle = _cacheRequest.assetBundle;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_cacheRequest.isDone == false)
|
||||
return;
|
||||
CacheBundle = _cacheRequest.assetBundle;
|
||||
}
|
||||
}
|
||||
|
||||
// Check error
|
||||
if (CacheBundle == null)
|
||||
{
|
||||
Logger.Error($"Failed to load assetBundle file : {BundleInfo.BundleName}");
|
||||
States = ELoaderStates.Fail;
|
||||
}
|
||||
else
|
||||
{
|
||||
States = ELoaderStates.Success;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 销毁
|
||||
/// </summary>
|
||||
public void Destroy(bool forceDestroy)
|
||||
{
|
||||
IsDestroyed = true;
|
||||
|
||||
// Check fatal
|
||||
if (forceDestroy == false)
|
||||
{
|
||||
if (RefCount > 0)
|
||||
throw new Exception($"Bundle file loader ref is not zero : {BundleInfo.BundleName}");
|
||||
if (IsDone() == false)
|
||||
throw new Exception($"Bundle file loader is not done : {BundleInfo.BundleName}");
|
||||
}
|
||||
|
||||
if (CacheBundle != null)
|
||||
{
|
||||
CacheBundle.Unload(true);
|
||||
CacheBundle = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否完毕(无论成功或失败)
|
||||
/// </summary>
|
||||
public bool IsDone()
|
||||
{
|
||||
return States == ELoaderStates.Success || States == ELoaderStates.Fail;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否可以销毁
|
||||
/// </summary>
|
||||
public bool CanDestroy()
|
||||
{
|
||||
if (IsDone() == false)
|
||||
return false;
|
||||
|
||||
return RefCount <= 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在满足条件的前提下,销毁所有资源提供者
|
||||
/// </summary>
|
||||
public void TryDestroyAllProviders()
|
||||
{
|
||||
if (IsDone() == false)
|
||||
return;
|
||||
|
||||
// 注意:必须等待所有Provider可以销毁的时候,才可以释放Bundle文件。
|
||||
foreach (var provider in _providers)
|
||||
{
|
||||
if (provider.CanDestroy() == false)
|
||||
return;
|
||||
}
|
||||
|
||||
// 除了自己没有其它引用
|
||||
if (RefCount > _providers.Count)
|
||||
return;
|
||||
|
||||
// 销毁所有Providers
|
||||
foreach (var provider in _providers)
|
||||
{
|
||||
provider.Destory();
|
||||
}
|
||||
|
||||
// 从列表里移除Providers
|
||||
AssetSystem.RemoveBundleProviders(_providers);
|
||||
_providers.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 主线程等待异步操作完毕
|
||||
/// </summary>
|
||||
public void WaitForAsyncComplete()
|
||||
{
|
||||
_isWaitForAsyncComplete = true;
|
||||
|
||||
int frame = 1000;
|
||||
while (true)
|
||||
{
|
||||
// 保险机制
|
||||
// 注意:如果需要从WEB端下载资源,可能会触发保险机制!
|
||||
frame--;
|
||||
if (frame == 0)
|
||||
{
|
||||
if (_isShowWaitForAsyncError == false)
|
||||
{
|
||||
_isShowWaitForAsyncError = true;
|
||||
Logger.Error($"WaitForAsyncComplete failed ! BundleName : {BundleInfo.BundleName} States : {States}");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// 驱动流程
|
||||
Update();
|
||||
|
||||
// 完成后退出
|
||||
if (IsDone())
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 64d0a51f59c2a964eaf97b87db47428f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal class DependBundleGrouper
|
||||
{
|
||||
/// <summary>
|
||||
/// 依赖的资源包加载器列表
|
||||
/// </summary>
|
||||
private readonly List<BundleFileLoader> _dependBundles;
|
||||
|
||||
public DependBundleGrouper(string assetPath)
|
||||
{
|
||||
_dependBundles = AssetSystem.CreateDependBundleLoaders(assetPath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否已经完成(无论成功或失败)
|
||||
/// </summary>
|
||||
public bool IsDone()
|
||||
{
|
||||
foreach (var loader in _dependBundles)
|
||||
{
|
||||
if (loader.IsDone() == false)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 主线程等待异步操作完毕
|
||||
/// </summary>
|
||||
public void WaitForAsyncComplete()
|
||||
{
|
||||
foreach (var loader in _dependBundles)
|
||||
{
|
||||
if (loader.IsDone() == false)
|
||||
loader.WaitForAsyncComplete();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加引用计数
|
||||
/// </summary>
|
||||
public void Reference()
|
||||
{
|
||||
foreach (var loader in _dependBundles)
|
||||
{
|
||||
loader.Reference();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 减少引用计数
|
||||
/// </summary>
|
||||
public void Release()
|
||||
{
|
||||
foreach (var loader in _dependBundles)
|
||||
{
|
||||
loader.Release();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取资源包的调试信息列表
|
||||
/// </summary>
|
||||
internal void GetBundleDebugInfos(List<DebugSummy.BundleInfo> output)
|
||||
{
|
||||
foreach (var loader in _dependBundles)
|
||||
{
|
||||
var debugInfo = new DebugSummy.BundleInfo();
|
||||
debugInfo.BundleName = loader.BundleInfo.BundleName;
|
||||
debugInfo.Version = loader.BundleInfo.Version;
|
||||
debugInfo.RefCount = loader.RefCount;
|
||||
debugInfo.States = loader.States;
|
||||
output.Add(debugInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b7eeccc96e4c31c45bd105b0dbc3fd6e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,17 @@
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 文件加载器状态
|
||||
/// </summary>
|
||||
public enum ELoaderStates
|
||||
{
|
||||
None = 0,
|
||||
Download,
|
||||
CheckDownload,
|
||||
LoadFile,
|
||||
CheckFile,
|
||||
Success,
|
||||
Fail,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 90f00fe388279034aaee9c89c07e2ef1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c64025c73372d9d4cab02c9cf9436e55
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,138 @@
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal abstract class AssetProviderBase : IAssetProvider
|
||||
{
|
||||
protected bool IsWaitForAsyncComplete { private set; get; } = false;
|
||||
|
||||
public string AssetPath { private set; get; }
|
||||
public string AssetName { private set; get; }
|
||||
public System.Type AssetType { private set; get; }
|
||||
public UnityEngine.Object AssetObject { protected set; get; }
|
||||
public UnityEngine.Object[] AllAssets { protected set; get; }
|
||||
public IAssetInstance AssetInstance { protected set; get; }
|
||||
public EAssetStates States { protected set; get; }
|
||||
public int RefCount { private set; get; }
|
||||
public AssetOperationHandle Handle { private set; get; }
|
||||
public System.Action<AssetOperationHandle> Callback { set; get; }
|
||||
public bool IsDestroyed { private set; get; } = false;
|
||||
public bool IsDone
|
||||
{
|
||||
get
|
||||
{
|
||||
return States == EAssetStates.Success || States == EAssetStates.Fail;
|
||||
}
|
||||
}
|
||||
public bool IsValid
|
||||
{
|
||||
get
|
||||
{
|
||||
return IsDestroyed == false;
|
||||
}
|
||||
}
|
||||
public virtual float Progress
|
||||
{
|
||||
get
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public AssetProviderBase(string assetPath, System.Type assetType)
|
||||
{
|
||||
AssetPath = assetPath;
|
||||
AssetName = System.IO.Path.GetFileName(assetPath);
|
||||
AssetType = assetType;
|
||||
States = EAssetStates.None;
|
||||
Handle = new AssetOperationHandle(this);
|
||||
}
|
||||
|
||||
public abstract void Update();
|
||||
public virtual void Destory()
|
||||
{
|
||||
IsDestroyed = true;
|
||||
}
|
||||
|
||||
public void Reference()
|
||||
{
|
||||
RefCount++;
|
||||
}
|
||||
public void Release()
|
||||
{
|
||||
if (RefCount <= 0)
|
||||
Logger.Warning("Asset provider reference count is already zero. There may be resource leaks !");
|
||||
|
||||
RefCount--;
|
||||
}
|
||||
public bool CanDestroy()
|
||||
{
|
||||
if (IsDone == false)
|
||||
return false;
|
||||
|
||||
return RefCount <= 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否为场景提供者
|
||||
/// </summary>
|
||||
public bool IsSceneProvider()
|
||||
{
|
||||
if (this is BundledSceneProvider || this is DatabaseSceneProvider)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 等待异步执行完毕
|
||||
/// </summary>
|
||||
public virtual void WaitForAsyncComplete()
|
||||
{
|
||||
IsWaitForAsyncComplete = true;
|
||||
|
||||
// 注意:主动轮询更新完成同步加载
|
||||
Update();
|
||||
|
||||
// 验证结果
|
||||
if (IsDone == false)
|
||||
{
|
||||
Logger.Warning($"WaitForAsyncComplete failed to loading : {AssetPath}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步操作任务
|
||||
/// </summary>
|
||||
System.Threading.Tasks.Task<object> IAssetProvider.Task
|
||||
{
|
||||
get
|
||||
{
|
||||
var handle = WaitHandle;
|
||||
return System.Threading.Tasks.Task.Factory.StartNew(o =>
|
||||
{
|
||||
handle.WaitOne();
|
||||
return AssetObject as object;
|
||||
}, this);
|
||||
}
|
||||
}
|
||||
|
||||
// 异步操作相关
|
||||
private System.Threading.EventWaitHandle _waitHandle;
|
||||
private System.Threading.WaitHandle WaitHandle
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_waitHandle == null)
|
||||
_waitHandle = new System.Threading.EventWaitHandle(false, System.Threading.EventResetMode.ManualReset);
|
||||
_waitHandle.Reset();
|
||||
return _waitHandle;
|
||||
}
|
||||
}
|
||||
protected void InvokeCompletion()
|
||||
{
|
||||
Callback?.Invoke(Handle);
|
||||
_waitHandle?.Set();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 02cc2ce3da5a88b48942ad96e6061aad
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,105 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal sealed class BundledAssetProvider : BundledProvider
|
||||
{
|
||||
private AssetBundleRequest _cacheRequest;
|
||||
public override float Progress
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_cacheRequest == null)
|
||||
return 0;
|
||||
return _cacheRequest.progress;
|
||||
}
|
||||
}
|
||||
|
||||
public BundledAssetProvider(string assetPath, System.Type assetType)
|
||||
: base(assetPath, assetType)
|
||||
{
|
||||
}
|
||||
public override void Update()
|
||||
{
|
||||
if (IsDone)
|
||||
return;
|
||||
|
||||
if (States == EAssetStates.None)
|
||||
{
|
||||
States = EAssetStates.CheckBundle;
|
||||
}
|
||||
|
||||
// 1. 检测资源包
|
||||
if (States == EAssetStates.CheckBundle)
|
||||
{
|
||||
if (IsWaitForAsyncComplete)
|
||||
{
|
||||
DependBundles.WaitForAsyncComplete();
|
||||
OwnerBundle.WaitForAsyncComplete();
|
||||
}
|
||||
|
||||
if (DependBundles.IsDone() == false)
|
||||
return;
|
||||
if (OwnerBundle.IsDone() == false)
|
||||
return;
|
||||
|
||||
if (OwnerBundle.CacheBundle == null)
|
||||
{
|
||||
States = EAssetStates.Fail;
|
||||
InvokeCompletion();
|
||||
}
|
||||
else
|
||||
{
|
||||
States = EAssetStates.Loading;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 加载资源对象
|
||||
if (States == EAssetStates.Loading)
|
||||
{
|
||||
if (IsWaitForAsyncComplete)
|
||||
{
|
||||
if (AssetType == null)
|
||||
AssetObject = OwnerBundle.CacheBundle.LoadAsset(AssetName);
|
||||
else
|
||||
AssetObject = OwnerBundle.CacheBundle.LoadAsset(AssetName, AssetType);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (AssetType == null)
|
||||
_cacheRequest = OwnerBundle.CacheBundle.LoadAssetAsync(AssetName);
|
||||
else
|
||||
_cacheRequest = OwnerBundle.CacheBundle.LoadAssetAsync(AssetName, AssetType);
|
||||
}
|
||||
States = EAssetStates.Checking;
|
||||
}
|
||||
|
||||
// 3. 检测加载结果
|
||||
if (States == EAssetStates.Checking)
|
||||
{
|
||||
if (_cacheRequest != null)
|
||||
{
|
||||
if (IsWaitForAsyncComplete)
|
||||
{
|
||||
// 强制挂起主线程(注意:该操作会很耗时)
|
||||
Logger.Warning("Suspend the main thread to load unity asset.");
|
||||
AssetObject = _cacheRequest.asset;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_cacheRequest.isDone == false)
|
||||
return;
|
||||
AssetObject = _cacheRequest.asset;
|
||||
}
|
||||
}
|
||||
|
||||
States = AssetObject == null ? EAssetStates.Fail : EAssetStates.Success;
|
||||
if (States == EAssetStates.Fail)
|
||||
Logger.Warning($"Failed to load asset : {AssetName} from bundle : {OwnerBundle.BundleInfo.BundleName}");
|
||||
InvokeCompletion();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ca5e4bf0c3efe6742bb57b494487be52
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,51 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal abstract class BundledProvider : AssetProviderBase
|
||||
{
|
||||
protected BundleFileLoader OwnerBundle { private set; get; }
|
||||
protected DependBundleGrouper DependBundles { private set; get; }
|
||||
|
||||
public BundledProvider(string assetPath, System.Type assetType) : base(assetPath, assetType)
|
||||
{
|
||||
OwnerBundle = AssetSystem.CreateOwnerBundleLoader(assetPath);
|
||||
OwnerBundle.Reference();
|
||||
OwnerBundle.AddProvider(this);
|
||||
DependBundles = new DependBundleGrouper(assetPath);
|
||||
DependBundles.Reference();
|
||||
}
|
||||
public override void Destory()
|
||||
{
|
||||
base.Destory();
|
||||
|
||||
// 释放资源包
|
||||
if (OwnerBundle != null)
|
||||
{
|
||||
OwnerBundle.Release();
|
||||
OwnerBundle = null;
|
||||
}
|
||||
if (DependBundles != null)
|
||||
{
|
||||
DependBundles.Release();
|
||||
DependBundles = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取资源包的调试信息列表
|
||||
/// </summary>
|
||||
internal void GetBundleDebugInfos(List<DebugSummy.BundleInfo> output)
|
||||
{
|
||||
var ownerInfo = new DebugSummy.BundleInfo();
|
||||
ownerInfo.BundleName = OwnerBundle.BundleInfo.BundleName;
|
||||
ownerInfo.Version = OwnerBundle.BundleInfo.Version;
|
||||
ownerInfo.RefCount = OwnerBundle.RefCount;
|
||||
ownerInfo.States = OwnerBundle.States;
|
||||
output.Add(ownerInfo);
|
||||
|
||||
DependBundles.GetBundleDebugInfos(output);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4eacb00a7e3873740b599112dfcf0123
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,109 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal sealed class BundledSceneProvider : BundledProvider
|
||||
{
|
||||
private SceneInstanceParam _param;
|
||||
private AsyncOperation _asyncOp;
|
||||
public override float Progress
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_asyncOp == null)
|
||||
return 0;
|
||||
return _asyncOp.progress;
|
||||
}
|
||||
}
|
||||
|
||||
public BundledSceneProvider(string scenePath, SceneInstanceParam param)
|
||||
: base(scenePath, null)
|
||||
{
|
||||
_param = param;
|
||||
}
|
||||
public override void Update()
|
||||
{
|
||||
if (IsDone)
|
||||
return;
|
||||
|
||||
if (States == EAssetStates.None)
|
||||
{
|
||||
States = EAssetStates.CheckBundle;
|
||||
}
|
||||
|
||||
// 1. 检测资源包
|
||||
if (States == EAssetStates.CheckBundle)
|
||||
{
|
||||
if (DependBundles.IsDone() == false)
|
||||
return;
|
||||
if (OwnerBundle.IsDone() == false)
|
||||
return;
|
||||
|
||||
if (OwnerBundle.CacheBundle == null)
|
||||
{
|
||||
States = EAssetStates.Fail;
|
||||
InvokeCompletion();
|
||||
}
|
||||
else
|
||||
{
|
||||
States = EAssetStates.Loading;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 加载场景
|
||||
if (States == EAssetStates.Loading)
|
||||
{
|
||||
_asyncOp = SceneManager.LoadSceneAsync(AssetName, _param.LoadMode);
|
||||
if (_asyncOp != null)
|
||||
{
|
||||
_asyncOp.allowSceneActivation = true;
|
||||
States = EAssetStates.Checking;
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Warning($"Failed to load scene : {AssetName}");
|
||||
States = EAssetStates.Fail;
|
||||
InvokeCompletion();
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 检测加载结果
|
||||
if (States == EAssetStates.Checking)
|
||||
{
|
||||
if (_asyncOp.isDone)
|
||||
{
|
||||
SceneInstance instance = new SceneInstance(_asyncOp);
|
||||
instance.Scene = SceneManager.GetSceneByName(AssetName);
|
||||
AssetInstance = instance;
|
||||
if (_param.ActivateOnLoad)
|
||||
instance.Activate();
|
||||
|
||||
States = instance.Scene.IsValid() ? EAssetStates.Success : EAssetStates.Fail;
|
||||
InvokeCompletion();
|
||||
}
|
||||
}
|
||||
}
|
||||
public override void Destory()
|
||||
{
|
||||
base.Destory();
|
||||
|
||||
// 卸载附加场景(异步方式卸载)
|
||||
if (_param.LoadMode == LoadSceneMode.Additive)
|
||||
{
|
||||
var instance = AssetInstance as SceneInstance;
|
||||
if (instance != null && instance.Scene != null)
|
||||
{
|
||||
if (instance.Scene.IsValid() && instance.Scene.isLoaded)
|
||||
SceneManager.UnloadSceneAsync(instance.Scene);
|
||||
}
|
||||
}
|
||||
}
|
||||
public override void WaitForAsyncComplete()
|
||||
{
|
||||
throw new System.Exception($"Unity scene is not support {nameof(WaitForAsyncComplete)}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 38b1b77cff590ca4e808c5068c9bf88b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,105 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal sealed class BundledSubAssetsProvider : BundledProvider
|
||||
{
|
||||
private AssetBundleRequest _cacheRequest;
|
||||
public override float Progress
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_cacheRequest == null)
|
||||
return 0;
|
||||
return _cacheRequest.progress;
|
||||
}
|
||||
}
|
||||
|
||||
public BundledSubAssetsProvider(string assetPath, System.Type assetType)
|
||||
: base(assetPath, assetType)
|
||||
{
|
||||
}
|
||||
public override void Update()
|
||||
{
|
||||
if (IsDone)
|
||||
return;
|
||||
|
||||
if (States == EAssetStates.None)
|
||||
{
|
||||
States = EAssetStates.CheckBundle;
|
||||
}
|
||||
|
||||
// 1. 检测资源包
|
||||
if (States == EAssetStates.CheckBundle)
|
||||
{
|
||||
if (IsWaitForAsyncComplete)
|
||||
{
|
||||
DependBundles.WaitForAsyncComplete();
|
||||
OwnerBundle.WaitForAsyncComplete();
|
||||
}
|
||||
|
||||
if (DependBundles.IsDone() == false)
|
||||
return;
|
||||
if (OwnerBundle.IsDone() == false)
|
||||
return;
|
||||
|
||||
if (OwnerBundle.CacheBundle == null)
|
||||
{
|
||||
States = EAssetStates.Fail;
|
||||
InvokeCompletion();
|
||||
}
|
||||
else
|
||||
{
|
||||
States = EAssetStates.Loading;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 加载资源对象
|
||||
if (States == EAssetStates.Loading)
|
||||
{
|
||||
if (IsWaitForAsyncComplete)
|
||||
{
|
||||
if (AssetType == null)
|
||||
AllAssets = OwnerBundle.CacheBundle.LoadAssetWithSubAssets(AssetName);
|
||||
else
|
||||
AllAssets = OwnerBundle.CacheBundle.LoadAssetWithSubAssets(AssetName, AssetType);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (AssetType == null)
|
||||
_cacheRequest = OwnerBundle.CacheBundle.LoadAssetWithSubAssetsAsync(AssetName);
|
||||
else
|
||||
_cacheRequest = OwnerBundle.CacheBundle.LoadAssetWithSubAssetsAsync(AssetName, AssetType);
|
||||
}
|
||||
States = EAssetStates.Checking;
|
||||
}
|
||||
|
||||
// 3. 检测加载结果
|
||||
if (States == EAssetStates.Checking)
|
||||
{
|
||||
if (_cacheRequest != null)
|
||||
{
|
||||
if (IsWaitForAsyncComplete)
|
||||
{
|
||||
// 强制挂起主线程(注意:该操作会很耗时)
|
||||
Logger.Warning("Suspend the main thread to load unity asset.");
|
||||
AllAssets = _cacheRequest.allAssets;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_cacheRequest.isDone == false)
|
||||
return;
|
||||
AllAssets = _cacheRequest.allAssets;
|
||||
}
|
||||
}
|
||||
|
||||
States = AllAssets == null ? EAssetStates.Fail : EAssetStates.Success;
|
||||
if (States == EAssetStates.Fail)
|
||||
Logger.Warning($"Failed to load sub assets : {AssetName} from bundle : {OwnerBundle.BundleInfo.BundleName}");
|
||||
InvokeCompletion();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b3763bf52bde23b41a756f0d015cb30d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,68 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal sealed class DatabaseAssetProvider : AssetProviderBase
|
||||
{
|
||||
public override float Progress
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsDone)
|
||||
return 100f;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public DatabaseAssetProvider(string assetPath, System.Type assetType)
|
||||
: base(assetPath, assetType)
|
||||
{
|
||||
}
|
||||
public override void Update()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (IsDone)
|
||||
return;
|
||||
|
||||
if (States == EAssetStates.None)
|
||||
{
|
||||
// 检测资源文件是否存在
|
||||
string guid = UnityEditor.AssetDatabase.AssetPathToGUID(AssetPath);
|
||||
if (string.IsNullOrEmpty(guid))
|
||||
{
|
||||
States = EAssetStates.Fail;
|
||||
InvokeCompletion();
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
States = EAssetStates.Loading;
|
||||
}
|
||||
|
||||
// 注意:模拟异步加载效果提前返回
|
||||
if (IsWaitForAsyncComplete == false)
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. 加载资源对象
|
||||
if (States == EAssetStates.Loading)
|
||||
{
|
||||
AssetObject = UnityEditor.AssetDatabase.LoadAssetAtPath(AssetPath, AssetType);
|
||||
States = EAssetStates.Checking;
|
||||
}
|
||||
|
||||
// 2. 检测加载结果
|
||||
if (States == EAssetStates.Checking)
|
||||
{
|
||||
States = AssetObject == null ? EAssetStates.Fail : EAssetStates.Success;
|
||||
if (States == EAssetStates.Fail)
|
||||
Logger.Warning($"Failed to load asset object : {AssetPath}");
|
||||
InvokeCompletion();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d4022dd2ea39af5458fb1d61ec997347
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,94 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal sealed class DatabaseSceneProvider : AssetProviderBase
|
||||
{
|
||||
private SceneInstanceParam _param;
|
||||
private AsyncOperation _asyncOp;
|
||||
public override float Progress
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_asyncOp == null)
|
||||
return 0;
|
||||
return _asyncOp.progress;
|
||||
}
|
||||
}
|
||||
|
||||
public DatabaseSceneProvider(string scenePath, SceneInstanceParam param)
|
||||
: base(scenePath, null)
|
||||
{
|
||||
_param = param;
|
||||
}
|
||||
public override void Update()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (IsDone)
|
||||
return;
|
||||
|
||||
if (States == EAssetStates.None)
|
||||
{
|
||||
States = EAssetStates.Loading;
|
||||
}
|
||||
|
||||
// 1. 加载资源对象
|
||||
if (States == EAssetStates.Loading)
|
||||
{
|
||||
LoadSceneParameters loadSceneParameters = new LoadSceneParameters();
|
||||
loadSceneParameters.loadSceneMode = _param.LoadMode;
|
||||
_asyncOp = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(AssetPath, loadSceneParameters);
|
||||
if (_asyncOp != null)
|
||||
{
|
||||
_asyncOp.allowSceneActivation = true;
|
||||
States = EAssetStates.Checking;
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Warning($"Failed to load scene : {AssetName}");
|
||||
States = EAssetStates.Fail;
|
||||
InvokeCompletion();
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 检测加载结果
|
||||
if (States == EAssetStates.Checking)
|
||||
{
|
||||
if (_asyncOp.isDone)
|
||||
{
|
||||
SceneInstance instance = new SceneInstance(_asyncOp);
|
||||
instance.Scene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
|
||||
AssetInstance = instance;
|
||||
if(_param.ActivateOnLoad)
|
||||
instance.Activate();
|
||||
|
||||
States = instance.Scene.IsValid() ? EAssetStates.Success : EAssetStates.Fail;
|
||||
InvokeCompletion();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
public override void Destory()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
base.Destory();
|
||||
|
||||
// 卸载附加场景(异步方式卸载)
|
||||
if (_param.LoadMode == LoadSceneMode.Additive)
|
||||
{
|
||||
var instance = AssetInstance as SceneInstance;
|
||||
if(instance != null && instance.Scene != null)
|
||||
{
|
||||
if (instance.Scene.IsValid() && instance.Scene.isLoaded)
|
||||
SceneManager.UnloadSceneAsync(instance.Scene);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
public override void WaitForAsyncComplete()
|
||||
{
|
||||
throw new System.Exception($"Unity scene is not support {nameof(WaitForAsyncComplete)}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8252a639423064f498ed22f14912adae
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,75 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal sealed class DatabaseSubAssetsProvider : AssetProviderBase
|
||||
{
|
||||
public override float Progress
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsDone)
|
||||
return 100f;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public DatabaseSubAssetsProvider(string assetPath, System.Type assetType)
|
||||
: base(assetPath, assetType)
|
||||
{
|
||||
}
|
||||
public override void Update()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (IsDone)
|
||||
return;
|
||||
|
||||
if (States == EAssetStates.None)
|
||||
{
|
||||
// 检测资源文件是否存在
|
||||
string guid = UnityEditor.AssetDatabase.AssetPathToGUID(AssetPath);
|
||||
if (string.IsNullOrEmpty(guid))
|
||||
{
|
||||
States = EAssetStates.Fail;
|
||||
InvokeCompletion();
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
States = EAssetStates.Loading;
|
||||
}
|
||||
|
||||
// 注意:模拟异步加载效果提前返回
|
||||
if (IsWaitForAsyncComplete == false)
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. 加载资源对象
|
||||
if (States == EAssetStates.Loading)
|
||||
{
|
||||
var findAssets = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(AssetPath);
|
||||
List<UnityEngine.Object> result = new List<Object>(findAssets.Length);
|
||||
foreach (var findObj in findAssets)
|
||||
{
|
||||
if (findObj.GetType() == AssetType)
|
||||
result.Add(findObj);
|
||||
}
|
||||
AllAssets = result.ToArray();
|
||||
States = EAssetStates.Checking;
|
||||
}
|
||||
|
||||
// 2. 检测加载结果
|
||||
if (States == EAssetStates.Checking)
|
||||
{
|
||||
States = AllAssets == null ? EAssetStates.Fail : EAssetStates.Success;
|
||||
if (States == EAssetStates.Fail)
|
||||
Logger.Warning($"Failed to load all asset object : {AssetPath}");
|
||||
InvokeCompletion();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3837962b901d7ba4abf02a9991ac4c4a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,16 @@
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 资源加载状态
|
||||
/// </summary>
|
||||
public enum EAssetStates
|
||||
{
|
||||
None = 0,
|
||||
CheckBundle,
|
||||
Loading,
|
||||
Checking,
|
||||
Success,
|
||||
Fail,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aa6c0fe63326c604bb699be16747c1e2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,114 @@
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 资源提供者
|
||||
/// </summary>
|
||||
internal interface IAssetProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// 资源路径
|
||||
/// </summary>
|
||||
string AssetPath { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 资源对象的名称
|
||||
/// </summary>
|
||||
string AssetName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 资源对象的类型
|
||||
/// </summary>
|
||||
System.Type AssetType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取的资源对象
|
||||
/// </summary>
|
||||
UnityEngine.Object AssetObject { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取的资源对象集合
|
||||
/// </summary>
|
||||
UnityEngine.Object[] AllAssets { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 扩展的实例对象
|
||||
/// </summary>
|
||||
IAssetInstance AssetInstance { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前的加载状态
|
||||
/// </summary>
|
||||
EAssetStates States { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 引用计数
|
||||
/// </summary>
|
||||
int RefCount { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 资源操作句柄
|
||||
/// </summary>
|
||||
AssetOperationHandle Handle { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 用户请求的回调
|
||||
/// </summary>
|
||||
System.Action<AssetOperationHandle> Callback { set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否已经销毁
|
||||
/// </summary>
|
||||
bool IsDestroyed { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否完毕(成功或失败)
|
||||
/// </summary>
|
||||
bool IsDone { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否有效(AssetFileLoader销毁会导致Provider无效)
|
||||
/// </summary>
|
||||
bool IsValid { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 加载进度
|
||||
/// </summary>
|
||||
float Progress { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 轮询更新方法
|
||||
/// </summary>
|
||||
void Update();
|
||||
|
||||
/// <summary>
|
||||
/// 销毁资源对象
|
||||
/// </summary>
|
||||
void Destory();
|
||||
|
||||
/// <summary>
|
||||
/// 引用计数递加
|
||||
/// </summary>
|
||||
void Reference();
|
||||
|
||||
/// <summary>
|
||||
/// 引用计数递减
|
||||
/// </summary>
|
||||
void Release();
|
||||
|
||||
/// <summary>
|
||||
/// 是否可以销毁
|
||||
/// </summary>
|
||||
bool CanDestroy();
|
||||
|
||||
/// <summary>
|
||||
/// 等待异步执行完毕
|
||||
/// </summary>
|
||||
void WaitForAsyncComplete();
|
||||
|
||||
/// <summary>
|
||||
/// 异步操作任务
|
||||
/// </summary>
|
||||
System.Threading.Tasks.Task<object> Task { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 54f7b01043e68994189cc954e55813aa
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user