mirror of
https://github.com/tuyoogame/YooAsset.git
synced 2026-05-25 02:00:16 +00:00
Support addressable location
支持可寻址资源定位
This commit is contained in:
@@ -1,11 +0,0 @@
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
public class AddressLocationServices : ILocationServices
|
||||
{
|
||||
public string ConvertLocationToAssetPath(YooAssets.EPlayMode playMode, string location)
|
||||
{
|
||||
throw new System.NotImplementedException("该功能暂未支持!");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
|
||||
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 UNITY_EDITOR
|
||||
CheckLocation(location);
|
||||
#endif
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
private void CheckLocation(string location)
|
||||
{
|
||||
if (string.IsNullOrEmpty(location))
|
||||
{
|
||||
YooLogger.Error("location param is null or empty!");
|
||||
}
|
||||
else
|
||||
{
|
||||
// 检查路径末尾是否有空格
|
||||
int index = location.LastIndexOf(" ");
|
||||
if (index != -1)
|
||||
{
|
||||
if (location.Length == index + 1)
|
||||
YooLogger.Warning($"Found blank character in location : \"{location}\"");
|
||||
}
|
||||
|
||||
if (location.IndexOfAny(Path.GetInvalidPathChars()) >= 0)
|
||||
YooLogger.Warning($"Found illegal character in location : \"{location}\"");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,11 @@ namespace YooAsset
|
||||
/// </summary>
|
||||
BundleInfo GetBundleInfo(string bundleName);
|
||||
|
||||
/// <summary>
|
||||
/// 可寻址地址转换为资源路径
|
||||
/// </summary>
|
||||
string ConvertAddress(string address);
|
||||
|
||||
/// <summary>
|
||||
/// 获取资源所属的资源包名称
|
||||
/// </summary>
|
||||
|
||||
8
Assets/YooAsset/Runtime/Services/LocationServices.meta
Normal file
8
Assets/YooAsset/Runtime/Services/LocationServices.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6e400ee1e8b3556479bfa493ff7fe778
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,29 @@
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
public class AddressLocationServices : ILocationServices
|
||||
{
|
||||
public AddressLocationServices()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
LocationServicesHelper.InitEditorPlayMode(true);
|
||||
#endif
|
||||
}
|
||||
|
||||
public string ConvertLocationToAssetPath(YooAssets.EPlayMode playMode, string location)
|
||||
{
|
||||
if (playMode == YooAssets.EPlayMode.EditorPlayMode)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
return LocationServicesHelper.ConvertLocationToAssetPath(location);
|
||||
#else
|
||||
throw new System.NotImplementedException();
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
return YooAssets.ConvertAddress(location);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
public class DefaultLocationServices : ILocationServices
|
||||
{
|
||||
private readonly string _resourceRoot;
|
||||
|
||||
public DefaultLocationServices(string resourceRoot)
|
||||
{
|
||||
if (string.IsNullOrEmpty(resourceRoot) == false)
|
||||
_resourceRoot = PathHelper.GetRegularPath(resourceRoot);
|
||||
|
||||
#if UNITY_EDITOR
|
||||
LocationServicesHelper.InitEditorPlayMode(false);
|
||||
#endif
|
||||
}
|
||||
|
||||
public string ConvertLocationToAssetPath(YooAssets.EPlayMode playMode, string location)
|
||||
{
|
||||
location = CombineAssetPath(_resourceRoot, location);
|
||||
if (playMode == YooAssets.EPlayMode.EditorPlayMode)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
return LocationServicesHelper.ConvertLocationToAssetPath(location);
|
||||
#else
|
||||
throw new System.NotImplementedException();
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
return location;
|
||||
}
|
||||
}
|
||||
private string CombineAssetPath(string root, string location)
|
||||
{
|
||||
if (string.IsNullOrEmpty(root))
|
||||
return location;
|
||||
else
|
||||
return $"{root}/{location}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#if UNITY_EDITOR
|
||||
using System.Reflection;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal static class LocationServicesHelper
|
||||
{
|
||||
private static System.Type AssetBundleGrouperSettingHelperClassType;
|
||||
|
||||
public static void InitEditorPlayMode(bool enableAddressable)
|
||||
{
|
||||
AssetBundleGrouperSettingHelperClassType = Assembly.Load("YooAsset.Editor").GetType("YooAsset.Editor.AssetBundleGrouperSettingHelper");
|
||||
InvokePublicStaticMethod(AssetBundleGrouperSettingHelperClassType, "InitEditorPlayMode", enableAddressable);
|
||||
}
|
||||
public static string ConvertLocationToAssetPath(string location)
|
||||
{
|
||||
return (string)InvokePublicStaticMethod(AssetBundleGrouperSettingHelperClassType, "ConvertLocationToAssetPath", location);
|
||||
}
|
||||
|
||||
private static object InvokePublicStaticMethod(System.Type type, string method, params object[] parameters)
|
||||
{
|
||||
var methodInfo = type.GetMethod(method, BindingFlags.Public | BindingFlags.Static);
|
||||
if (methodInfo == null)
|
||||
{
|
||||
UnityEngine.Debug.LogError($"{type.FullName} not found method : {method}");
|
||||
return null;
|
||||
}
|
||||
return methodInfo.Invoke(null, parameters);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b39dcdab07554ca4ab4ace5ca97aee78
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user