Skip to content

Matrix 2D

The serializable 2D matrix allows to have an array of arrays (where all rows and columns have the same size) and the structure can be serialized in order to persist in the Inspector or saving the game.

To create a serializable matrix, simply inherit from TSerializableMatrix2D<T>. For example, to create a matrix that uses GameObject:

public MyMatrix : TSerializableMatrix2D<GameObject>
{ }

You can now create a matrix that automatically serializes its values:

public MyComponent : MonoBehaviour
{
    public MyMatrix matrix = new MyMatrix(10, 5);

    private void Awake()
    {
        // Add element:
        this.matrix[2, 3] = this.gameObject;

        // Print element added
        Debug.Log(this.matrix[2, 3].name);
    }
}