将数据输出到JSON文件csharp(统一)

时间:2019-05-09 20:49:40

标签: json unity3d io

我不知道如何将复杂的数据类型输出到JSON。

我构造了一个数据类型,该数据类型基本上包含较小的数据类型,我还将该数据类型分配给了新数据类型,因此它们似乎都具有引用。我已经研究了输出复杂数据的问题,但是似乎找不到与我类似的问题。我会考虑附加数据,但是如果我可以成功输出数据类型,则此方法会简单得多。

保存数据代码

[System.Serializable]
public class SaveData
{
    public MapData mapData;
}

[System.Serializable]
public class TileData
{
    public List<BlockData> blockData;
}

[System.Serializable]
public class BlockData
{
    public Vector3 blockPosition;
    public string blockName;
    public float blockOrientation;
    public int blockLayer;
}

[System.Serializable]
public class MapData
{
    public List<TileData> tileData;
}

获取地图数据方法

    SaveData GetMapData()
    {
        mapHeight += mapStartY;
        maplength += mapStartX;

        int tileCounter = 0;

        MapData mapData = new MapData();
        SaveData saveData = new SaveData();

        List<TileData> tileList = new List<TileData>();

        for (float r = mapStartY; r < mapHeight; r++)
        {
            for(float c = mapStartX; c < maplength; c++)
            {



                Vector2 currentPosition = new Vector2(c * (blocksize)-(blocksize/2), blocksize * r -(blocksize/2));



                GameObject[] currentTile = getObjectID.RayDetectAll(currentPosition);



                if (currentTile!= null)
                {
                    //adds a tiledata list here if the tile is occupied.

                    TileData tileData = new TileData();

                    //adds a list of blocks here.
                    List<BlockData> blocks = new List<BlockData>();
                    for (int i = 0; i < currentTile.Length; i++)
                    {

                        BlockData blockData = new BlockData();


                        GameObject currentBlock = currentTile[i];

                        blockData.blockPosition = currentBlock.transform.position;
                        blockData.blockName = currentBlock.name;
                        blockData.blockOrientation = currentBlock.transform.eulerAngles.z;
                        blockData.blockLayer = currentBlock.GetComponent<SpriteRenderer>().sortingOrder;                  

                        //adds a blockdata to the blocks list
                        blocks.Add(blockData);

                        Debug.LogWarning(blockData.blockName);
                    }

                    //need to assign tile data and add a new one to the list
                    tileList.Add(tileData);

                    //assins the blocks to tile data  block data list
                    tileData.blockData = blocks;

                }
                else
                {
                    //Debug.LogWarning("warning! no objects found on tile: " + currentPosition);
                }

                tileCounter++;               
            }

        }
'''


I want the file to output all the data so that i can read the data and reassign it. Right now it outputs the data wrong.

2 个答案:

答案 0 :(得分:0)

总的来说,我认为这是可以评论的,但是我还不能发表评论。

如果只想将对象转换为Json,可以使用here中所述的JsonUtility.ToJson()吗?

答案 1 :(得分:0)

为了让人们知道,我设计了一种新方法,该方法计算所有图块的数组并将其分配给其中包含数组的数据类型。它设法从这种格式加载。