- Home /
How could I merge 2 components' inspectors through a custom editor?
I've been thinking about doing this for a while. Is there a way to make a custom editor, that 'picks up' components? Lets say I'd have a main script called 'Manager' that'd have an a custom inspector with 3 buttons. But if I added a script called 'Extend', instead of showing the inspector for 'Extend', it'd add a button to the 'Manager' inspector. Also, I'd like the 'Manager' component to be added automatically if only the 'Extend' script is added - through RequireComponent, probably.
--David
Answer by Jamora · Jun 22, 2013 at 02:12 PM
This seems to work:
#pragma strict
@CustomEditor(tst)
class Manager extends Editor{
static var extensionFunctions = new ArrayList();
function OnInspectorGUI(){
EditorGUILayout.LabelField("Basic Manager");
for(var func:function() in extensionFunctions){
func();
}
}
}
#pragma strict
@CustomEditor (tstExtended)
class extEditor extends Editor{
function Awake(){
Manager.extensionFunctions.Add(func);
}
var func = function(){
EditorGUILayout.LabelField("Extended Manager");
};
}
Now, it doesn't completely hide the extending monobehaviour from the gameobject component list, but there are some tips on achieving that at http://answers.unity3d.com/questions/34616/is-there-a-way-to-hide-a-monobehaviour-in-the-insp.html. There is also a warning about casting Object to function, but that can be ignored if you make sure you add only functions to the ArrayList in Manager. Alternatively, make a list of functions instead of Objets in Manager.
Answer by numberkruncher · Jun 22, 2013 at 02:34 PM
In your answer you are resorting to use of static state which could lead to problems further down the line. For example, if you had multiple objects with the same component, or when you switch between scenes.
Why not just place all of your editor functionality into the one editor?
Warning: Not tested, but something like this should work :)
#pragma strict
@CustomEditor(tst)
class Manager extends Editor {
var extendedTarget:tstExtended;
var extendedSerializedObject:SerializedObject;
function OnEnable() {
tstExtended = target.GetComponent.<tstExtended>();
if (tstExtended != null)
extendedSerializedObject = new SerializedObject(tstExtended);
}
function OnInspectorGUI() {
serializedObject.Update();
// Use serialized properties as normal :)
serializedObject.ApplyModifiedProperties();
// Draw extended inspector controls when extended
// component is present.
if (extendedSerializedObject != null)
OnExtendedInspectorGUI();
}
function OnExtendedInspectorGUI() {
extendedSerializedObject.Update();
// Use serialized properties as normal :)
extendedSerializedObject.ApplyModifiedProperties();
}
}
@CustomEditor(tstExtended)
class extEditor extends Editor {
function OnEnable() {
// This might work, might not...
// Based upon suggestion in the other FAQ you linked to.
target.hideFlags = HideFlags.HideInInspector;
}
function OnInspectorGUI() { /* Intentionally empty! */ }
}
Thanks for both of your answers, I was out all day, and only saw this now. I'll make sure to test it out myself tomorrow, see what works best and then post about it ;)
Your answer
Follow this Question
Related Questions
How can i get SerializedProperty from UnityEvent which in List. Sorry for my Eng. 2 Answers
Custom editor super slow even with no draw operations 1 Answer
Saving editor-only variables 0 Answers
Overwrite the inspector window on Scene Asset heading chosen in the hierarchy window 0 Answers
Edit chosen material in the inspector for custom editor. 2 Answers