【Unity】由Unity资源的相对路径获取资源的AssetDatabase路径

由Unity资源的相对路径获取资源的AssetDatabase路径,仅用于编辑器。代码如下:

/// <summary>
/// 由Unity资源的相对路径获取资源的AssetDatabase路径。
/// 仅用于编辑器。
/// </summary>
/// <param name="assetRelativePath">Unity资源文件的相对路径。</param>
/// <param name="callerFilePath">请勿传入此参数。</param>
/// <returns></returns>
public static string GetAssetDatabasePathFromRelativePath(string assetRelativePath, [System.Runtime.CompilerServices.CallerFilePath] string callerFilePath = null)
{
#if !UNITY_EDITOR
    throw new System.NotSupportedException("Not supported for non Editor mode.");
#endif
 
    var callerDirectoryPath = System.IO.Path.GetDirectoryName(callerFilePath);
    var unityAssetRelativePath = System.IO.Path.Combine(callerDirectoryPath, assetRelativePath);
    var unityAssetAbsolutePath = System.IO.Path.GetFullPath(unityAssetRelativePath);
    var unityAssetEditorPath = $"Assets/{unityAssetAbsolutePath.Replace("\\", "/").Replace(Application.dataPath, null)}";
    return unityAssetEditorPath;
}

相关文章

实现Unity AssetBundle资源加载管理器 AssetBundle是实现资源...
Unity3D 使用LineRenderer绘制尾迹与虚线 1.添加LineRendere...
Unity 添加新建Lua脚本选项 最近学习Unity的XLua热更新框架的...
挂载脚本时文件名和类名的关联方式 写过Unity脚本的人应该都...
Unity单例基类的实现方式 游戏开发的过程中我们经常会将各种...
这篇文章主要介绍了Unity游戏开发中外观模式是什么意思,具有...