Version: 2021.3

EditorUtility.InstanceIDToObject

切换到手册
public static Object InstanceIDToObject (int instanceID);

描述

将实例 ID 转换为对对象的引用。

如果对象不是从磁盘加载的,请从磁盘加载该对象。


用于输入实例 ID 并输出对象名称的编辑器窗口。

using UnityEngine;
using UnityEditor;

public class InstanceIDToObjectExample : EditorWindow { static int id;

[MenuItem("Example/ID To Name")] static void Init() { // Get existing open window or if none, make a new one: InstanceIDToObjectExample window = (InstanceIDToObjectExample)EditorWindow.GetWindow(typeof(InstanceIDToObjectExample)); window.Show(); }

void OnGUI() { id = EditorGUILayout.IntField("Instance ID:", id); if (GUILayout.Button("Find Name")) { Object obj = EditorUtility.InstanceIDToObject(id); if (!obj) Debug.LogError("No object could be found with instance id: " + id); else Debug.Log("Object's name: " + obj.name); } }

void OnInspectorUpdate() { Repaint(); } }