Unity Inspector limiting display of nested lists.

I’m having a problem where when I want to inspect an object that has nested lists in the inspector causes the unity editor to become unresponsive.

I basically have the following code:

[System.Serializable]
public class Cell{
     public List<Cell> AdjacentCells;
}

And a monobehaviour that has a Cell.

In my current testscene I have 4 Cells and each of the cell has the three other ones as an adjacent cell, so:
cell 1 has cell 2, 3 and 4 as adjacent cells.
cell 2 has cell 1, 3 and 4 as adjacent cells. etc.

the problem is when I want to inspect that monobehaviour to make sure all adjacentcells are properly set, unity becomes unresponsive because when it want to display the properties it is basically doing an endless loop through those lists of adjacent cells.

Now I was wondering if anyone knows of a way to make a custom inspector that only shows the adjacent cells for the root nodes?

The problem is that this can exceed the serialization depth limit. Your cell has a list of cells, each of those has a list of cells, and so on potentially infinitely.

Please see my answer on this question for a possible solution.

This kind of structure is not supported by Unity’s serialization system, It’s not really a visual / display problem. Custom serializable classes are not serialized as unique instance but simply has sub and sub-sub properties of the MonoBehaviour instance. Circular dependencies are not supported at all. The serializer usually detects such things since it simply stops after a nested depth of 7.

You need your cells as an actual asset which can be referenced. So either make it a MonoBehaviour or a ScriptableObject. Those can be serialized on their own and have an actual asset / instance ID which is used for cross references between assets / instances.

An alternative way is to only have a single array with all cells in it, make the List non serialized and use the ISerializationCallbackReceiver interface to save the cross references in a different way. For example as indices into that large array. Though it’s usually better to use ScriptableObjects and save them as assets. Keep in mind that you can save multiple ScriptableObjects into a single assetfile. Just have a look at the AssetDatabase and more specifically AddObjectToAsset.

Of course displaying them in a tree-like fashion requires you to create a custom inspector for your MonoBehaviour,