- Home /
Inspector Rect position
Hi, I just began using custom editor scripts to make my game scripts more accessible to my designers/users. However I've come across a bit of a problem. Like I said Im making the actual scripts somewhat more organized to handle, so this is: all the variables in the Inspector on that specific component.
Im using a Popup for certain variables too, and they need a Rect for position and scale and such. Is there any way to find the position of the component/script in the inspector or a way to dynamically see where the component starts?
Answer by Bunny83 · Sep 18, 2013 at 12:55 PM
Right after you drawn a GUI element you can use GUILayoutUtility.GetLastRect to get the Rect of the last element.
Note: You can only get the Rect inside the Repaint event.
GUILayoutUtility.GetLastRect works even though you haven't used a Rect yet? So it just grabs the Inspector's Rect by default?
Answer by Pryme8 · Oct 30, 2015 at 09:10 PM
@Danzou Also you can load the default game Inspector window into a EditorWindow type by finding all of the EditorWindows OnEnable and pushing the results through a loop looking for "UnityEditor.InspectorWindow" as the title of the EditorWindows....
Keep in mind in this example it is attached to a normal MonoBehavior Script, which would need to be loaded with the script that controls your user interface so that your variable is nice and fresh, that is why there is the command to execute in edit more; this would be unnecessary on a edtior script, but you would have to decide to do it on Init, or on Enable...
Example:
using UnityEditor;
using UnityEditorInternal;
[ExecuteInEditMode]
public class someClass : MonoBehaviour {
public static EditorWindow inspector;
public void OnEnable(){
EditorWindow[] windows = Resources.FindObjectsOfTypeAll<EditorWindow> () as EditorWindow[];
foreach (EditorWindow ew in windows) {
if(ew.title == "UnityEditor.InspectorWindow"){
inspector = ew;
}
}
}
}
Should get you pointed in the correct direction.... and will give you a global EditorWindow that is a reference to the default inspector window. I am able to cover the entire inspector window with my own custom window that moves with the Default Inspector and resized to its same scale.
I know its prolly not the best way to do this (in fact just using the default GUI rendering area not a custom window is the convention to make something like that, but i had my reasoning for doing it. )
If you need anymore help in this matter let me know.
Also, im sure you could pull it off with
?Shotty Example?:
EditorWindow inspector= Resources.FindObjectsOfTypeAll<EditorWindow> ("UnityEditor.InspectorWindow") as EditorWindow;
but I have never tried... Im not sure if the declaration after the type is for title or for name or for type as a string blah blah in fact Im gonna go look it up now, cause I feel like a dumb dumb not knowing what that is actually calling...
Both of those are written off the top of my head but should drop no errors, I know the top one works.