Saving and Loading Tilemaps with json

Hello All,

I have been working on a project in unity that requires me to save several Tilemaps to the cloud and download them again at a later date (from the DB even after closing the app).

I have been using JSON for all other DB stuff (I am using Firebase RTDB) and things work great, I also use Addressables with Google cloud storage for sprites etc. (not sure if that is somehow going to become part of my required solution… just more info)

This is a short snippet of the output I get while trying to transition back from JSON to my class which contains a bunch of tilemaps.

"{\"galaxy_Background_Map\":{\"m_FileID\":14900,\"m_PathID\":0},\"galaxy_Foreground_Map\":{\"m_FileID\":14912,\"m_PathID\":0},\"galaxy_GalaxyItemParent_Map\":{\"m_FileID\":14918,\"m_PathID\":0},\"galaxy_Grid_Map\":{\"m_FileID\":14908,\"m_PathID\":0},\"galaxy_Midground_Map\":{\"m_FileID\":14914,\"m_PathID\":0},\"galaxy_Territory_Map\":{\"m_FileID\":14910,\"m_PathID\":0},\"galaxy_TileSelect_Map\":{\"m_FileID\":14902,\"m_PathID\":0},\"shipSlot1_Map\":{\"m_FileID\":14904,\"m_PathID\":0},\"shipSlot2_Map\":{\"m_FileID\":14906,\"m_PathID\":0},\"shipSlot3_Map\":{\"m_FileID\":14916,\"m_PathID\":0},\"shipSlot4_Map\":{\"m_FileID\":14920,\"m_PathID\":0}}"

The big problem here is that these show as null when I try to shove it back into the tilemaps… not sure if there is a better way to do this.

How I am saving:

string jsonGalaxy_Save = JsonUtility.ToJson(galaxy);
databaseReference.Child(CurrentGalaxy_GetDBPath()).SetRawJsonValueAsync(jsonGalaxy_Save);

How I am loading:

DataSnapshot snapshot = task.Result;
string json_snapshot = snapshot.GetRawJsonValue();
Galaxy galaxy = JsonUtility.FromJson<Galaxy>(json_snapshot);

When loading, you can never re-create a MonoBehaviour or ScriptableObject instance directly from JSON. The reason is they are hybrid C# and native engine objects, and when the JSON package calls new to make one, it cannot make the native engine portion of the object.

Instead you must first create the MonoBehaviour using AddComponent() on a GameObject instance, or use ScriptableObject.CreateInstance() to make your SO, then use the appropriate JSON “populate object” call to fill in its public fields.