Add get asset info method.

增加获取资源信息的方法。
pull/9/head
hevinci 2022-05-03 12:23:08 +08:00
parent 665a16fe60
commit 9ec841ff67
9 changed files with 258 additions and 59 deletions

View File

@ -0,0 +1,27 @@

namespace YooAsset
{
public class AssetInfo
{
/// <summary>
/// 资源路径
/// </summary>
public string AssetPath { private set; get; }
/// <summary>
/// 资源类型
/// </summary>
public System.Type AssetType { private set; get; }
public AssetInfo(string assetPath, System.Type assetType)
{
AssetPath = assetPath;
AssetType = assetType;
}
public AssetInfo(string assetPath)
{
AssetPath = assetPath;
AssetType = null;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 07db4f624e6cf80468fadd9e94691017
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -82,7 +82,7 @@ namespace YooAsset
} }
else else
{ {
throw new Exception($"Invalid depend id : {dependID} Asset path : {assetPath}"); throw new Exception($"Invalid bundle id : {dependID} Asset path : {assetPath}");
} }
} }
return result.ToArray(); return result.ToArray();
@ -109,7 +109,7 @@ namespace YooAsset
} }
else else
{ {
throw new Exception($"Invalid depend id : {bundleID} Asset path : {assetPath}"); throw new Exception($"Invalid bundle id : {bundleID} Asset path : {assetPath}");
} }
} }
else else
@ -135,7 +135,7 @@ namespace YooAsset
} }
else else
{ {
throw new Exception($"Invalid depend id : {bundleID} Asset path : {patchAsset.AssetPath}"); throw new Exception($"Invalid bundle id : {bundleID} Asset path : {patchAsset.AssetPath}");
} }
} }
return string.Empty; return string.Empty;

View File

@ -47,6 +47,10 @@ namespace YooAsset
return bundleInfo; return bundleInfo;
} }
} }
AssetInfo[] IBundleServices.GetAssetInfos(string[] tags)
{
return PatchHelper.GetAssetsInfoByTag(AppPatchManifest, tags);
}
string IBundleServices.MappingToAssetPath(string location) string IBundleServices.MappingToAssetPath(string location)
{ {
return AppPatchManifest.MappingToAssetPath(location); return AppPatchManifest.MappingToAssetPath(location);

View File

@ -309,6 +309,10 @@ namespace YooAsset
return bundleInfo; return bundleInfo;
} }
} }
AssetInfo[] IBundleServices.GetAssetInfos(string[] tags)
{
return PatchHelper.GetAssetsInfoByTag(LocalPatchManifest, tags);
}
string IBundleServices.MappingToAssetPath(string location) string IBundleServices.MappingToAssetPath(string location)
{ {
return LocalPatchManifest.MappingToAssetPath(location); return LocalPatchManifest.MappingToAssetPath(location);

View File

@ -56,6 +56,10 @@ namespace YooAsset
return bundleInfo; return bundleInfo;
} }
} }
AssetInfo[] IBundleServices.GetAssetInfos(string[] tags)
{
return PatchHelper.GetAssetsInfoByTag(AppPatchManifest, tags);
}
string IBundleServices.MappingToAssetPath(string location) string IBundleServices.MappingToAssetPath(string location)
{ {
return AppPatchManifest.MappingToAssetPath(location); return AppPatchManifest.MappingToAssetPath(location);

View File

@ -4,10 +4,15 @@ namespace YooAsset
internal interface IBundleServices internal interface IBundleServices
{ {
/// <summary> /// <summary>
/// 获取AssetBundle的信息 /// 获取资源包信息
/// </summary> /// </summary>
BundleInfo GetBundleInfo(string bundleName); BundleInfo GetBundleInfo(string bundleName);
/// <summary>
/// 获取资源信息列表
/// </summary>
AssetInfo[] GetAssetInfos(string[] tags);
/// <summary> /// <summary>
/// 映射为资源路径 /// 映射为资源路径
/// </summary> /// </summary>

View File

@ -169,5 +169,25 @@ namespace YooAsset
BundleInfo bundleInfo = new BundleInfo(patchBundle, BundleInfo.ELoadMode.LoadFromRemote, streamingPath, streamingPath); BundleInfo bundleInfo = new BundleInfo(patchBundle, BundleInfo.ELoadMode.LoadFromRemote, streamingPath, streamingPath);
return bundleInfo; return bundleInfo;
} }
/// <summary>
/// 获取资源信息列表
/// </summary>
public static AssetInfo[] GetAssetsInfoByTag(PatchManifest patchManifest, string[] tags)
{
List<AssetInfo> result = new List<AssetInfo>(100);
foreach (var patchAsset in patchManifest.AssetList)
{
string bundleName = patchManifest.GetBundleName(patchAsset.AssetPath);
if(patchManifest.Bundles.TryGetValue(bundleName, out PatchBundle bundle))
{
if(bundle.HasTag(tags))
{
result.Add(new AssetInfo(patchAsset.AssetPath));
}
}
}
return result.ToArray();
}
} }
} }

