Reputation: 1987
I'm hoping someone can help to serialize this 2d array configuration... essentially it serializies everything but doesn't serialize the 2d arrays called wallConfig0...3. Right now I have 4 of them, but I'd like to have a list of wallConfig 2d arrays that I can serialize to JSON. Please give me some guidance on how to serialize this data structure. Thanks.
-Rik
I have a list of rooms:
[Serializable]
public class RoomDataList
{
public List<RoomData> roomDataList = new List<RoomData>();
}
Each room has this data:
[Serializable]
public class RoomData {
public int[,] wallConfig0;//TODO: change to list of 2d arrays
public int[,] wallConfig1;
public int[,] wallConfig2;
public int[,] wallConfig3;
public float[] wallLoc0;//TODO: change to list of vector3
public float[] wallLoc1;
public float[] wallLoc2;
public float[] wallLoc3;
public float wallRot0;
public float wallRot1;
public float wallRot2;
public float wallRot3;
public int[,] floorConfig;
public int parentRoomIndex;
public int parentDoorIndex;
public float parentDoorWallSize;
public int roomIndex;
public Vector3 location;
public Quaternion rotation;
public string filePath;
public string[] folderContents0;
public string[] folderContents1;
}
Here are my load/save to JSON functions:
private static RoomDataList LoadRoomData(string path) {
string json = File.ReadAllText(path);
return JsonUtility.FromJson<RoomDataList>(json);
}
private static void SaveRoomData(string path, RoomDataList roomDataList) {
string json = JsonUtility.ToJson(roomDataList);
StreamWriter sw = File.CreateText(path);
sw.Close();
File.WriteAllText(path, json);
}
Upvotes: 1
Views: 16534
Reputation: 121
You can't serialize array, but you can serialize class with list, so for serializing array you need to wrap matrix to lists, wrap this lists to class and serialize this class. For example:
[System.Serializable]
public class Cell
{
//some stuff
}
[System.Serializable]
public class Array
{
public List<Cell> cells = new List<Cell>();
public Cell this[int index] => cells[index];
}
[System.Serializable]
public class Matrix
{
public List<Array> arrays = new List<Array>();
public Cell this[int x, int y] => arrays[x][y];
}
Don't forget to use this for easy data reaching like "Matrix[0,1]"
Upvotes: 3
Reputation: 1987
This is the solution that ended up working: essentially wrappers within wrappers:
[Serializable]
public class RoomDataList
{
public List<RoomData> roomDataList = new List<RoomData>();
}
[Serializable]
public class RoomData
{
public string guid;
public List<WallConfig> wallConfigList = new List<WallConfig>();
public float[] counts;
public Vector3 wallLoc0;
public Vector3 wallLoc1;
public Vector3 wallLoc2;
public Vector3 wallLoc3;
public float wallRot0;
public float wallRot1;
public float wallRot2;
public float wallRot3;
public float[] size;
public string[] doorRoomGuid;
public int parentRoomIndex;
public int parentDoorIndex;
public float parentDoorWallSize;
public int roomIndex;
public Vector3 location;
public Quaternion rotation;
public string filePath;
public string[] folderContents0;
public string[] folderContents1;
}
[Serializable]
public class WallConfig {
public List<WallFloorConfig> wallConfig=new List<WallFloorConfig>();
}
[Serializable]
public class WallFloorConfig
{
public int[] wallFloorConfig;
}
Upvotes: 1
Reputation: 502
Unfortunately Unity doesn't automatically serialize multi-dimensional arrays or Lists. In your case, a simple workaround is to replace the 2D arrays with an array of Vector2
s.
Similarly, in other situations, you can replace a multi-dimensional array with a class/struct containing as many variable as you want and then create an array of this class/struct.
Upvotes: 1