其他分享
首页 > 其他分享> > 【Unity】由Unity资源的相对路径获取资源的AssetDatabase路径

【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;
}

标签:AssetDatabase,System,Unity,相对路径,var,Path,资源
来源: https://blog.csdn.net/qq_21397217/article/details/117064416