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 GenericUsername_1004 · Apr 27, 2012 at 06:51 AM · guibuttonscalecontrolwindow

How do I draw controls in a GUI Window - based on screen dimensions?

It seems that whenever I try to code any GUI stuff that should theoretically work, Unity doesn't like it. In the Script Reference, they tell you how to draw Controls, but they only use integer values. However, I'd like the buttons inside to change position relative to the Screen Resolution, but it doesn't really work. Any ideas?

Code:

 var w : int;
 var h : int;
 var csw : float;
 var csh : float;
 
 function Update()
 {
     w = Screen.width;
     h = Screen.height;
     csw = w*0.9;
     csh = h*0.9;
 }
 
 function OnGUI()
 {
     if(ButtonMaker.clickedButton == 1){
         GUI.Window(0,Rect(w/10,h/10,w*0.8,h*0.8),MultiMenu,"Multiplayer Menu");
     }
 }
 
 function MultiMenu(windowID : int)
 {
     if(GUI.Button(Rect(csw-32,csh,32,32),"",close))
     {
         freezeMainMenu = false;
         ButtonMaker.clickedButton = 0;
     }
 }
Comment
Add comment · Show 2
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 $$anonymous$$ · Apr 27, 2012 at 08:20 AM 0
Share

What doesn't work in your code?

avatar image GenericUsername_1004 · Apr 28, 2012 at 11:48 AM 0
Share

In the line"if(GUI.Button(Rect(csw-32,csh,32,32),"",close))" which should render a Button, the button doesn't appear, but if I used only integer values ins$$anonymous$$d, it would appear.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Fabkins · Apr 28, 2012 at 12:27 PM

What I do is use co-ordinates based on a percentage of the screen rather than absolute position. So I've this utility function to create me a Rect:

 function SR( x: float, y: float, width: float, height: float)
 {
 return ( Rect (  Screen.width *x, Screen.height *y, 
                 Screen.width * width, Screen.height *height) );
 }

Anytime I want to place a GUI item I then do something like:

 GUILayout.BeginArea ( SR(0.1,0.5,0.8,0.2))

This for example will create an area that uses most of the screen at is 10% (0.1) in on the left and is 80% (0.8) of the screen width.

Thats all well and good but then for things like buttons its no good as it depends on the apsect ratio. ie the button will be a strange shape the screen is landscape vs portrait.

So for buttons / labels I do something a bit different. I have a function that basically uses the a percentage of the largest of either width or height.

 function SP( size: float)
 {
 if ( Screen.width > Screen.height)
 {
     return( Screen.width * size);
 }
 else
 {
     return( Screen.height * size);
 }
  } 

I can use that with buttons like this:

  GUI.Button(Rect( SP(0.025),SP(0.025), SP(0.08), SP(0.08) ),buttonIcon);

As long as the aspect ratio generally stays the same the system works well. ie 3:2 or 4:3 in either orientation are close enough. Screens wont be identical between a 3:2 and 4:3 device buts its close enough.

I have an App that displays things correctly on retina display iPad and iPhone as well as non-retina display iPhone and iPad in either landscape or portrait orientation. Looks good on the computer screen as well.

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 GenericUsername_1004 · Apr 28, 2012 at 04:24 PM

Alright, I'll give this a try. Thanks!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to make camera position relative to a specific target. 1 Answer

Buttons in window problem 2 Answers

Is there a way to use ScaleMode.Scaletofit on buttons? 0 Answers

Dragging and Locking Buttons. 1 Answer

Add EditorUI elements when a button is pressed 0 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