Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Add Inspector to custom EditorWindow

Discussion in 'Editor & General Support' started by babklapper, Sep 12, 2019.

  1. babklapper

    babklapper

    Joined:
    Sep 17, 2014
    Posts:
    6
    Hi,

    I would like to extend the editor a bit, and now I am struggling with a way to open an Inspector window inside my own EditorWindow.
    What I currently get is simply an empty window:
    actual.jpg
    Using the usual:
    Code (CSharp):
    1. [MenuItem("My Menu/Custom Editor")]
    2. public static void ShowWindow()
    3. {
    4.     EditorWindow baseWindow = GetWindow<MyCustomEditor>("My Window", true);
    5. }
    This works fine, of course. I have made simple windows before.

    Now, I would like to dock additional windows, such as an Inspector. I found that I can get the inspector window type through:
    System.Type inspectorType = System.Type.GetType("UnityEditor.InspectorWindow,UnityEditor.dll");
    But I am not sure how or where to actually spawn this.

    To be clear, what I would like to achieve is something like this:
    wanted.jpg

    Any help is greatly appreciated :)

    Thanks!
     
  2. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    You can't really dock different tabs in the same window to have them both visible at the same time like that, to my knowledge. The DockArea class just doesn't support it to my knowledge, although it's possible it could be hacked in. You'll notice that none of the built-in windows behave in this manner. Essentially, only one tab in a DockArea can be visible at a time. The best you could do is fake it by drawing your own second "tab" in your EditorWindow and creating your own "inspector", but it'd be kind of janky and not great. If you want to use the Inspector, let people manage it as its own window and use Selection.activeObject to control what the Inspector displays.
     
  3. babklapper

    babklapper

    Joined:
    Sep 17, 2014
    Posts:
    6
    Interesting... The second screenshot, I achieved by simply using the "add tab" in the top right manually. It isn't a Photoshop. So in unity it is possible. I just hoped it could be done through code
     
  4. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Odd. Adding a tab like that does not give me that effect. It may be a version difference; I'm using 2019.1.7f1, so a bit of an older version. You can add tabs through code, but not through any of the normal stuff; you have to use reflection.
     
  5. babklapper

    babklapper

    Joined:
    Sep 17, 2014
    Posts:
    6
    I found a few posts that mention using reflection and docking. I should try to make that work.

    I should have been a bit more specific though, I am sorry (was in a bit of a hurry yesterday).
    I am using Unity 2019.1.2f1. and when I use "add tab", it actually adds the tab tabbed, not side by side. I have to also manually drag and drop the new tab (e.g. inspector) into that location.

    I will try to figure out how to spawn and dock another tab through reflection :)
     
  6. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    You can get the DockArea that an EditorWindow is attached to by getting the m_parent field from it using reflection. You can then call the AddTab and RemoveTab methods on the DockArea to add/remove tabs. Be sure to remove a window from its current DockArea before adding it to a new one.

    However, the DockArea doesn't handle the splitting you want, the SplitView class does, and I'm not 100% sure how you'd set one up manually. You'll have to do some experimentation.
    https://github.com/Unity-Technologi...76064c75dc2705b08/Editor/Mono/GUI/DockArea.cs

    https://github.com/Unity-Technologi...6064c75dc2705b08/Editor/Mono/GUI/SplitView.cs
     
    Last edited: Sep 13, 2019