Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Mackuss · Jul 13, 2011 at 02:42 AM · guionguiparameters

GUI Placement Question.

Hey guys I'm currently making a mini map to go on a screen. I need it to go in the top right of the screen. When placing things on the screen such as:

GUI.Label (Rect (0,Screen.height - 390,150,150), imageFile);

obviously the "0" in the first parameter is easy to do because it's in the top left of the screen.

However, how would I code it to always be at the top RIGHT of any screen on any resolution? I'm sure it's easy...just...unknown to me, lol.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Jul 13, 2011 at 02:51 AM

You should subtract the rectangle width from the screen width. Supposing W and H are the image width and height:

   GUI.Label (Rect(Screen.width - W, 0, W, H), imageFile);

EDITED: Well, I finnally got what's the problem: you've altered the GUI scale using GUI matrix, but nobody told it to Screen.width. In other words, Screen.width is still expressed in scale 1, while all GUI itens are using the scale you've defined. The solution is to convert the screen width to this scale:

 // calculate scale
 var scale: float = Screen.height / nativeVerticalResolution;
 var newScreenW: float = Screen.width / scale; // adjust screen width...
 var newScreenH: float =  nativeVerticalResolution; // and height
 GUI.matrix = Matrix4x4.TRS(Vector3(0,0,0),Quaternion.identity,Vector3(scale,scale,1));
 // the rect will be calculated using the new screen width:
 GUI.Label (Rect(newScreenW - W, 0, W, H), imageFile);
 // the same thing, but at the bottom-left corner:
 GUI.Label (Rect(0, newScreenH - H, W, H), imageFile);
Comment
Add comment · Show 7 · 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 Mackuss · Jul 13, 2011 at 03:08 AM 0
Share

That's putting it at the top, but dead center. Hhm...

avatar image aldonaletto · Jul 13, 2011 at 03:25 AM 0
Share

You should set the W and H values in the above example to the width and height of the texture, or else the image will appear at the wrong position. The image in GUI.Label is always aligned to the top-left of the rect passed as the first parameter; if the rectangle has a different size, the image will not appear where you expect.

avatar image Mackuss · Jul 13, 2011 at 03:32 AM 0
Share

Yeah, you mean like:

GUI.Label (Rect(Screen.width - imageFile.width, 0, imageFile.width, imageFile.height), imageFile);

(even if I defined them as variables, it's the EXACT same thing as doing what I just did, yes?)

?

avatar image Mackuss · Jul 13, 2011 at 04:27 AM 0
Share

I currently have the $$anonymous$$atrix:

GUI.matrix = $$anonymous$$atrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (Screen.height / nativeVerticalResolution, Screen.height / nativeVerticalResolution, 1));

to automatically set the GUI to the current screen resolution. The code you provided works WITHOUT that in it, but ofcourse, there are other problems. I think it is ideal to have the matrix for obvious reasons, but again, the code you provided is not working with it.

avatar image aldonaletto · Jul 13, 2011 at 10:20 AM 0
Share

Dear God! What are you trying to do? $$anonymous$$essing with GUI.$$anonymous$$atrix can drive you nuts, unless you know exactly what you're doing! But you may try a simple alternative: save the original GUI.$$anonymous$$atrix, do whatever you want to do with your matrix, then restore GUI.$$anonymous$$atrix:

 function OnGUI(){
 
   GUI.Label(Rect(....),imageFile);
   var old$$anonymous$$atrix = GUI.matrix; // save original matrix
   GUI.matrix = $$anonymous$$atrix4x4.TRS(...);
   // do whatever you want with the new matrix
   GUI.matrix = old$$anonymous$$atrix; // restore it
 }

I used this trick once to rotate a texture in a map.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Free Gui solutions 2 Answers

Unity 4.7 - OnGUI prevent click/touch through 0 Answers

How do i change the GUI.Box font size and box size ? 0 Answers

How expensive is Rect.Contains(Event.current.mousePosition)? 1 Answer

GUI functions inside OnGUI giving argument exception 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