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 Pearbook · Jul 31, 2014 at 04:45 PM · androidguiiossizedevice

GUI Size on different Android/iOS Devices?

I'm wondering how to properly Size my GUI elements, I'm relatively new to scripting GUI. Although it isn't hard, It is quite tricky to get it right.

So the biggest problem I'm having is that on my phone everything looks perfect, the size and the position. But when I tested my game on a friends phone, the GUI was really small.

Currently I use this to get kind off the right size:

 void Update () 
     {
 
         float ButtonWidth = 0.3f * Screen.width;
         float ButtonHeight = 0.25f * Screen.height;
 
     }
 
     void OnGUI()
     {
         if(DisplayScreen)
         {
             if(GUI.Button(new Rect(Screen.width/2 - ButtonWidth/2, Screen.height/2 - ButtonHeight/2, ButtonWidth, ButtonHeight), "Start game"))
             {
                 
             }
 
             if(GUI.Button(new Rect(Screen.width/2 - ButtonWidth/2, Screen.height/2 + 10 + ButtonHeight/2, ButtonWidth, ButtonHeight), "Statistics"))
             {
                 
             }
         }
 
     }

But because not every button is the same size or placed on the same positions this could become a very annoying and it feels very inefficient. So I was wondering if there is a better way to make the GUI sizes work properly on different devices?

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
0
Best Answer

Answer by zharik86 · Jul 31, 2014 at 07:08 PM

For function OnGUI() and scaling size text into it use GUI.matrix. For example(write on CSharp):

  void OnGUI() {
   //write your GUI elements for one screen resolution, for example, 1280x720
   float scalex = (float)(Screen.width) / 1280.0f;
   float scaley = (float)(Screen.height) / 720.0f
   GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(scalex, scaley, 1);
   //and, for example, I create button with text
   GUI.Button(new Rect(0, 200, 600, 100), "myButton");
  }

I hope that it will help you.

Comment
Add comment · Show 2 · 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 Pearbook · Jul 31, 2014 at 07:59 PM 0
Share

Ah this is what is was looking for, Thank you. Was confused about the position at first, but it looks like you don't have to divide the screen width by 2 anymore.

avatar image zharik86 · Aug 01, 2014 at 05:30 AM 0
Share

@Pearbook Yes, Screen.width shouldn't divide on 2 any more. You replaced it with a matrix and specified a basic display resolution. Therefore to get to the middle it is necessary to use number 600 (1200/2.0f). Really, there will be distortions for different aspects 4:3 or 16:9. But if your application is oriented, for example, only on landscape orientation, these distortions for devices the android can be neglected.

avatar image
0

Answer by hkessock · Jul 31, 2014 at 07:15 PM

Zharik's approach is the one I use, but in order to handle different aspect ratios and avoid stretching you also have to 'letterbox' your GUI in a sense.

What I mean is that when I write GUI code and it has to run on an iPad (4:3) and a Nexus 7 (16:10) I do two things. First, in OnGUI I compute the scaling I need to pretend that every device running my code has a 2048x1536 screen like the iPad. Second, I compute the translation component of the GUI matrix so that any device with an aspect ratio different than 4:30 translates the GUI so that it is in the exact center of the device.

If I drew a white textured background in my GUI code and the camera was set to black background you would see a black vertical bar on the left of the screen and a black vertical bar on the right of the screen and a 4:3 white rectangle in the middle representing the portion of the screen the GUI can draw on.

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 okimacera2 · Jan 02, 2015 at 11:05 AM

In Javascript You Just need to set StartResolutions :))

 var CalculatePositionForNewScreen:boolean=false;
 var CalculateScaleForNewScreen:boolean=false;
 var StartResolutions:Vector2;
 var OffsetX:float;
 var OffsetY:float;
 var FindOffsetStart:boolean;
 private var Can:boolean;
 
 
 function Start () {
 if(FindOffsetStart){
 FindOffset();
 }else{
 Can=true;
 }
 }
 private var XFaz:float;
 private var YFaz:float;
 
 function Update () {
 var Widht=GetComponent.<GUITexture>().pixelInset.width ;
 var Height=GetComponent.<GUITexture>().pixelInset.height;
 
 if(Can){
 
 if(OffsetX==0){
 XFaz=((Widht-20)/2);
 }else{
 XFaz=0;
 }
 if(OffsetY==0){
 YFaz=((Height-20)/2);
 }else{
 YFaz=0;
 }
 GetComponent.<GUITexture>().pixelInset.x=((Screen.width)/2)-XFaz-OffsetX;
 
 GetComponent.<GUITexture>().pixelInset.y=((Screen.height)/2)-YFaz-OffsetY;
 CalculateOffset();
 }
 }
 
 
 
 function FindOffset () {
 var Widht=GetComponent.<GUITexture>().pixelInset.width ;
 var Height=GetComponent.<GUITexture>().pixelInset.height;
 
 OffsetX= ((Screen.width)/2)-GetComponent.<GUITexture>().pixelInset.x;
 OffsetY= ((Screen.height)/2)-GetComponent.<GUITexture>().pixelInset.y;
 
 yield WaitForSeconds(0.1);
 CalculateOffset();
 
 }
 
 
 function CalculateOffset() {
 var Widht=GetComponent.<GUITexture>().pixelInset.width;
 var Height=GetComponent.<GUITexture>().pixelInset.height;
 var Xk=(StartResolutions.x)/Screen.width;
 var Yk=(StartResolutions.y)/Screen.height;
 if(CalculatePositionForNewScreen){
 OffsetX=OffsetX/Xk;
 OffsetY=OffsetY/Yk;
 
 yield WaitForSeconds(0.001);
 StartResolutions.x=Screen.width;
 StartResolutions.y=Screen.height;
 }
 Can=true;
 if(CalculateScaleForNewScreen){
 GetComponent.<GUITexture>().pixelInset.width/=Xk;
 GetComponent.<GUITexture>().pixelInset.height/=Yk;
 }
 
 }
 
 
 
 
 
 
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

size of GUI pics too big 1 Answer

Is it possible to check if the touchscreen keyboard has been closed? 0 Answers

GUI placement problems for android. 1 Answer

Splitting Large texture among multiple planes 1 Answer

Font support on different platforms 1 Answer


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