ARTICLE AD BOX
First of all, the button should be there, though it should be positioned in the top-left corner of the script Inspector, without respecting the area. I am not sure why it is not displayed for you. You may have to figure that out if after applying the solution below the button is still invisible. And, this is what the result of your code looks like for me:
Now, to the main problem. You face this because you are using the runtime methods in Editor. While they mostly work, the layout systems in runtime and in editor are different, and some of the runtime methods don't behave as expected.
GUILayoutUtility.GetRect(100f, 100f) returns a rect of (0, 0, 100, 100). But when this rect is supplied to GUI.Box, something shady is happening inside, and the box takes far more space horizontally and has offsets from all 4 sides. But when you supply the same rect to GUILayout.BeginArea, the actual rect is used, and the area begins at (0, 0) in Inspector coordinates - the top-left corner.
The first thing to do here is to surround the area with a group:
GUI.BeginGroup(rect); GUILayout.BeginArea(rect); GUILayout.Button("Button"); GUILayout.EndArea(); GUI.EndGroup();The GUI.BeginGroup is from the GUI class, and it is logical to assume that it will be handled similarly to GUI.Box. And what it does is it sets the coordinates for the contents of the group to (0, 0). Exactly what we need for BeginArea. After this change, the button should be placed in the top left corner of the box:
But BeginArea still respects the rectangle width, which is 100, and doesn't expand horizontally. To fix this, we can calculate the actual width of the area. For your use case - a top-level control - it can be done this way:
// EditorGUIUtility.currentViewWidth is close to what // we need, but it doesn't respect the margins that are applied to the box area. var viewWidth = EditorGUIUtility.currentViewWidth; // So we make a yet another GUILayoutUtility.GetRect call using it. // You can also probably use some large number here instead. // 0 to not request any vertical space. var newRect = GUILayoutUtility.GetRect(viewWidth, 0);So you should have something like this in the end:
using UnityEditor; using UnityEngine; [CustomEditor(typeof(Foo))] public class TestAreaEditor : Editor { public override void OnInspectorGUI() { var rect = GUILayoutUtility.GetRect(100f, 100f); GUI.Box(rect, "Box"); UserDraw(rect); } public void UserDraw(Rect rect) { var viewWidth = EditorGUIUtility.currentViewWidth; var newRect = GUILayoutUtility.GetRect(viewWidth, 0); rect.width = newRect.width; GUI.BeginGroup(rect); GUILayout.BeginArea(rect); GUILayout.Button("Button"); GUILayout.EndArea(); GUI.EndGroup(); } }Note that things can get trickier for the nested controls.



