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

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 Is there a way to hide a MonoBehaviour in the Inspector? - Questions & Answers - Unity Discussions. 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.

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 :slight_smile:

#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! */ }

}