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 HenryN-24 · Feb 17, 2014 at 08:36 PM · android2dgui-problem

C# Most efficient way to create GUI for android games?

Hello,

I am creating my first android game using Unity 2D and I am 90% finished, I just need to create the GUI. At the moment I am just using onGUI. Some examples of my code:

         GUI.Label (new Rect(Screen.width/2 - 500, Screen.height/2 - 400, 1000, 600), "Title");
         
         if(GUI.Button (new Rect(Screen.width/2 - 500, Screen.height/2 - 300, 1000, 600), "Button"))
         {
             Application.LoadLevel ("Scene");
         }



At the moment I am struggling to create a GUI that will resize to fit multiple screen sizes, as there are loads of android phone resolutions. So far I have tested it on a few devices, but the major problem at the moment is the size of the GUI. I have tested the game on HTC Explorer, Nexus 7 and Galaxy Note 2. The Nexus 7 and Note 2 GUI is very small and in the center of the screen, but on the Explorer it fits quite well (The HTC Explorer is a small phone).

Is onGUI the best method for creating a user interface for what I'm doing? if it is, how would I fix my problem I stated above?

Thanks in advance :)

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 Andre Barbosa · Feb 18, 2014 at 05:48 PM

If you don't mind the aspect ratio you can do the following:

Get the two resolution modifiers (x,y), based on your working resolution:

 var resolution = new Vector2(Screen.width, Screen.height);
 var resx = resolution.x/1280.0f; // 1280 is the x value of the working resolution
 var resy = resolution.y/800.0f; // 800 is the y value of the working resolution


And then just use this modifier to resize/relocate the Rects that contain your textures:

 //If you had originally used:
 targetRect = Rect(70,370,150,140);

 //You would now change it for:
 targetRect = Rect(70*resx,370*resy,150*resx,140*resy);

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Expand object on touch? 1 Answer

buttons not working in android. 1 Answer

TouchCount always > 0 on Android 1 Answer

How to control a 2d character in an top-down android game with the standard joystick? 1 Answer

Trouble with Screen Glitch with Android Build 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