Version: 2017.2

Editor.CreateEditor

切换到手册
public static Editor CreateEditor (Object targetObject, Type editorType= null);
public static Editor CreateEditor (Object[] targetObjects, Type editorType= null);

参数

objects 所有对象的类型必须完全相同。

描述

targetObjecttargetObjects 创建自定义编辑器。

默认情况下,所选的相应编辑器具有匹配的 CustomEditor 属性。 如果已指定 editorType,则会创建该类型的编辑器。 如果您已创建多个自定义编辑器,其中每个编辑器显示该对象的不同属性,请使用此方法。 如果 objects 具有不同的类型或者未找到相应的编辑器,将返回 NULL。

设想一个 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 &amp;&amp; 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; }