版本: Unity 6 (6000.0)
语言英语
  • C#

Editor.CreateEditor

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交,但我们确实会阅读用户提出的每个建议更改,并在适用情况下进行更新。

关闭

提交失败

由于某些原因,您的建议更改无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

声明

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

声明

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

参数

对象 所有对象必须是同一类型。

描述

targetObjecttargetObjects创建自定义编辑器。

默认情况下,会创建一个具有匹配 CustomEditor 属性的适当编辑器。如果指定了 editorType,则会改为创建该类型的编辑器。如果您创建了多个自定义编辑器,并且每个编辑器显示对象的属性不同,则可以使用此方法。如果objects类型不同或未找到合适的编辑器,则返回 NULL。使用此函数创建的编辑器必须使用Object.DestroyObject.DestroyImmediate显式销毁。

考虑一个用于编辑路点数组的 Transform 的脚本 WaypointPathEditor。

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 (); } } }

附加到路点 GameObjects 的脚本

using UnityEngine;
using System.Collections;

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