Is there an EditorWindow is docked value?

Hi, I'd like to figure out if the EditorWindow has an undocumented value or function that lets me know if it is docked or not. The reason I need it is because when I try to resize the window via script, it undocks it, so if I could check for that to not resize it, that would be great!

I know this is very late but there are probably more people looking for this answer. There is a solution! Unfortunately it isn’t a public variable, so you’ll have to use some fancy workarounds to do it. Here’s what I use to get the “docked” variable from an EditorWindow.

BindingFlags fullBinding = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;

MethodInfo isDockedMethod = typeof( EditorWindow ).GetProperty( "docked", fullBinding ).GetGetMethod( true );

if ( ( bool ) isDockedMethod.Invoke(this, null) == false ) // if not docked
    m_This.position = new Rect( 40, 40, Screen.currentResolution.width / 3, Screen.currentResolution.height / 4 * 3 );

What’s going on here?

The first line with BindingFlags sets up the binding for our Method info to allow us to get any variable from EditorWindow, whether it is an instance variable, static variable, public variable, or non-public variable.

The if statement contains a lot in it but I’ll break it down here.

MethodInfo isDockedMethod = typeof( EditorWindow ).GetProperty( "docked", fullBinding ).GetGetMethod( true );

This gives us a MethodInfo which allows us to call a function or get a variable based on a string. We’re getting a MethodInfo for a property called “docked” that follows our BindingFlags for a class called EditorWindow. This MethodInfo is kind of useless on its own, though. So we need to invoke this MethodInfo on an object, and that object is “this”.

if ( ( bool ) isDockedMethod.Invoke(this, null) == false )

This part invokes the MethodInfo on “this” and checks if the variable “docking” is set to false.

One can insert this in class inherited from EditorWindow

public bool IsDocked
{
    get
    {
        BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;
        MethodInfo method = GetType().GetProperty( "docked", flags ).GetGetMethod( true );
        return (bool)method.Invoke( this, null );
    }
}

You can now actually just use the variable directly, no need to hassle with binding flags and reflection.

public class MyWindow : EditorWindow 
{
    private void OnGUI()
    {
        if(!docked) 
        {
            //do something when not docked
        }        
    }
}