- Home /
Get height of a group of GUILayout controls
Is there a way to get the height of a collection of Editor/GUILayout
controls? I know that you can use GUILayoutUtility.GetLastRect
to get the height of the last control, but what about fetching the height of the following:
void DoLayout() {
GUILayout.Label("ok");
EditorGUILayout.IntField("myInt", 32);
}
where DoLayout
contains arbitrary Editor\GUILayout
commands?
For example, is there some control that I could wrap DoLayout
in? ie,
float GetGroupHeight(Action group) {
GUILayout.BeginZone(); // made up function
group();
GUILayout.EndZone(); // made up function
return GUILayoutUtility.GetLastHeight().height;
}
where GetGroupHeight
works recursively?
Thanks.
Answer by FIFTYTWO · May 07, 2019 at 03:40 PM
Maybe it's too late to answer the question, but I looked for it too and found the way to do it. So I'll post an answer for myself to find it later or for someone else who is looking for it too. The idea is to call GUILayoutUtility.GetLastRect()
after some layout group, so it will capture the rect of the group. You should call it when EventType.Repaint occurred and save to class variable. Here is an example:
private Rect _buttonsRect;
public override void OnInspectorGUI ()
{
if( GUILayout.Button( "Log Buttons Rect" ) )
{
Debug.Log( "Buttons rect: " + _buttonsRect );
}
EditorGUILayout.BeginVertical();
for( int i = 0; i < 25; ++i )
{
GUILayout.Button( "Button " + i );
}
EditorGUILayout.EndVertical();
if( Event.current.type == EventType.Repaint )
_buttonsRect = GUILayoutUtility.GetLastRect();
}
Your answer
Follow this Question
Related Questions
Space for describing text in EditorGUILayout too short 1 Answer
Make children of a Layout Group fit their respective sizes (confusing docs) 1 Answer
Setting height via GUILayout.Height() has no effect to BeginVertical in Inspector 0 Answers
(Solution) - Can't use GUILayout stuff in PropertyDrawer.OnGUI? 2 Answers