Why Rect is drawn top-left and Vector2 goes from bottom-right

That is just drawing me crazy.

Rect x-y goes from top-left. If i say that Rect is at position (0,0) and is 50x50px it will be drawn by GUI.DrawTexture like this :
17083-rect.png

And for Vector2 if for example i catch the touch on screen , it will give me position going from bottom-left , like here :
17084-vector2.png

So if i’m about to check if touchPosition is at position of rectangle , it would never work due to diferent coordinate systems.

In begginer tutorials , they explain that unity uses always the Vector2 coordinate system so i just don’t get why rect is drawn from top to bottom.

Is there any reason behind this or it’s some kind of bug ?

Also is there any way to tell GUI.DrawTexture to draw from bottom to top ?

I’m pretty sure they’re dumb questions but i’m new to unity , couldn’t find any anserw and would be glad for some help :).

This is by design. The discrepancy is because the rect is drawn in GUI space while the touch coordinates (the Vector2) get returned in Screen Space. Those are identical except for the fact that GUI coordinates have their origin in the upper left corner, while the screen space coordinate system has its origin in the bottom left.

I can’t tell you exactly why both coordinate systems exist, but I can tell you, it isn’t specific to Unity. All GUI frameworks use upper-left as the origin, including Microsoft’s WinForms or their WPF GUI systems, as well as Java’s Swing. It’s just the way GUI coords are defined.

The screen space being lower left is probably because they’re more closely tied to the idea of UV-coordinates, which always index into textures from the lower-left, too. That would be my theory, at least.

When converting between GUI and screen space, you just have to remember to invert the y, i.e. subtract the coordinate from Screen.height.