- Home /
How can I override the default inspector editor with my own?
Hello everybody.
I try to create a custom editor, which should replace the old one (from unity). My target is to allow the programmer to define the design of the editor just with attributes at the properties in the script.
For that I want to override the default inspector editor, so that my new one is always used.
My question:
How can I achieve that my editor is used for default?
I don't want to use:
[CustomEditor(typeof(MyClass))]
Because then I would need to create a copy of this editor script for each script...
Greetings
Chillersanim
I don't think its possible without using
[CustomEditor(typeof($$anonymous$$yClass))]
Answer by TonyLi · Jul 08, 2013 at 01:52 PM
If all of your scripts derive from the same base class, you can put the code in a custom inspector for the base class. Then each subclass can just inherit this.
For example, say you have these scripts:
public class NPC : MonoBehaviour {
public string title;
}
public class Friend : NPC {
}
public class Neutral: NPC {
}
public class Enemy : NPC {
public bool aggressive;
}
Then you could have these custom inspector classes:
[CustomEditor(typeof(NPC))]
public class NPCEditor : Editor {
public override void OnInspectorGUI() {
title = EditorGUILayout.TextField("Title", title);
}
}
[CustomEditor(typeof(Friend))]
public class FriendEditor : NPCEditor {}
[CustomEditor(typeof(Neutral))]
public class NeutralEditor : NPCEditor {}
[CustomEditor(typeof(Enemy))]
public class EnemyEditor : NPCEditor {
public override void OnInspectorGUI() {
base.OnInspectorGUI();
// Add another property to inspector:
aggressive = EditorGUILayout.Toggle("Aggressive", aggressive);
}
}
Ah you gave me a idea. I could directly use [CustomEditor(typeof($$anonymous$$onoBehaviour))] or wouldn't that work?
I want to make an editor which reads all properties and draws them automaticaly depending on the attributes of them. So that you could define the design inside the script.
If you're talking about replacing the default editor for all $$anonymous$$onoBehaviours, I would recommend @JohnnySunshine's Property Drawers answer ins$$anonymous$$d. It requires Unity 4, but it allows the programmer to specify custom handling on a property-by-property basis.
Well, it is a good idea, but with that, I cannot implement grouping, limiting and that sort of stuf. At least not as far as I understood. I will make some tests and after that, I'm gona tell you how it worked. Until then I cannot say which answer I should accept...
Answer by Cameron_SM · Jan 24, 2015 at 01:48 PM
Not sure if this feature existed when this question was asked, but the CustomEditor attribute also takes a second boolean paramater to allow your editor to be applied to child classes too.
If you wanted to add something to every editor you could in fact use:
[CustomEditor(typeof(MonoBehaviour), true)]
http://docs.unity3d.com/ScriptReference/CustomEditor-ctor.html
Answer by JohnnySunshine · Jul 08, 2013 at 02:31 PM
You might actually be interested in Property Drawers. They help to customize the inspector window based on attributes on your public properties.
Great hint. I will surely use them, they look really mighty. Thanks.
Your answer
Follow this Question
Related Questions
Draw specific Object Inspector into Rect 1 Answer
Way to graphically visualize a Vector2 or Vector3 in inspector? 2 Answers
Customizing Inspector variables. 2 Answers
How to set a shared material in inspector? 0 Answers
How do I get the default scene GUI for a CustomEditor for RectTransform? 1 Answer