Possibility of reusing custom editors in custom window?


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

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 realised, this couldn’t possibly utilise 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");
    }

    GameObject activeGO;
    Editor editor;

    void Update() {
        if (activeGO == Selection.activeGameObject)
            return;

        activeGO = Selection.activeGameObject;
        editor = null;

        if (activeGO != null)
            editor = UnityEditor.ActiveEditorTracker.MakeCustomEditor(activeGO.transform);

        Repaint();
    }

    void OnGUI() {
        GameObject go = Selection.activeGameObject;
        if (go == null)
            return;

        // Use the registered editor for `Transform`
        EditorGUILayout.InspectorTitlebar(true, editor.target);
        editor.OnInspectorGUI();
    }

}

Unfortunately this functionality is not currently documented and as such cannot be 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 utilise this functionality if it were documented for customisable action GUIs.

Please consider voting for Unity Technologies to add a basic level of documentation for this functionality 🙂
http://feedback.unity3d.com/unity/all-categories/1/hot/active/document-unityeditoractiveedi 

Advertisements

4 thoughts on “Possibility of reusing custom editors in custom window?

  1. Hi!

    Nice tips on your blog! Just saved me with the “lock inspector” button.

    You seem to understand really well the Editor scripts of Unity even the stuff you need to spy the DLLs to understand!

    Take a look at this tool I made [http://u3d.as/content/the-laboratory/inspector-gear]

    Here is some Intro in slide format. [http://goo.gl/aPzOtK]

    Thanks!

    1. I gleaned the “lock inspector” button trick from a chat on the IRC channel #unity3d (freenode). The folks over in IRC offer all sorts of neat tips and tricks!

      Btw your asset looks pretty neat!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s