Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Alexis · Nov 11, 2010 at 08:01 PM · guiscreendrawalign

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

  1. Set your GUITexture position to (0, 1, 0)
  2. 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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
}
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges