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.

namespace Sirenix.Utilities.Editor
{
[DefaultMemberAttribute("Item")]
public class GUITable
}

Requirements

Namespace: Sirenix.Utilities.Editor
Assembly: Sirenix.OdinInspector
Version: 2.0.0.0

See Also

Static Methods

Return Type Definition Description
GUITable Create(int, int, Action<Rect, int, int>, string, Action<Rect, int>, string, Action<Rect, int>, bool) Creates a table.
GUITable Create(int, string, GUITableColumn[]) Creates a table.
GUITable Create<T>(IList<T>, string, GUITableColumn[]) Creates a table.

Fields

Return Type Definition Description
int ColumnCount The column count.
int RowCount The row count.

Constructors

Definition Description
GUITable(int, int) Initializes a new instance of the GUITable class.

Properties

Return Type Definition Description
GUITableCell Item Gets or sets a GUITableCell from the GUITable.
Rect TableRect The Table Rect.

Methods

Return Type Definition Description
void DrawTable() Draws the table.
void MarkDirty() Recaluclates cell and column sizes in the next frame.
void 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.

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;