Select your preferred scripting language. All code snippets will be displayed in this language.
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
Closeobjects | All objects must be of same exact type. |
Make a custom editor for targetObject
or targetObjects
.
By default an appropriate editor is chosen that has a matching CustomEditor attribute.
If an editorType is specified, an editor of that type is created instead.
Use this if you have created multiple custom editors where each editor shows different properties of the object.
Returns NULL if objects
are of different types or if no approprate editor was found.
Consider a script WaypointPathEditor for editing the Transforms of a wayPoint array.
#pragma strict @CustomEditor(WaypointPath) public class WaypointPathEditor extends Editor { var currentTransformEditor: Editor; var waypoints: Transform[]; var selectedTransform: Transform; var optionsList: String[]; var index: int = 0; var myWayPath: WaypointPath; function GetWaypoints() { myWayPath = target as WaypointPath; if (myWayPath.wayPointArray != null) { optionsList = new String[myWayPath.wayPointArray.Length]; for (var i: int = 0; i < optionsList.Length; i++) { optionsList[i] = myWayPath.wayPointArray[i].name; } } } public override function OnInspectorGUI() { GetWaypoints(); DrawDefaultInspector(); EditorGUILayout.Space(); EditorGUI.BeginChangeCheck(); if (optionsList != null) index = EditorGUILayout.Popup("Select Waypoint", index, optionsList); if (EditorGUI.EndChangeCheck()) { var tmpEditor: Editor = 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(); } } }
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 (); } } }
And the script attached to a waypath GameObject:
#pragma strict // Note: this is not an editor script. public class WaypointPath extends MonoBehaviour { public var wayPointArray: Transform[]; }
using UnityEngine; using System.Collections;
// Note: this is not an editor script. public class WaypointPath : MonoBehaviour { public Transform[] wayPointArray; }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thanks for helping to make the Unity documentation better!