Possibility of reusing custom editors in a custom window?

Several weeks ago I had some ideas that would add lots of flexibility to my custom editor window. In a nutshell the idea was to add, modify and remove certain components. To be honest I thought that this would be relatively easy because this functionality is already available within Unity inspector windows. I remembered seeing EditorGUILayout.PropertyField and I foolishly assumed that would do the trick, but I was wrong…

My next attempt was to enumerate component properties using SerializedObject and SerializedProperty. Whilst this approach did in fact provide something that resembled a custom inspector, it was extremely lacking. When I realized, this couldn’t possibly utilize custom editors!

For example, the Transform component has a custom inspector which is tailored specifically for it. In Rotorz Tile System we use a custom inspector on RotorzTileSystem components which allows properties to be modified and actions to be performed. These custom inspectors are easily defined using the CustomEditor attribute. So the idea here is to reuse these custom editors (which are not necessarily created by myself) within my editor window!

With thanks to MonoDevelop’s fantastic assembly browser I came across the undocumented class UnityEditor.ActiveEditorTracker. The publicly exposed methods are well named and fairly self-explanatory. So I had a go at creating a rough mockup of a custom inspector. I was pleasantly surprised at how easy this had now become. Creating a rudimentary custom inspector is as simple as shown below:

using UnityEngine;
using UnityEditor;
 
public class CustomWindow : EditorWindow
{
    [MenuItem("Window/Test")]
    public static void Test()
    {
        EditorWindow.GetWindow<CustomWindow>("Test");
    }
 
    private GameObject activeGO;
    private Editor this.editor;
 
    private void Update()
    {
        if (this.activeGO == Selection.activeGameObject) {
            return;
        }
 
        this.activeGO = Selection.activeGameObject;
        this.editor = null;
 
        if (this.activeGO != null) {
            this.editor = ActiveEditorTracker.MakeCustomEditor(this.activeGO.transform);
        }
 
        Repaint();
    }
 
    private void OnGUI()
    {
        var go = Selection.activeGameObject;
        if (go == null) {
            return;
        }
 
        // Use the registered editor for `Transform`
        EditorGUILayout.InspectorTitlebar(true, this.editor.target);
        this.editor.OnInspectorGUI();
    }
}

Unfortunately this functionality is not currently documented and as such cannot be reliably used in a commercial Unity extension because there is no guarantee that it will continue to work in future versions of Unity. This is a great shame because I could put this to good use! I also suspect that the developers of PlayMaker could utilize this functionality if it were documented for customizable action GUIs.

Please consider voting for Unity Technologies to add a basic level of documentation for this functionality :)

https://feedback.unity3d.com/suggestions/document-unityeditoractiveedi

EDIT - This functionality has since been added in Unity 4! http://docs.unity3d.com/Documentation/ScriptReference/Editor.CreateEditor.html