What rect should be used to place a field using EditorGUI (not EditorGUILayout)?

If I place a field using EditorGUI it seems the field appears at the top of the inspector window instead of where my script component is located (further down). My question is how can I calculate the rect.top i need to place it at the script component instead. Is there some way to get this offset height from unity? Clearly this is a bit tricky because if other scripts are collapsed this height will need to be recalculated.

Here is the code in c#:

//EditorTest.cs:  

using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomEditor(typeof(Test))]
public class EditorTest: Editor
{
  public override void OnInspectorGUI()
  {
    EditorGUI.LabelField(new Rect(0, 0, 400, 400), "label1", "label2");
  }
}

//Test.cs:

public class Test : MonoBehaviour
{
  public float foo;
  void Start()
  {
  }
  void Update()
  {
  }
}

Thanks for your help!

... digging up an old question, anyone still care?

You can use EditorGUILayout to find the rectangle, then EditorGUI to do the actual drawing.

For example, the following will create a space 111 pixels high, and then fill it with a button (just for illustration, so you can see where the space is). Note that the space will be empty -- we use BeginHorizontal to get the rectangle, and a dummy TextArea to reserve the space.

The default inspector can be folded out (this is a handy trick to use in custom editors, so you don't need to reinvent all the default editor behaviour -- make the controlling bool a static so the foldout state is the same across all components), and when it does, your space will move down.

public static bool mShowDefaultInspector = false;
public override void OnInspectorGUI()
{
    mShowDefaultInspector = EditorGUILayout.Foldout(mShowDefaultInspector, "Default");
    if (mShowDefaultInspector)
    {
        DrawDefaultInspector();
    }

    Rect space = EditorGUILayout.BeginHorizontal();
    EditorGUILayout.TextArea(String.Empty, GUIStyle.none, GUILayout.Height(111));
    EditorGUILayout.EndHorizontal();

    GUI.Button(space, "space");
}

Once you've reserved the space, you can replace the GUI.Button call with EditorGUI.LabelField or whatever you like.

This is the one liner version.

var space = GUILayoutUtility.GetRect(GUIContent.none, GUIStyle.none, GUILayout.Height(100));

@evanmoran 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. )

From this point I would imagine you could grab components out of it and figure out if their bool is enabled or not and have a calculation to push the element down for the number of active components… but I have not researched any of that, I figured id at least give you a way to reference the window that I believe you are talking about and let you from there…

um…you should check out the answer and image on here: How to clamp EditorGUILayout controls' width ? - Unity Answers it deals with “How to adjust the width and ehight of your BeginHorizontal or beginVertical” as well as how to display Custom Inspector Layouts