So i made a room script to help prototyping rooms faster and it would save all the tiles (TileBase) in a 2d array. The problem with that is when i entered play mode the array would be reset and become null. Ater a little research i knew that multidimensional array couldn’t be serializable so i tried to use binaryformatter and memorystream to serialized it but failed. Then i learned that i can only use that if TileBase itself is serializable but it’s not. So are there any ways to do this?
Okay i figured it out! Make a serializable class that store the tilebase (this class don’t inheritant from mono behaviour) and have reference to this class instead.
For example:
Before:
public class Rooms : MonoBehaviour
{
private TileBase[,] allTiles;
private Matrix4x4[,] tilesRotation;
}
After:
public class Rooms : MonoBehaviour
{
public[] SerializableTile serializableTiles;
[Serializable] // this is the class unity will save so don't inheritant from mono behaviour.
public class SerializableTile
{
public TileBase tile;
public Matrix4x4 rotation;
public SerializableTile(TileBase _tile, Matrix4x4 _rotation)
{
tile = _tile;
rotation = _rotation;
}
}
}