x


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?

more ▼

asked Feb 10 '12 at 06:16 PM

swisscoder gravatar image

swisscoder
380 13 15 23

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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);
}
more ▼

answered Feb 11 '12 at 09:03 PM

SteveFSP gravatar image

SteveFSP
1k 8 13 29

Great. But to calculate weather or not the mouse is inside this Rect, I still need to know how big the offset is.

Feb 12 '12 at 04:07 PM swisscoder

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.

Feb 12 '12 at 04:41 PM swisscoder

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.

Feb 12 '12 at 05:29 PM SteveFSP

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!

Maybe work on sunday made me missinterpret it ;) Thank you a lot Steve!!!

Feb 12 '12 at 06:28 PM swisscoder
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1730
x351
x71

asked: Feb 10 '12 at 06:16 PM

Seen: 664 times

Last Updated: Feb 12 '12 at 07:00 PM