- Home /
How to align a GUI texture with the top left of the screen?
I've been looking through the 3rd person platformer tutorial and it has a good system of aligning your GUI even when the screen resolution changes. I am, however, having issues with trying to figure out how to align the texture in question with the Top Left of the screen (the platformer tutorial aligns with the bottom right or just the bottom of the screen). Does anyone know how to accomplish this? Below is the bit of code that allows for drawing to the bottom right:
function DrawImageBottomRightAligned (pos : Vector2, image : Texture2D)
{
var scaledResolutionWidth = nativeVerticalResolution / Screen.height * Screen.width;
GUI.Label(Rect (scaledResolutionWidth - pos.x - image.width, nativeVerticalResolution - image.height - pos.y, image.width, image.height), image);
}
Answer by VS48 · Nov 11, 2010 at 10:18 PM
You don't need to script anything if you know the size of the texture ahead of time.
- Set your GUITexture position to (0, 1, 0)
- Set your GUITexture pixel inset to (X: 0, Y: -256, Width: 256, Height: 256)
(obviously assumes a 256x256 texture)
What aligns this texture to the top is setting the position's Y component to 1 (the position is in viewport space where 1,1 is top-right). Then, the negative number in the pixel offset will move the texture down.
Hope this helps.
Answer by Matthew A · Nov 11, 2010 at 08:52 PM
I'm not familiar with the 3d platformer GUI code (The code you've posted looks rather odd, in that the x position is set with the "scaled" resolution, and the y with "native". Why not just use Screen.height and Screen.width directly?)
In any case, the origin is actually located in the top left, so you should just be able to use this:
GUI.Label(Rect(pos.x, pos.y, image.width, image.height), image);
Answer by StephanK · Nov 11, 2010 at 10:03 PM
Use this function to position you label/image anywhere you want. You will have to pass in positions between 0 and 1, where 0/0 will be top left corner and 1/1 would be bottom right.
function DrawImageAtRelativePosition (pos : Vector2, image : Texture2D)
{
var x = pos.x * Screen.width;
var y = pos.y * Screen.height;
GUI.Label(Rect (x, y, image.width, image.height), image);
}
Your answer
Follow this Question
Related Questions
How can I get a UI Panel's size in relation to the screen size? 1 Answer
Make my HUD full screen 2 Answers
Unity 3d C# GUI box goes off to the side needs to be in the center of the screen 0 Answers
Animating a texture, what method should I use? 0 Answers
gui position of Screen relative position 2 Answers