文档

Unity Engine

Unity Engine

GetAssetPath

Gets the path of an asset object, relative to the project folder.
阅读时间2 分钟最后更新于 19 天前

Definition

  • Type: Method
  • Namespace: UnityEditor
  • Assembly: UnityEditor.CoreModule

GetAssetPath(Object)

Gets the path of an asset object, relative to the project folder.
public static string GetAssetPath(Object assetObject)

Parameters

assetObject

The asset object to get the path of.

Returns

Type

Description

stringThe project-relative path of the asset, or an empty string if the asset doesn't exist.

Remarks

Use this method to find where an asset is stored. Paths are relative to the project folder, for example
Assets/MyTextures/hello.png
.

Examples

using UnityEngine;using UnityEditor;public class CreateMaterialExample : MonoBehaviour{ [MenuItem("GameObject/Create Material")] static void CreateMaterial() { // Create a simple material asset Material material = new Material(Shader.Find("Specular")); AssetDatabase.CreateAsset(material, "Assets/MyMaterial.mat"); // Print the path of the created asset Debug.Log(AssetDatabase.GetAssetPath(material)); }}

GetAssetPath(int)

Gets the path of the asset with the specified instance ID, relative to the project folder.
警告
Removed. Please use GetAssetPath(Object) with the asset reference (e.g. GetAssetPath(myObject) instead of GetAssetPath(myObject.GetInstanceID())), or GetAssetPath(EntityId) with the EntityId parameter type instead.
public static string GetAssetPath(int instanceID)

Parameters

instanceID

The instance ID of the asset.

Returns

Type

Description

stringThe project-relative path of the asset, or an empty string if the asset doesn't exist.

Remarks

This overload is obsolete. Use the overload that takes an EntityId instead.

GetAssetPath(EntityId)

Gets the path of the asset with the specified EntityId, relative to the project folder.
public static string GetAssetPath(EntityId entityId)

Parameters

entityId

EntityId
The EntityId of the asset.

Returns

Type

Description

stringThe project-relative path of the asset, or an empty string if the asset doesn't exist.

Remarks

Use this method to find where an asset is stored. Paths are relative to the project folder, for example
Assets/MyTextures/hello.png
.