• 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 Therian13 · Jan 19, 2015 at 07:31 AM · buttontextresolutiononguigui-button

OnGUI button changes size for resolution, but text doesnt?

Hello Everyone,

I am currently working on a series of GUIs for my game, and I have come across a strange error.

I have all my GUI buttons on Screen.width/#, etc, so that they change correctly for different screen resolutions, but the texts that are on the buttons do not.

Is there anyway to fix this? Any help would be greatly appreciated.

Here is how I am writing it. (the rest of the script is pretty much just repeats of this.

 if(GUI.Button(Rect(Screen.width/2.5,Screen.height/2,Screen.width/10,Screen.height/20), "Cancel Quest"))
             {
                     {
                     questNumber.questNumber = 0;
                     questNumLooking =0;
                     questSelected = false;
                     checkingQuest = false;
                     drawGUI = true;
                     }
                 }
             }


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

Answer by zharik86 · Jan 19, 2015 at 07:33 AM

Of course, font size don't change. Simple solution: create new GUIStyle in Inspector and change font size in Start(). Or for your case best of all the following code will approach. For example, you have LandScape orientation and for the size screen 1920 the size of the text is equal 40. Then:

  public GUIStyle myStyleBtn = null; //reference on your style for button

  void Start() {
   myStyleBtn.fontSize = (int)(40.0f * (float)(Screen.width)/1920.0f); //scale size font
   //or you can create style in program
  }

  void OnGUI() {
   //and use style for your button
   if(GUI.Button(Rect(Screen.width/2.5,Screen.height/2,Screen.width/10,Screen.height/20), "Cancel Quest", myStyleBtn)) {
    //...
   }
  }

Probably, it is better for you to use GUI.matrix for scaling of all GUI elements. Or update Unity and use new UI. I hope that it will help you.

Comment
Add comment · Show 3 · 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 Therian13 · Jan 19, 2015 at 08:10 AM 0
Share

but wouldn't that change the font style to only work with people who have a 1920X# screen?

avatar image Therian13 · Jan 19, 2015 at 08:13 AM 0
Share

ah, nevermind, I think I see now. It changes the number based from my own screen resolution to match the current screen width...

sorry, since it was in c# it threw me off a bit. I'll give it a try and let you know.

Thank you for the script also. I think I can convert it to JS.

avatar image Therian13 · Jan 20, 2015 at 02:09 AM 0
Share

Works like a charm! Thank you zharik86!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

UI Button Instead of OnGUI Button? 1 Answer

Gui button doesnt show 1 Answer

Bold and italic and other combinations in GUI Button or Grid? 0 Answers

Changing Text Based off of a Prefab 0 Answers

iphone OnGUI define and test within rect?? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges