- Home /
Get GUI size
Hi!
I have this box: GUILayout.Box("Slider Value: ");
It will ajust its width and height automatically according to the text string, but how can I get the calculated size for it? I've tried
var a = GUILayout.Box("Slider Value: "); a.width;
but GUILayout.Box() returns void.
Thanks!
Answer by Bunny83 · Aug 21, 2012 at 04:26 PM
You can use GUILayoutUtility.GetLastRect, but you can only use this function in the Repaint event, not while you're in the Layouting step. The size isn't known during the layout step. That's what the layout step is good for. Unity will collect all information during this step and at the end it calculates the sizes / positions of all elements. In the normal repaint step all rects are valid.
It would help to know that you want to do with that value. Maybe there's another way to achieve that.
I need it because i'll have a slider bellow that box and i want it to have the same width.
That would happen automatically when you put both in a vertical group
Those groups are the real magic of GUILayout ;) They actually make up your layout.
GUILayout.BeginVertical();
GUILayout.Box("Slider Value: ");
value = GUILayout.HorizontalSlider(value, 0, 100);
GUILayout.EndVertical();
Your answer
Follow this Question
Related Questions
Object resolution 0 Answers
How to change the Width and Height in the script C#? (New Gui Unity 4.6 beta) 2 Answers
Importing texture, problem with size - width and height get changed 3 Answers
Cutoff top and bottom of screen instead of sides when changing aspect ratio 1 Answer
Having an issue with setting the minimum size for a field size 0 Answers