I’m working on a map editor that will be used to define a sparse 3D grid of boxes (an array of a custom linked-list type). Boxes can be placed atop each other, but many boxes will not have anything on top of them, so each MapTile has a reference to the next MapTile in its stack, implementing a linked list. Everything works fine in the editor, and the generated mesh survives into runtime, but the map data itself loses all but its base array. In other words, the MapTile’s reference to an object of its own type is always null on deserialization.
An abbreviated sample:
[System.Serializable]
public class MapTile {
//this field always returns to null between edit-time and run-time
public MapTile next=null;
//tile data that deserializes fine...
}
public class Map : MonoBehavior {
[SerializeField]
MapTile[] stacks;
//code that initializes the stacks array and
//sets stacks[n].next to MapTile instances
//code that generates a 3D mesh from the stacks
}
I have a suspicion that this recursive deserialization may not be supported by Unity, because entries in stacks that should be null on deserialization (where no tile has been placed) get populated with uninitialized MapTile objects (a case I handle separately). I would really like to use Unity serialization for this, but if it’s not possible how should I work around it? Are there hooks I can implement to serialize and deserialize my Map behavior?