Editor.CreateEditor

Unity · Версия 2020.3 LTS · Scripting · Editor

Editor.CreateEditor

Объявление

public static Editor CreateEditor(Object targetObject, Type editorType = null);

public static Editor CreateEditor(Object[] targetObjects, Type editorType = null);

Параметры

objects Все объекты должны быть одного типа.

Описание

Создаёт собственный редактор для targetObject или targetObjects.

По умолчанию создается соответствующий редактор с соответствующим атрибутом CustomEditor. Если указан editorType, вместо этого создается редактор этого типа. Используйте это, если вы создали несколько пользовательских редакторов, и каждый редактор показывает разные свойства объекта. Возвращает NULL, если объекты относятся к разным типам или не найден подходящий редактор. Редакторы, созданные с помощью этой функции, должны быть уничтожены явным образом с помощью Object.Destroy или Object.DestroyImmediate.

Рассмотрите сценарий WaypointPathEditor для редактирования преобразований массива wayPoint.

using UnityEditor;
using UnityEngine;
using System.Collections;


[CustomEditor(typeof(WaypointPath))]
public class WaypointPathEditor : Editor
{

Editor currentTransformEditor;
    Transform[] waypoints;
    Transform selectedTransform;
    string[] optionsList;
    int index = 0;
    WaypointPath myWayPath;

    void GetWaypoints()
    {
        myWayPath = target as WaypointPath;

        if (myWayPath.wayPointArray != null)
        {
            optionsList = new string[myWayPath.wayPointArray.Length];

            for (int i = 0; i < optionsList.Length; i++)
            {
                optionsList[i] = myWayPath.wayPointArray[i].name;
            }
        }
    }

    public override void OnInspectorGUI()
    {
        GetWaypoints ();
        DrawDefaultInspector ();
        EditorGUILayout.Space ();
        EditorGUI.BeginChangeCheck ();

        if (optionsList != null)
            index = EditorGUILayout.Popup ("Select Waypoint", index, optionsList);

        if (EditorGUI.EndChangeCheck())
        {
            Editor tmpEditor = null;

            if (index < myWayPath.wayPointArray.Length)
            {
                selectedTransform = myWayPath.wayPointArray[index];

                //Creates an Editor for selected Component from a Popup
                tmpEditor = Editor.CreateEditor(selectedTransform);
            } else {
                selectedTransform = null;
            }

            // If there isn't a Transform currently selected then destroy the existing editor
            if (currentTransformEditor != null)
            {
                DestroyImmediate (currentTransformEditor);
            }

            currentTransformEditor = tmpEditor;
        }

        // Shows the created Editor beneath CustomEditor
        if (currentTransformEditor != null && selectedTransform != null)
        {
            currentTransformEditor.OnInspectorGUI ();
        }
    }
}

Скрипт, прикрепленный к игровому объекту waypath:

using UnityEngine;
using System.Collections;

// Примечание: это не скрипт редактора.
public class WaypointPath : MonoBehaviour
{
    public Transform[] wayPointArray;
}

Обновлено 17 ноября 2026 г.