Resource location interface extension

资源定位接口扩展
pull/4/head
hevinci 2022-04-14 12:19:04 +08:00
parent eb6b6e3aba
commit 09a985fadb
11 changed files with 138 additions and 82 deletions

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: bf4cc5f446778bc428a69fdcd183e447
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,77 @@
using System.IO;
namespace YooAsset
{
public class DefaultLocationServices : ILocationServices
{
private readonly string _resourceRoot;
public DefaultLocationServices(string resourceRoot)
{
if (string.IsNullOrEmpty(resourceRoot) == false)
_resourceRoot = PathHelper.GetRegularPath(resourceRoot);
}
public string ConvertLocationToAssetPath(YooAssets.EPlayMode playMode, string location)
{
if (playMode == YooAssets.EPlayMode.EditorPlayMode)
{
string filePath = CombineAssetPath(_resourceRoot, location);
return FindDatabaseAssetPath(filePath);
}
else
{
return CombineAssetPath(_resourceRoot, location);
}
}
/// <summary>
/// 合并资源路径
/// </summary>
private static string CombineAssetPath(string root, string location)
{
if (string.IsNullOrEmpty(root))
return location;
else
return $"{root}/{location}";
}
/// <summary>
/// 获取AssetDatabase的加载路径
/// </summary>
private 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 = PathHelper.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 = PathHelper.GetDirectory(assetPath);
if (assetDirectory != directory)
continue;
string assetName = Path.GetFileNameWithoutExtension(assetPath);
if (assetName == fileName)
return assetPath;
}
// 没有找到同名的资源文件
YooLogger.Warning($"Not found asset : {filePath}");
return filePath;
#else
throw new System.NotImplementedException();
#endif
}
}
}

View File

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

View File

@ -0,0 +1,11 @@

namespace YooAsset
{
public interface ILocationServices
{
/// <summary>
/// 定位地址转换为资源路径
/// </summary>
string ConvertLocationToAssetPath(YooAssets.EPlayMode playMode, string location);
}
}

View File

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

View File

@ -71,56 +71,6 @@ namespace YooAsset
return StringUtility.Format("file:///{0}", path);
#elif UNITY_WEBGL
return path;
#endif
}
/// <summary>
/// 合并资源路径
/// </summary>
public static string CombineAssetPath(string root, string location)
{
if (string.IsNullOrEmpty(root))
return location;
else
return $"{root}/{location}";
}
/// <summary>
/// 获取AssetDatabase的加载路径
/// </summary>
public 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;
}
// 没有找到同名的资源文件
YooLogger.Warning($"Not found asset : {filePath}");
return filePath;
#else
throw new System.NotImplementedException();
#endif
}
}

View File

