- Home /
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!
Answer by stevethorne · Dec 11, 2013 at 05:01 PM
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.
Answer by Akzwar · Nov 16, 2020 at 10:15 PM
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 );
}
}
Your answer
Follow this Question
Related Questions
Unity Editor Event System 1 Answer
Changes to Object made in custom Editor Window don't persist 0 Answers
Keep equal width for panels in EditorGUILayout.HorizontalScope 0 Answers
Execute editor window scripts when project errors are present 0 Answers
Is interraction between EditorWindow and php file possible? 1 Answer