【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;
}
上一篇:UGUI UniWebView适配


下一篇:【Unity3D】UGUI实现层级菜单