GUITable class
A Utility class for creating tables in Unity's editor GUI.
A table can either be created from scratch using new GUITable(xCount,yCount), or created using one of the static GUITable.Create overloads.
See the online documentation, for examples and more information.
Creating a matrix table for a two-dimentional array.
private GUITable table;
private void Init()
{
bool[,] boolArr = new bool[20,20];
this.table = GUITable.Create(
twoDimArray: boolArr,
drawElement: (rect, x, y) => boolArr[x, y] = EditorGUI.Toggle(rect, boolArr[x, y]),
horizontalLabel: "Optional Horizontal Label", // horizontalLabel is optional and can be null.
columnLabels: (rect, x) => GUI.Label(rect, x.ToString()), // columnLabels is optional and can be null.
verticalLabel: "Optional Vertical Label", // verticalLabel is optional and can be null.
rowLabels: (rect, x) => GUI.Label(rect, x.ToString()) // rowLabels is optional and can be null.
);
}
private void OnGUI()
{
this.table.DrawTable();
}
Creating a table for a list.
private GUITable table;
private void Init()
{
Listt<SomeClasst> someList = new List<SomeClass>() { new SomeClass(), new SomeClass(), new SomeClass() };
this.table = GUITable.Create(someList, "Optional Title",
new GUITableColumn()
{
ColumnTitle = "A",
OnGUI = (rect, i) => someList[i].A = EditorGUI.TextField(rect, someList[i].A),
Width = 200,
MinWidth = 100,
},
new GUITableColumn()
{
ColumnTitle = "B",
OnGUI = (rect, i) => someList[i].B = EditorGUI.IntField(rect, someList[i].B),
Resizable = false,
},
new GUITableColumn()
{
ColumnTitle = "C",
OnGUI = (rect, i) => someList[i].C = EditorGUI.IntField(rect, someList[i].C),
SpanColumnTitle = true,
}
);
}
private void OnGUI()
{
this.table.DrawTable();
}
private class SomeClass
{
public string A;
public int B;
public int C;
public int D;
}
Styling a cell.
Each GUITableCell has two events, OnGUI and OnGUIStyle. OnGUIStyle is called right before OnGUI, but only in repaint events.
guiTable[x,y].GUIStyle += rect => EditorGUI.DrawRect(rect, Color.red);
Row and column span.
A cell will span and cover all neighbour cells that are null.
// Span horizontally:
guiTable[x - 2,y] = null;
guiTable[x - 1,y] = null;
guiTable[x,y].SpanX = true;
guiTable[x + 1,y] = null;
// Span vertically:
guiTable[x,y - 2] = null;
guiTable[x,y - 1] = null;
guiTable[x,y].SpanY = true;
guiTable[x,y + 1] = null;
Constructors
public GUITable(int columnCount, int rowCount)
System.Int32 |
columnCount |
|
System.Int32 |
rowCount |
|
Fields
public readonly int ColumnCount
public bool RespectIndentLevel
public readonly int RowCount
Properties
public GUITableCell this[int x, int y] { get; set; }
System.Int32 |
x |
|
System.Int32 |
y |
|
public Rect TableRect { get; }
Methods
public static GUITable Create(int colCount, int rowCount, Action<Rect, int, int> drawElement, string horizontalLabel, Action<Rect, int> columnLabels, string verticalLabel, Action<Rect, int> rowLabels, bool resizable = true)
System.Int32 |
colCount |
|
System.Int32 |
rowCount |
|
System.Action<UnityEngine.Rect, System.Int32, System.Int32> |
drawElement |
|
System.String |
horizontalLabel |
|
System.Action<UnityEngine.Rect, System.Int32> |
columnLabels |
|
System.String |
verticalLabel |
|
System.Action<UnityEngine.Rect, System.Int32> |
rowLabels |
|
System.Boolean |
resizable |
|
public static GUITable Create(int rowCount, string title, params GUITableColumn[] columns)
public static GUITable Create<T>(T[, ] twoDimArray, Action<Rect, int, int> drawElement, string horizontalLabel, Action<Rect, int> columnLabels, string verticalLabel, Action<Rect, int> rowLabels)
T[,] |
twoDimArray |
|
System.Action<UnityEngine.Rect, System.Int32, System.Int32> |
drawElement |
|
System.String |
horizontalLabel |
|
System.Action<UnityEngine.Rect, System.Int32> |
columnLabels |
|
System.String |
verticalLabel |
|
System.Action<UnityEngine.Rect, System.Int32> |
rowLabels |
|
public static GUITable Create<T>(IList<T> list, string title, params GUITableColumn[] columns)
System.Collections.Generic.IList<T> |
list |
|
System.String |
title |
|
GUITableColumn[] |
columns |
|
public void ReCalculateSizes()