@ -10,7 +10,7 @@ namespace YooAsset
/// <summary>
/// 运行模式
/// </summary>
private enum EPlayMode
public enum EPlayMode
{
/// <summary>
/// 编辑器下模拟运行模式
@ -28,16 +28,18 @@ namespace YooAsset
HostPlayMode,
}
/// <summary>
/// 初始化参数
/// </summary>
public abstract class CreateParameters
{
/// <summary>
/// 资源定位的根路径
/// 例如Assets/MyResource
/// 资源定位服务接口
/// </summary>
public string LocationRoot;
public ILocationServices LocationServices = null;
/// <summary>
/// 文件解密接口
/// 文件解密服务接口
/// </summary>
public IDecryptionServices DecryptionServices = null;
@ -100,9 +102,9 @@ namespace YooAsset
private static bool _isInitialize = false;
private static string _locationRoot;
private static EPlayMode _playMode;
private static IBundleServices _bundleServices;
private static ILocationServices _locationServices;
private static EditorPlayModeImpl _editorPlayModeImpl;
private static OfflinePlayModeImpl _offlinePlayModeImpl;
private static HostPlayModeImpl _hostPlayModeImpl;
@ -119,6 +121,11 @@ namespace YooAsset
if (parameters == null)
throw new Exception($"YooAsset create parameters is null.");
if (parameters.LocationServices == null)
throw new Exception($"{nameof(IBundleServices)} is null.");
else
_locationServices = parameters.LocationServices;
#if !UNITY_EDITOR
if (parameters is EditorPlayModeParameters)
throw new Exception($"Editor play mode only support unity editor.");
@ -149,9 +156,6 @@ namespace YooAsset
YooLogger.Warning($"{nameof(parameters.OperationSystemMaxTimeSlice)} minimum value is 33 milliseconds");
}
if (string.IsNullOrEmpty(parameters.LocationRoot) == false)
_locationRoot = PathHelper.GetRegularPath(parameters.LocationRoot);
if (parameters.AutoReleaseInterval > 0)
_releaseCD = parameters.AutoReleaseInterval;
@ -241,7 +245,7 @@ namespace YooAsset
throw new NotImplementedException();
}
}
/// <summary>
/// 向网络端请求并更新补丁清单
/// </summary>
@ -307,7 +311,7 @@ namespace YooAsset
/// </summary>
public static BundleInfo GetBundleInfo(string location)
{
string assetPath = ConvertLocationToAssetPath(location);
string assetPath = _locationServices.ConvertLocationToAssetPath(_playMode, location);
string bundleName = _bundleServices.GetBundleName(assetPath);
return _bundleServices.GetBundleInfo(bundleName);
}
@ -350,7 +354,7 @@ namespace YooAsset
/// <param name="priority">优先级</param>
public static SceneOperationHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool activateOnLoad = true, int priority = 100)
{
string scenePath = ConvertLocationToAssetPath(location);
string scenePath = _locationServices.ConvertLocationToAssetPath(_playMode, location);
var handle = AssetSystem.LoadSceneAsync(scenePath, sceneMode, activateOnLoad, priority);
return handle;
}
@ -362,7 +366,7 @@ namespace YooAsset
/// </summary>
public static RawFileOperation LoadRawFileAsync(string location, string savePath)
{
string assetPath = ConvertLocationToAssetPath(location);
string assetPath = _locationServices.ConvertLocationToAssetPath(_playMode, location);
return AssetSystem.LoadRawFileAsync(assetPath, savePath);
}
@ -451,7 +455,7 @@ namespace YooAsset
private static AssetOperationHandle LoadAssetInternal(string location, System.Type assetType, bool waitForAsyncComplete)
{
string assetPath = ConvertLocationToAssetPath(location);
string assetPath = _locationServices.ConvertLocationToAssetPath(_playMode, location);
var handle = AssetSystem.LoadAssetAsync(assetPath, assetType);
if (waitForAsyncComplete)
handle.WaitForAsyncComplete();
@ -459,7 +463,7 @@ namespace YooAsset
}
private static SubAssetsOperationHandle LoadSubAssetsInternal(string location, System.Type assetType, bool waitForAsyncComplete)
{
string assetPath = ConvertLocationToAssetPath(location);
string assetPath = _locationServices.ConvertLocationToAssetPath(_playMode, location);
var handle = AssetSystem.LoadSubAssetsAsync(assetPath, assetType);
if (waitForAsyncComplete)
handle.WaitForAsyncComplete();
@ -539,7 +543,7 @@ namespace YooAsset
List<string> assetPaths = new List<string>(locations.Length);
foreach (var location in locations)
{
string assetPath = ConvertLocationToAssetPath(location);
string assetPath = _locationServices.ConvertLocationToAssetPath(_playMode, location);
assetPaths.Add(assetPath);
}
return _hostPlayModeImpl.CreateDownloaderByPaths(assetPaths, downloadingMaxNumber, failedTryAgain);
@ -642,22 +646,6 @@ namespace YooAsset
}
}
}
/// <summary>
/// 定位地址转换为资源路径
/// </summary>
private static string ConvertLocationToAssetPath(string location)
{
if (_playMode == EPlayMode.EditorPlayMode)
{
string filePath = PathHelper.CombineAssetPath(_locationRoot, location);
return PathHelper.FindDatabaseAssetPath(filePath);
}
else
{
return PathHelper.CombineAssetPath(_locationRoot, location);
}
}
#endregion
}
}