对象 | 所有对象必须是同一类型。 |
为targetObject
或targetObjects
创建自定义编辑器。
默认情况下,会创建一个具有匹配 CustomEditor 属性的适当编辑器。如果指定了 editorType,则会改为创建该类型的编辑器。如果您创建了多个自定义编辑器,并且每个编辑器显示对象的属性不同,则可以使用此方法。如果objects
类型不同或未找到合适的编辑器,则返回 NULL。使用此函数创建的编辑器必须使用Object.Destroy或Object.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; }