Unity 6.3
0 онлайн 89 гостей 3 в системе
Вход

Editor.CreateEditor

Declaration

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

Параметры

Параметр Описание
объекты Все объекты должны быть одного типа.

Описание

Создайте пользовательский редактор для targetObject или targetObjects.

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

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

using UnityEditor;
using UnityEngine;
using System.Collections;

[CustomEditor(typeof(WaypointPath))] public class WaypointPathEditor : Editor { Editor currentTransformEditor; 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++) { Transform wayPoint = myWayPath.wayPointArray[i];

if (wayPoint != null) optionsList[i] = wayPoint.name; else optionsList[i] = $"Empty waypoint {(i + 1)}"; } } }

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 GameObject:

using UnityEngine;
using System.Collections;

// Note: this is not an editor script. public class WaypointPath : MonoBehaviour { public Transform[] wayPointArray; }