- Home /
How to serialized 2d array of TileBase?
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?
Answer by Long2904 · Apr 03, 2020 at 01:05 PM
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;
}
}
}
Thank you! I had been building my save system under the assumption that TileBase could not be serialized, knowing that it can has made things much easier. Stay safe during the CoralVita$$anonymous$$
Could you share how you then save the TileBases? I'm using json/Easy Save, but for some reason some TileBases can't be found upon loading (it's like they dissappear in the saving process). If you got it to work I would LOVE to see how to do it!
Ah... since then, I found out that this solution was serializing tiles as scriptableObject instances, which can change periodically. At the time, I assumed it worked because the instance id’s weren’t changing.
The simple way: create a dictionary of all tiles you want to save (before starting the game). On start, generate a second dictionary that swaps the keys and values of the first dictionary. When saving, use the tileBase as a key to get an int/string to save the time as. When loading, use the other dictionary to turn that string/int back into a tileBase. This will work even for extended tiles like rule tiles
Thank you so much for replying!
I also found out that their ID will change periodically, and yesterday I did a similar solution as the one you're describing. I made a Dictionary that holds tile-names and the tile connected to that name. Everytime I save, I save the name of the tile connected to a tile-coordinate, I then use the name to replace all the tiles when I load.
Your solution sounds a little simpler even! If you wouldn't $$anonymous$$d I would LOVE to see your code how you load/save the tiles specifically? I really searched the web for like 3 days finding nothing, so if you feel comfortable sharing I really think it could be a help to a lot of people!
Again thank you for your reply :)
Your answer
Follow this Question
Related Questions
Objects created in editor tool, fields reset on play and unable to serialize. 2 Answers
can somebody help for the algorithm or code how implement this crossword math? 1 Answer
How to use binaryformatter with custom class in unity? 1 Answer
How do I slice a terrain after it has been made? 0 Answers
"Random" Rule Tile 3 Answers