Editor
Namespace: UnityEditor
/
Inherits from: ScriptableObject
Suggest a change
Success!
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.
Close
Sumbission failed
For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
Close
Description
Base class to derive custom Editors from. Use this to create your own custom inspectors and editors for your objects.
Consider a script MyPlayer with variables for armor, damage, and a reference to a gun GameObject:
// MyPlayer.js
// This is not an editor script.
var armor : int = 75;
var damage : int = 25;
var gun : GameObject;
function Update () {
// Update logic here...
}
// Custom Editor using SerializedProperties.
// Automatic handling of multi-object editing, undo, and prefab overrides.
#pragma strict
@CustomEditor(MyPlayer)
@CanEditMultipleObjects
class MyPlayerEditor extends Editor {
var damageProp : SerializedProperty;
var armorProp : SerializedProperty;
var gunProp : SerializedProperty;
function OnEnable () {
// Setup the SerializedProperties
damageProp = serializedObject.FindProperty ("damage");
armorProp = serializedObject.FindProperty ("armor");
gunProp = serializedObject.FindProperty ("gun");
}
function OnInspectorGUI() {
// Update the serializedProperty - always do this in the beginning of OnInspectorGUI.
serializedObject.Update ();
// Show the custom GUI controls
EditorGUILayout.IntSlider (damageProp, 0, 100, new GUIContent ("Damage"));
// Only show the damage progress bar if all the objects have the same damage value:
if (!damageProp.hasMultipleDifferentValues)
ProgressBar (damageProp.intValue / 100.0, "Damage");
EditorGUILayout.IntSlider (armorProp, 0, 100, new GUIContent ("Armor"));
// Only show the armor progress bar if all the objects have the same armor value:
if (!armorProp.hasMultipleDifferentValues)
ProgressBar (armorProp.intValue / 100.0, "Armor");
EditorGUILayout.PropertyField (gunProp, new GUIContent ("Gun Object"));
// Apply changes to the serializedProperty - always do this in the end of OnInspectorGUI.
serializedObject.ApplyModifiedProperties ();
}
// Custom GUILayout progress bar.
function ProgressBar (value : float, label : String) {
// Get a rect for the progress bar using the same margins as a textfield:
var rect : Rect = GUILayoutUtility.GetRect (18, 18, "TextField");
EditorGUI.ProgressBar (rect, value, label);
EditorGUILayout.Space ();
}
}
// Custom Editor the "old" way by modifying the script variables directly.
// No handling of multi-object editing, undo, and prefab overrides!
@CustomEditor (MyPlayer)
class MyPlayerEditor extends Editor {
function OnInspectorGUI () {
target.damage = EditorGUILayout.IntSlider ("Damage", target.damage, 0, 100);
ProgressBar (target.damage / 100.0, "Damage");
target.armor = EditorGUILayout.IntSlider ("Armor", target.armor, 0, 100);
ProgressBar (target.armor / 100.0, "Armor");
var allowSceneObjects : boolean = !EditorUtility.IsPersistent (target);
target.gun = EditorGUILayout.ObjectField ("Gun Object", target.gun, GameObject, allowSceneObjects);
}
// Custom GUILayout progress bar.
function ProgressBar (value : float, label : String) {
// Get a rect for the progress bar using the same margins as a textfield:
var rect : Rect = GUILayoutUtility.GetRect (18, 18, "TextField");
EditorGUI.ProgressBar (rect, value, label);
EditorGUILayout.Space ();
}
}
Inherited members
Messages
OnDestroy |
This function is called when the scriptable object will be destroyed. |
OnDisable |
This function is called when the scriptable object goes out of scope.
|
OnEnable |
This function is called when the object is loaded. |