Tri-Inspector/Editor/Elements/TriHorizontalGroupElement.cs

61 lines
1.7 KiB
C#
Raw Normal View History

2022-01-30 11:01:58 -05:00
using TriInspector.Utilities;
using UnityEditor;
using UnityEngine;
2022-01-15 10:06:45 -05:00
namespace TriInspector.Elements
{
public class TriHorizontalGroupElement : TriPropertyCollectionBaseElement
{
public override float GetHeight(float width)
{
if (ChildrenCount == 0)
{
return 0f;
}
var height = 0f;
2022-01-30 11:01:58 -05:00
var spacing = EditorGUIUtility.standardVerticalSpacing;
var totalWidth = width - spacing * (ChildrenCount - 1);
var childWidth = totalWidth / ChildrenCount;
2022-01-15 10:06:45 -05:00
for (var i = 0; i < ChildrenCount; i++)
{
var child = GetChild(i);
var childHeight = child.GetHeight(childWidth);
height = Mathf.Max(height, childHeight);
}
return height;
}
public override void OnGUI(Rect position)
{
if (ChildrenCount == 0)
{
return;
}
2022-01-30 11:01:58 -05:00
var spacing = EditorGUIUtility.standardVerticalSpacing;
var totalWidth = position.width - spacing * (ChildrenCount - 1);
var childWidth = totalWidth / ChildrenCount;
2022-01-15 10:06:45 -05:00
for (var i = 0; i < ChildrenCount; i++)
{
var child = GetChild(i);
var childRect = new Rect(position)
{
width = childWidth,
2022-01-30 11:01:58 -05:00
height = child.GetHeight(childWidth),
x = position.x + i * (childWidth + spacing),
2022-01-15 10:06:45 -05:00
};
2022-01-30 11:01:58 -05:00
using (TriGuiHelper.PushLabelWidth(EditorGUIUtility.labelWidth / ChildrenCount))
{
child.OnGUI(childRect);
}
2022-01-15 10:06:45 -05:00
}
}
}
}