使用 JsonUtility 序列化二维数组 [英] Serializing a 2D array with JsonUtility

查看:103
本文介绍了使用 JsonUtility 序列化二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我尝试使用 Unity JSON 实用程序保存一些数据,但我遇到了一些麻烦.

我有一个 World 类,里面有一些参数,比如 Width Height 等,还有一个 2D 数组Tiles",它是另一个类

精简版:

公开课世界{[序列化字段]私人瓷砖[,]瓷砖;公共瓷砖[,]瓷砖{得到{返回瓷砖;} 保护集{ } }[序列化字段]私有整数宽度;公共整数宽度{得到 { 返回宽度;}}[序列化字段]私有整数高度;公共整数高度{得到{返回高度;}}公共 int WorldSize{得到{返回高度 * 宽度;}}}

在另一个脚本中,我有保存系统,目前我正试图用它的瓷砖来保存这个世界:

 public void SaveWorld(World worldToSave){SaveSystem.Init();字符串 json = JsonUtility.ToJson(worldToSave);Debug.Log("Json es:" + json);//AHORA MISMO ESTO GUARDA SOLO WIDTH Y HEIGHT DEL MUNDOFile.WriteAllText(SaveSystem.SAVE_FOLDER + "/Save.txt", json);}

Tiles 已经带有 Serializable,如果我创建一个 1D 数组,我可以保存它们并从中获取数据,但我不知道如何使用 2D 或如何更改它(它是 2D,因为我得到它们X 和 Y 坐标).

另外,我真的不明白 JSON 如何将这些磁贴包装在世界中,以及磁贴内的东西等等.

解决方案

自 Unity 序列化程序

请注意,您需要在 JSON 中序列化数组的宽度和高度.

so Im tryint to save some data with the Unity JSON utilities but Im having some trobles.

I have a World class that inside has some parameters like Width Height etc, and a 2D array of "Tiles", that its another class

Reduced version:

public class World
{
[SerializeField]
private Tile[,] tiles;
public Tile[,] Tiles { get { return tiles; } protected set { } }

[SerializeField]
private int width;
public int Width
{
    get { return width; }
}

[SerializeField]
private int height;
public int Height
{
    get { return height; }
}
public int WorldSize
{
    get
    {
        return height * width;
    }
}
}

And in another script I have the save system, currently Im trying to save this world with its tiles:

    public void SaveWorld(World worldToSave)
    {
    SaveSystem.Init();
    string json = JsonUtility.ToJson(worldToSave);
    Debug.Log("Json es: " + json);
    //AHORA MISMO ESTO GUARDA SOLO WIDTH Y HEIGHT DEL MUNDO
    File.WriteAllText(SaveSystem.SAVE_FOLDER + "/Save.txt", json);
    }

Tiles are already with Serializable, and if I make an 1D array I can save them and get data from them, but I dont know how to do it with 2D or how could I change it (its 2D because I get them with X and Y coordinates).

Also, I dont really undestand how JSON wraps this tiles inside the world, and things inside the tiles and so on.

解决方案

Since Unity serializer does not support multi-dimensional array, you can do the following:

  • convert 2D array to 1D array
  • serialize to JSON
  • deserialize from JSON
  • convert 1D array back to 2D array

Example:

namespace ConsoleApp2
{
    internal static class Program
    {
        private static void Main(string[] args)
        {
            // generate 2D array sample

            const int w = 3;
            const int h = 5;

            var i = 0;

            var source = new int[w, h];

            for (var y = 0; y < h; y++)
            for (var x = 0; x < w; x++)
                source[x, y] = i++;

            // convert to 1D array

            var j = 0;

            var target = new int[w * h];

            for (var y = 0; y < h; y++)
            for (var x = 0; x < w; x++)
                target[j++] = source[x, y];

            // convert back to 2D array

            var result = new int[w, h];

            for (var x = 0; x < w; x++)
            for (var y = 0; y < h; y++)
                result[x, y] = target[y * w + x];
        }
    }
}

Result:

Note that you will need to seralize width and height of your array in JSON.

这篇关于使用 JsonUtility 序列化二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