I
I
inthevaldis2019-08-11 06:17:32
C++ / C#
inthevaldis, 2019-08-11 06:17:32

Storing a 2D array in JSON UNITY C#?

When saved, everything is preserved except the Map, which is Map = new int[width, height];
But there is nothing in the JSON file at this point.
void Update()
{
if (Input.GetKeyDown(KeyCode.LeftAlt))
{
if (Map != null)
{
mapInf.MapData = Map;
mapInf.width = width;
mapInf.height = height;
mapInf.MapOffset = MapOffset;
mapInf.sprites = sprites;
File.WriteAllText(path, JsonUtility.ToJson(mapInf));
Debug.Log("Saved!");
}
}
}
[Serializable]
public class MapInfo {
public int[,] MapData = new int[0,0];
public int width;
public int height;
public int MapOffset;
public Sprite[] sprites;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Griboks, 2019-08-11
@inthevaldis

Open the help, read: "The types of fields that you want to be included must be supported by the serializer; unsupported fields will be ignored, as will private fields, static fields, and fields with the NonSerialized attribute applied."

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question