- Home /
Size of the header of the EditorWindow
Hi there,
I just realized, that when I use Screen.height from within an editor window, it gives my not the actual space I have to draw the things inside.
The problem => the header bar is included as well in this calculation for the height!
I measured it to be around 22 pixels, on a Window 7 / Unity Pro setup. Probably this value differs with other setting?
Is there a constant or something that gives back that value?
Answer by SteveFSP · Feb 11, 2012 at 09:03 PM
To get the draw area of an editor Window, use EditorWindow.position. Don't be fooled by the name. It's a Rect that indicates the draw area.
Edit: Added some example code.
// C# - The OnGUI() function in the EditorWindow. void OnGUI() { Rect drawArea = position; // EditorWindow.position
// Detecting the mouse inside the window.
Event evt = Event.current;
Vector2 mousePos = evt.mousePosition;
if (drawArea.Contains(mousePos))
{
// Mouse is inside the draw area.
if (evt.type == EventType.MouseDown && evt.button == 0)
{
// Clicked...
}
}
// Draw a red rectangle that fills half of the draw area.
// (From the draw origin.)
Vector3[] cellVerts = new Vector3[4];
Vector3 origin = new Vector3(drawArea.x, drawArea.y);
cellVerts[0] = origin;
cellVerts[1] = origin;
cellVerts[1].x += drawArea.width / 2;
cellVerts[2] = origin;
cellVerts[2].x += drawArea.width / 2;
cellVerts[2].y += drawArea.height / 2;
cellVerts[3] = origin;
cellVerts[3].y += drawArea.height / 2;
Handles.DrawSolidRectangleWithOutline(cellVerts, Color.clear, Color.red);
}
Great. But to calculate weather or not the mouse is inside this Rect, I still need to know how big the offset is.
Actually this does not work! EditorWindow.position does give back the size of the total EditorWindow! EditorWindow.position.height = screen.Size +1
This does definitely not take the offset for the title bar into account.
If it doesn't work, then my rather complex EditorWindow implementation must be working by magic. :-) I've added some example code derived from my own implementation. I've never had to adjust the position Rect take the tile bar into account.
Hmm, you are right! What a shame, I probably read 60 as 80, else I can not explain it. Now I had the following results:
Clicked at: (274.0, 6.0) - Was 6 Pixel below the title bar => correct drawArea: (left:314.00, top:197.00, width:952.00, height:463.00) Screen.height: 485
Height is 22 Pixel smaller => correct!
$$anonymous$$aybe work on sunday made me missinterpret it ;) Thank you a lot Steve!!!
Your answer
Follow this Question
Related Questions
Editor Window Views 0 Answers
Print to status bar. 0 Answers
SceneView.onSceneGUIDelegate GUI sorting problem 1 Answer
Lining up GUI 1 Answer
Where can I find advanced controls for custom editor? 1 Answer