- Home /
gui content dynamic change with screen size
So I understand how to use Screen.width and Screen.height to set my group, box and buttons to scale with different screen sizes.
How would you control the button sizes so they are always proportional to the box? I cant seem to find a way to reference the box.
And as the buttons change how to dynamically adjust the font sizes of text in the buttons at the same time so the text always fits in the buttons. Any thoughts or insights? I did a lot of searching on this and maybe missed the answer. If you have seen it a link would be great. Thanks.
For example ....
void OnGUI () {
GUI.BeginGroup(new Rect(Screen.width/2 -(Screen.width*0.9f/2), Screen.height - Screen.height * .05f, Screen.width * .9f, Screen.height*.05f));
GUI.Box(new Rect(0,0, Screen.width * .9f, Screen.height*.05f), "");
GUI.Button(new Rect(5,5,100,40),"this is a bunch");
GUI.EndGroup();
}
For the font you'll probably have to reCreate it with the proper size set..
GUIStyle myStyle = new GUIStyle();
myStyle.font = myFont;
GUI.Label(new Rect(10,10, 100, 30), "Hello World!", myStyle);
For the button - you would have to check the size of the box (assu$$anonymous$$g that the button is inside the box) and just change the buttons size depending on the box size.. (well actually the rectangle)..
So have a look at https://docs.unity3d.com/Documentation/ScriptReference/Rect.html and
get the Rect() size with
var rect = Rect (0, 0, 10, 10);
rect.width...
rect.height...
and use that to calculate the buttons size.. so basically if you want the buttons size to be 1/2 of the rect, you'd just do...
var rect = Rect (0, 0, 100, 100);
GUI.Button(Rect(10,10,rect.width/2,rect.height/2),btnTexture)
Gotcha ... so I would need to have a rect var for each of the buttons in my Group/Box and adjust them as needed. And then for the fonts I need a set that would cover the variations I anticipate and have them available to swap in as needed.
I will give this a try, and let you know. Thanks.
Answer by zharik86 · Mar 08, 2014 at 06:04 AM
Use GUI.matrix. As to work with it, watch my pregoing response. I hope, it will help you.
Very cool, looks promising. I will try it out. Thank you.
Nice, a quick test appears to be doing the trick. Now to add complexity. Thanks again!!!
I am having the hardest time finding a discussion describing what makes this work. It appears that the Gui.matrix is a constant of the OnGUI function and simply calling it then effects everything within OnGUI. Sweet!!!! But if anyone can point me to a detailed discussion of how this is operating I would appreciate it. Thanks again!
Your answer
Follow this Question
Related Questions
[FIXED] Why is my GUI.Button always pressed? 0 Answers
GUI Error - Method not found: 'GUI.BeginGroup'. 1 Answer
OnGui Function help 1 Answer
GUI.Style is messing up big time 0 Answers