- Home /
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;
}
}
}
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.
but wouldn't that change the font style to only work with people who have a 1920X# screen?
ah, never$$anonymous$$d, 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.
Your answer
Follow this Question
Related Questions
UI Button Instead of OnGUI Button? 1 Answer
Bold and italic and other combinations in GUI Button or Grid? 0 Answers
Gui button doesnt show 1 Answer
Cannot click UI button. 4 Answers
My dialogue system has a strange bug 0 Answers