Unity 获取资源项目文件的路径

1.获取相对于存储资源的项目文件的路径名称

使用AssetDatabase类中的静态方法:

public static string GetAssetPath (int instanceID );
public static string GetAssetPath ( Object assetObject );

举例:

Material mat = new Material(Shader.Find("Specular"));
AssetDatabase.CreateAsset(mat, "Assets/MyMaterial.mat");
// 获取mat路径
string assetPath = AssetDatabase.GetAssetPath(mat);
// 输出:Assets/MyMaterial.mat
Debug.Log(assetPath);

2.获取与.meta文件关联的资源文件路径

方法:

public static string GetAssetPathFromTextMetaFilePath (string path );

举例:

Material mat = new Material(Shader.Find("Specular"));
AssetDatabase.CreateAsset(mat, "Assets/MyMaterial.mat");
// 获取mat路径
string assetPath = AssetDatabase.GetAssetPathFromTextMetaFilePath ("Assets/MyMaterial.mat.meta");
// 输出:Assets/MyMaterial.mat
Debug.Log(assetPath);

你可能感兴趣的:(Unity,unity,游戏引擎,c#)