要在 Unity 的 EditorWindow 上加载和显示 Prefab,你可以使用以下步骤:

  1. 创建一个继承自 EditorWindow 的脚本,例如 'PrefabWindow.cs'。
using UnityEditor;
using UnityEngine;

public class PrefabWindow : EditorWindow
{
    private GameObject prefab;

    [MenuItem("Window/Prefab Window")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow<PrefabWindow>("Prefab Window");
    }

    private void OnGUI()
    {
        GUILayout.Label("Prefab", EditorStyles.boldLabel);
        prefab = EditorGUILayout.ObjectField(prefab, typeof(GameObject), false) as GameObject;

        if (GUILayout.Button("Instantiate"))
        {
            InstantiatePrefab();
        }
    }

    private void InstantiatePrefab()
    {
        if (prefab != null)
        {
            PrefabUtility.InstantiatePrefab(prefab);
        }
    }
}
  1. 将脚本附加到一个游戏对象上,并确保脚本的文件名与类名匹配。

  2. 在 Unity 的菜单栏中点击 'Window',然后选择 'Prefab Window' 来打开自定义的 EditorWindow。

  3. 在 EditorWindow 的界面上,使用 EditorGUILayout.ObjectField 来创建一个用于选择 Prefab 的字段。

  4. 在 EditorWindow 的界面上,使用 GUILayout.Button 来创建一个按钮,点击按钮会调用 InstantiatePrefab 方法。

  5. 在 InstantiatePrefab 方法中,使用 PrefabUtility.InstantiatePrefab 来实例化选定的 Prefab。

这样,当你在 EditorWindow 上选择一个 Prefab 并点击 'Instantiate' 按钮时,该 Prefab 将在场景中实例化出来。

Unity EditorWindow 加载并显示 Prefab