Version 3.3.1.2

GUITable class

Namespace: Sirenix.Utilities.Editor
Assembly: Sirenix.Utilities.Editor
public class GUITable

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.

Inheritance
  • System.Object
  • GUITable
Example

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();
}
Example

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;
}
Example

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);
Example

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

GUITable(Int32, Int32)
Initializes a new instance of the GUITable class.
public GUITable(int columnCount, int rowCount)
Parameters
System.Int32 columnCount

System.Int32 rowCount

Fields

ColumnCount
The column count.
public readonly int ColumnCount
RespectIndentLevel
Whether to respect the current GUI indent level.
public bool RespectIndentLevel
RowCount
The row count.
public readonly int RowCount

Properties

Item[Int32, Int32]
Gets or sets a GUITableCell from the GUITable.
public GUITableCell this[int x, int y] { get; set; }
Parameters
System.Int32 x

System.Int32 y

TableRect
The Table Rect.
public Rect TableRect { get; }

Methods

Create(Int32, Int32, Action<Rect, Int32, Int32>, String, Action<Rect, Int32>, String, Action<Rect, Int32>, Boolean)
Creates a table.
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)
Parameters
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

Returns
GUITable

Create(Int32, String, GUITableColumn[])
Creates a table.
public static GUITable Create(int rowCount, string title, params GUITableColumn[] columns)
Parameters
System.Int32 rowCount

System.String title

GUITableColumn[] columns

Returns
GUITable

Create<T>(T[,], Action<Rect, Int32, Int32>, String, Action<Rect, Int32>, String, Action<Rect, Int32>)
Creates a table.
public static GUITable Create<T>(T[, ] twoDimArray, Action<Rect, int, int> drawElement, string horizontalLabel, Action<Rect, int> columnLabels, string verticalLabel, Action<Rect, int> rowLabels)
Parameters
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

Returns
GUITable

Type Parameters
T

Create<T>(IList<T>, String, GUITableColumn[])
Creates a table.
public static GUITable Create<T>(IList<T> list, string title, params GUITableColumn[] columns)
Parameters
System.Collections.Generic.IList<T> list

System.String title

GUITableColumn[] columns

Returns
GUITable

Type Parameters
T

DrawTable()
Draws the table.
public void DrawTable()
MarkDirty()
Recaluclates cell and column sizes in the next frame.
public void MarkDirty()
ReCalculateSizes()

Recalculates the layout for the entire table.

This method gets called whenever the table is initialized, resized or adjusted. If you are manipulating the width or height of individual table cells, remember to call this method when you're done.

public void ReCalculateSizes()