Skip to content

Link List

The serializable linked list allows you to have the functionality of System.Collections.Generic.LinkedList but also allows you to automatically serialize its values.

To create a serializable linked list, simply inherit from TSerializableLinkList<T>. For example, to create a linked list that uses GameObject types:

[Serializable]
public class MyLinkedList : TSerializableLinkList<GameObject>
{ }

You can now create a list that automatically serializes its values and use it as:

public class MyComponent : MonoBehaviour
{
    public MyLinkedList list = new MyLinkedList();

    public GameObject objectA;
    public GameObject objectB;
    public GameObject objectC;

    private void Awake()
    {
        // Add elements:
        this.list.AddLast(this.objectA);
        this.list.AddLast(this.objectB);
        this.list.AddFirst(this.objectC);

        // Print the first element:
        Debug.Log(this.list.First().name);
    }
}

The previous example adds objectA and objectB at the end of the list and then adds objectC at the beginning, so the list ends up being ordered as objectC, objectA and objectB.