0

I am creating a game in Unity that uses Tilemaps to procedurally generated a world similar to how a 2D Minecraft would work. I have two classes: Chunk which is 16x16 int array containing tile information, and Region which is 3x3 Chunk array. Chunks are used to load tiles onto the tilemap and Regions are used to save and load data to JSON files. When a player is playing, there will be a 3x3 chunk area loaded around their current chunk and a 3x3 region area loaded around their current region. The following is the classes:

//Stores tile data of a chunk
[Serializable]
public class Chunk
{
    public int[,] tiles = new int[CHUNK_SIZE, CHUNK_SIZE];
}

//Stores Chunk data of a region
[Serializable]
public class Region
{
    public Vector2Int location;
    public Chunk[,] chunks = new Chunk[REGION_SIZE, REGION_SIZE];
}

The following is the code that generates a region, fills the chunks, and then serializes it:

Region region = new Region();
    region.location = new Vector2Int(x, y);
    for (int i = 0; i < REGION_SIZE; i++)
    {
        for (int j = 0; j < REGION_SIZE; j++)
        {
            region.chunks[i, j] = GenerateChunk();
        }
    }
    string regionJson = JsonUtility.ToJson(region);

The problem I am having is that the resulting regionJson string only contains the position information and not the chunks information. I know the JsonUtility is minimal and has problem with arrays, however I thought wrapping an array within a class solves this issue. What I'm trying to figure out is if the problem is from the Chunk class containing an array, the Region class containing an array of Chunks, or something totally else. Any help is greatly aprreciated!

3
  • 2
    JsonUtility doesn't support serialization of multi-dimensional arrays. I think you check here for a workaround: stackoverflow.com/questions/58559529/… Commented Oct 27, 2021 at 19:19
  • 3
    Read Script Serialization for built-in supported types ... for the rest use Newtonsoft JSON.Net (by now comes as package via the package Manager) or other third party libraries Commented Oct 27, 2021 at 19:28
  • 1
    @BFyre Never saw anything about multi-dimensional arrays not being supported. Thanks for the clarification and the linked solution! Commented Oct 27, 2021 at 20:25

1 Answer 1

2

I would not pull my hair to make a workaround to use JsonUtility in this scenario when JsonNet does it simply for me.

using Newtonsoft.Json;
string regionJson = JsonConvert.SerializeObject(region);

I've tested your scenario by generating some fake data and it works perfectly with multi-dimensional arrays.

Sign up to request clarification or add additional context in comments.

1 Comment

Works like a charm. Thanks for going ahead and testing it out for me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.