- Home /
How to align a Rect() to the bottom right of the screen
I'm trying to create a rect of text at the bottom right of the screen. Maybe because I'm a web developer, where stuff like this is easy I'm searching for a more sensble way. Surely there's a way to do this relative to the bottom right corner? In CSS it would be: position:absolute; bottom:10px; right:10px;
- easy right? But in Unity I assume the way I have to do it is calculate the height of the screen, minus the box's height, same with the screenwidth minus the box's width. Is that really the best way?
I know doing these calculations won't take me long, but I just wondered if there was a nicer way, like css' position:absolute?
And is there a way to place the text inside the rect, so that it will always adapt to the width of the Rect, or do I have to do the same calculation and have it floating above?
Answer by fafase · Nov 24, 2014 at 09:12 AM
Rect rect = new Rect(Screen.width - width, Screen.height - height, width, height);
But depending what you are using it will be different, for instance using a GUITexture is not the same.
You would have to position the GUITexture based on the normalized screen which has a different coordinate system:
guitexture.transform.position = new Vector3 (1,0,0);
this will put it in the bottom right but it won't show (Make sure your scale is (0,0,0)).
Rect rect = new Rect(-width, 0, width, height);
guiTexture.pixelInset = rect;
or you can leave it at position (0,0,0) ans use something like the first example.
Using a GUIText, the you have the CSS style or alignment bottom right.
So as you see it will depend on what you are after.