View File

@ -61,21 +61,21 @@ namespace YooAsset
} }
/// <summary> /// <summary>
/// 编辑器下模拟运行(只支持在编辑器下运行) /// 编辑器下模拟运行模式参数
/// </summary> /// </summary>
public class EditorPlayModeParameters : CreateParameters public class EditorPlayModeParameters : CreateParameters
{ {
} }
/// <summary> /// <summary>
/// 离线模式(本地打包运行模式) /// 离线模式参数
/// </summary> /// </summary>
public class OfflinePlayModeParameters : CreateParameters public class OfflinePlayModeParameters : CreateParameters
{ {
} }
/// <summary> /// <summary>
/// 网络模式(网络打包运行模式) /// 网络模式参数
/// </summary> /// </summary>
public class HostPlayModeParameters : CreateParameters public class HostPlayModeParameters : CreateParameters
{ {
@ -305,16 +305,6 @@ namespace YooAsset
} }
} }
/// <summary>
/// 获取资源包信息
/// </summary>
public static BundleInfo GetBundleInfo(string location)
{
string assetPath = _locationServices.ConvertLocationToAssetPath(location);
string bundleName = _bundleServices.GetBundleName(assetPath);
return _bundleServices.GetBundleInfo(bundleName);
}
/// <summary> /// <summary>
/// 资源回收(卸载引用计数为零的资源) /// 资源回收(卸载引用计数为零的资源)
/// </summary> /// </summary>
@ -332,6 +322,38 @@ namespace YooAsset
AssetSystem.ForceUnloadAllAssets(); AssetSystem.ForceUnloadAllAssets();
} }
/// <summary>
/// 获取资源包信息
/// </summary>
/// <param name="location">资源的定位地址</param>
public static BundleInfo GetBundleInfo(string location)
{
string assetPath = _locationServices.ConvertLocationToAssetPath(location);
string bundleName = _bundleServices.GetBundleName(assetPath);
return _bundleServices.GetBundleInfo(bundleName);
}
/// <summary>
/// 获取资源包信息
/// </summary>
/// <param name="assetInfo">资源信息</param>
public static BundleInfo GetBundleInfo(AssetInfo assetInfo)
{
string bundleName = _bundleServices.GetBundleName(assetInfo.AssetPath);
return _bundleServices.GetBundleInfo(bundleName);
}
/// <summary>
/// 获取资源信息列表
/// </summary>
/// <param name="tag">资源标签</param>
/// <returns></returns>
public static AssetInfo[] GetAssetInfos(string tag)
{
string[] tags = new string[] { tag };
return _bundleServices.GetAssetInfos(tags);
}
/// <summary> /// <summary>
/// 获取调试信息 /// 获取调试信息
/// </summary> /// </summary>
@ -347,7 +369,7 @@ namespace YooAsset
/// <summary> /// <summary>
/// 异步加载场景 /// 异步加载场景
/// </summary> /// </summary>
/// <param name="location">场景对象相对路径</param> /// <param name="location">场景的定位地址</param>
/// <param name="sceneMode">场景加载模式</param> /// <param name="sceneMode">场景加载模式</param>
/// <param name="activateOnLoad">加载完毕时是否主动激活</param> /// <param name="activateOnLoad">加载完毕时是否主动激活</param>
/// <param name="priority">优先级</param> /// <param name="priority">优先级</param>
@ -357,6 +379,20 @@ namespace YooAsset
var handle = AssetSystem.LoadSceneAsync(scenePath, sceneMode, activateOnLoad, priority); var handle = AssetSystem.LoadSceneAsync(scenePath, sceneMode, activateOnLoad, priority);
return handle; return handle;
} }
/// <summary>
/// 异步加载场景
/// </summary>
/// <param name="assetInfo">场景的资源信息</param>
/// <param name="sceneMode">场景加载模式</param>
/// <param name="activateOnLoad">加载完毕时是否主动激活</param>
/// <param name="priority">优先级</param>
public static SceneOperationHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool activateOnLoad = true, int priority = 100)
{
string scenePath = assetInfo.AssetPath;
var handle = AssetSystem.LoadSceneAsync(scenePath, sceneMode, activateOnLoad, priority);
return handle;
}
#endregion #endregion
#region 资源加载 #region 资源加载
@ -368,36 +404,28 @@ namespace YooAsset
public static RawFileOperation GetRawFileAsync(string location, string copyPath = null) public static RawFileOperation GetRawFileAsync(string location, string copyPath = null)
{ {
string assetPath = _locationServices.ConvertLocationToAssetPath(location); string assetPath = _locationServices.ConvertLocationToAssetPath(location);
if (_playMode == EPlayMode.EditorPlayMode) return GetRawFileInternal(assetPath, copyPath);
{
string bundleName = _bundleServices.GetBundleName(assetPath);
BundleInfo bundleInfo = _bundleServices.GetBundleInfo(bundleName);
RawFileOperation operation = new EditorPlayModeRawFileOperation(bundleInfo, copyPath);
OperationSystem.ProcessOperaiton(operation);
return operation;
}
else if (_playMode == EPlayMode.OfflinePlayMode)
{
string bundleName = _bundleServices.GetBundleName(assetPath);
BundleInfo bundleInfo = _bundleServices.GetBundleInfo(bundleName);
RawFileOperation operation = new OfflinePlayModeRawFileOperation(bundleInfo, copyPath);
OperationSystem.ProcessOperaiton(operation);
return operation;
}
else if (_playMode == EPlayMode.HostPlayMode)
{
string bundleName = _bundleServices.GetBundleName(assetPath);
BundleInfo bundleInfo = _bundleServices.GetBundleInfo(bundleName);
RawFileOperation operation = new HostPlayModeRawFileOperation(bundleInfo, copyPath);
OperationSystem.ProcessOperaiton(operation);
return operation;
}
else
{
throw new NotImplementedException();
}
} }
/// <summary>
/// 异步获取原生文件
/// </summary>
/// <param name="assetInfo">资源信息</param>
/// <param name="copyPath">拷贝路径</param>
public static RawFileOperation GetRawFileAsync(AssetInfo assetInfo, string copyPath = null)
{
return GetRawFileInternal(assetInfo.AssetPath, copyPath);
}
/// <summary>
/// 同步加载资源对象
/// </summary>
/// <param name="assetInfo">资源信息</param>
public static AssetOperationHandle LoadAssetSync(AssetInfo assetInfo)
{
return LoadAssetInternal(assetInfo.AssetPath, assetInfo.AssetType, true);
}
/// <summary> /// <summary>
/// 同步加载资源对象 /// 同步加载资源对象
@ -406,7 +434,8 @@ namespace YooAsset
/// <param name="location">资源的定位地址</param> /// <param name="location">资源的定位地址</param>
public static AssetOperationHandle LoadAssetSync<TObject>(string location) where TObject : class public static AssetOperationHandle LoadAssetSync<TObject>(string location) where TObject : class
{ {
return LoadAssetInternal(location, typeof(TObject), true); string assetPath = _locationServices.ConvertLocationToAssetPath(location);
return LoadAssetInternal(assetPath, typeof(TObject), true);
} }
/// <summary> /// <summary>
@ -416,7 +445,18 @@ namespace YooAsset
/// <param name="type">资源类型</param> /// <param name="type">资源类型</param>
public static AssetOperationHandle LoadAssetSync(string location, System.Type type) public static AssetOperationHandle LoadAssetSync(string location, System.Type type)
{ {
return LoadAssetInternal(location, type, true); string assetPath = _locationServices.ConvertLocationToAssetPath(location);
return LoadAssetInternal(assetPath, type, true);
}
/// <summary>
/// 同步加载子资源对象
/// </summary>
/// <param name="assetInfo">资源信息</param>
public static SubAssetsOperationHandle LoadSubAssetsSync(AssetInfo assetInfo)
{
return LoadSubAssetsInternal(assetInfo.AssetPath, assetInfo.AssetType, true);
} }
/// <summary> /// <summary>
@ -426,7 +466,8 @@ namespace YooAsset
/// <param name="location">资源的定位地址</param> /// <param name="location">资源的定位地址</param>
public static SubAssetsOperationHandle LoadSubAssetsSync<TObject>(string location) public static SubAssetsOperationHandle LoadSubAssetsSync<TObject>(string location)
{ {
return LoadSubAssetsInternal(location, typeof(TObject), true); string assetPath = _locationServices.ConvertLocationToAssetPath(location);
return LoadSubAssetsInternal(assetPath, typeof(TObject), true);
} }
/// <summary> /// <summary>
@ -436,10 +477,20 @@ namespace YooAsset
/// <param name="type">子对象类型</param> /// <param name="type">子对象类型</param>
public static SubAssetsOperationHandle LoadSubAssetsSync(string location, System.Type type) public static SubAssetsOperationHandle LoadSubAssetsSync(string location, System.Type type)
{ {
return LoadSubAssetsInternal(location, type, true); string assetPath = _locationServices.ConvertLocationToAssetPath(location);
return LoadSubAssetsInternal(assetPath, type, true);
} }
/// <summary>
/// 异步加载资源对象
/// </summary>
/// <param name="assetInfo">资源信息</param>
public static AssetOperationHandle LoadAssetAsync(AssetInfo assetInfo)
{
return LoadAssetInternal(assetInfo.AssetPath, assetInfo.AssetType, false);
}
/// <summary> /// <summary>
/// 异步加载资源对象 /// 异步加载资源对象
/// </summary> /// </summary>
@ -447,7 +498,8 @@ namespace YooAsset
/// <param name="location">资源的定位地址</param> /// <param name="location">资源的定位地址</param>
public static AssetOperationHandle LoadAssetAsync<TObject>(string location) public static AssetOperationHandle LoadAssetAsync<TObject>(string location)
{ {
return LoadAssetInternal(location, typeof(TObject), false); string assetPath = _locationServices.ConvertLocationToAssetPath(location);
return LoadAssetInternal(assetPath, typeof(TObject), false);
} }
/// <summary> /// <summary>
@ -457,7 +509,18 @@ namespace YooAsset
/// <param name="type">资源类型</param> /// <param name="type">资源类型</param>
public static AssetOperationHandle LoadAssetAsync(string location, System.Type type) public static AssetOperationHandle LoadAssetAsync(string location, System.Type type)
{ {
return LoadAssetInternal(location, type, false); string assetPath = _locationServices.ConvertLocationToAssetPath(location);
return LoadAssetInternal(assetPath, type, false);
}
/// <summary>
/// 异步加载子资源对象
/// </summary>
/// <param name="assetInfo">资源信息</param>
public static SubAssetsOperationHandle LoadSubAssetsAsync(AssetInfo assetInfo)
{
return LoadSubAssetsInternal(assetInfo.AssetPath, assetInfo.AssetType, false);
} }
/// <summary> /// <summary>
@ -467,7 +530,8 @@ namespace YooAsset
/// <param name="location">资源的定位地址</param> /// <param name="location">资源的定位地址</param>
public static SubAssetsOperationHandle LoadSubAssetsAsync<TObject>(string location) public static SubAssetsOperationHandle LoadSubAssetsAsync<TObject>(string location)
{ {
return LoadSubAssetsInternal(location, typeof(TObject), false); string assetPath = _locationServices.ConvertLocationToAssetPath(location);
return LoadSubAssetsInternal(assetPath, typeof(TObject), false);
} }
/// <summary> /// <summary>
@ -477,21 +541,48 @@ namespace YooAsset
/// <param name="type">子对象类型</param> /// <param name="type">子对象类型</param>
public static SubAssetsOperationHandle LoadSubAssetsAsync(string location, System.Type type) public static SubAssetsOperationHandle LoadSubAssetsAsync(string location, System.Type type)
{ {
return LoadSubAssetsInternal(location, type, false); string assetPath = _locationServices.ConvertLocationToAssetPath(location);
return LoadSubAssetsInternal(assetPath, type, false);
} }
private static AssetOperationHandle LoadAssetInternal(string location, System.Type assetType, bool waitForAsyncComplete) private static RawFileOperation GetRawFileInternal(string assetPath, string copyPath)
{
string bundleName = _bundleServices.GetBundleName(assetPath);
BundleInfo bundleInfo = _bundleServices.GetBundleInfo(bundleName);
if (_playMode == EPlayMode.EditorPlayMode)
{
RawFileOperation operation = new EditorPlayModeRawFileOperation(bundleInfo, copyPath);
OperationSystem.ProcessOperaiton(operation);
return operation;
}
else if (_playMode == EPlayMode.OfflinePlayMode)
{
RawFileOperation operation = new OfflinePlayModeRawFileOperation(bundleInfo, copyPath);
OperationSystem.ProcessOperaiton(operation);
return operation;
}
else if (_playMode == EPlayMode.HostPlayMode)
{
RawFileOperation operation = new HostPlayModeRawFileOperation(bundleInfo, copyPath);
OperationSystem.ProcessOperaiton(operation);
return operation;
}
else
{
throw new NotImplementedException();
}
}
private static AssetOperationHandle LoadAssetInternal(string assetPath, System.Type assetType, bool waitForAsyncComplete)
{ {
string assetPath = _locationServices.ConvertLocationToAssetPath(location);
var handle = AssetSystem.LoadAssetAsync(assetPath, assetType); var handle = AssetSystem.LoadAssetAsync(assetPath, assetType);
if (waitForAsyncComplete) if (waitForAsyncComplete)
handle.WaitForAsyncComplete(); handle.WaitForAsyncComplete();
return handle; return handle;
} }
private static SubAssetsOperationHandle LoadSubAssetsInternal(string location, System.Type assetType, bool waitForAsyncComplete) private static SubAssetsOperationHandle LoadSubAssetsInternal(string assetPath, System.Type assetType, bool waitForAsyncComplete)
{ {
string assetPath = _locationServices.ConvertLocationToAssetPath(location);
var handle = AssetSystem.LoadSubAssetsAsync(assetPath, assetType); var handle = AssetSystem.LoadSubAssetsAsync(assetPath, assetType);
if (waitForAsyncComplete) if (waitForAsyncComplete)
handle.WaitForAsyncComplete(); handle.WaitForAsyncComplete();
@ -562,10 +653,11 @@ namespace YooAsset
} }
} }
/// <summary> /// <summary>
/// 创建补丁下载器,用于下载更新指定的资源列表依赖的资源包文件 /// 创建补丁下载器,用于下载更新指定的资源列表依赖的资源包文件
/// </summary> /// </summary>
/// <param name="locations">资源列表</param> /// <param name="locations">资源定位列表</param>
/// <param name="downloadingMaxNumber">同时下载的最大文件数</param> /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
/// <param name="failedTryAgain">下载失败的重试次数</param> /// <param name="failedTryAgain">下载失败的重试次数</param>
public static PatchDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain) public static PatchDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain)
@ -594,6 +686,38 @@ namespace YooAsset
throw new NotImplementedException(); throw new NotImplementedException();
} }
} }
/// <summary>
/// 创建补丁下载器,用于下载更新指定的资源列表依赖的资源包文件
/// </summary>
/// <param name="assetInfos">资源信息列表</param>
/// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
/// <param name="failedTryAgain">下载失败的重试次数</param>
public static PatchDownloaderOperation CreateBundleDownloader(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain)
{
if (_playMode == EPlayMode.EditorPlayMode || _playMode == EPlayMode.OfflinePlayMode)
{
List<BundleInfo> downloadList = new List<BundleInfo>();
var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain);
return operation;
}
else if (_playMode == EPlayMode.HostPlayMode)
{
if (_hostPlayModeImpl == null)
throw new Exception("YooAsset is not initialized.");
List<string> assetPaths = new List<string>(assetInfos.Length);
foreach (var assetInfo in assetInfos)
{
assetPaths.Add(assetInfo.AssetPath);
}
return _hostPlayModeImpl.CreatePatchDownloaderByPaths(assetPaths, downloadingMaxNumber, failedTryAgain);
}
else
{
throw new NotImplementedException();
}
}
#endregion #endregion
#region 资源解压 #region 资源解压