- Home /
Serialization of an Enum Array
I have some probably easy to solve problem regarding the serialization of an Enum-array. The following code probably explains it best:
MapGenerator.TileType[,] tilemap = new MapGenerator.TileType[2, 2];
tilemap[0, 0] = MapGenerator.TileType.Floor;
tilemap[0, 1] = MapGenerator.TileType.Wall;
tilemap[1, 0] = MapGenerator.TileType.Ramp;
tilemap[1, 1] = MapGenerator.TileType.Floor;
Debug.Log(tilemap[0, 0].ToString());
Debug.Log(tilemap[0, 1].ToString());
Debug.Log(tilemap[1, 0].ToString());
Debug.Log(tilemap[1, 1].ToString());
string json = JsonUtility.ToJson(tilemap);
Debug.Log(json);
As you can see, i'm just trying to serialize the Enum. This works on a single enum entry, but the output from my code is just {}
.
For the sake of completeness here is the Enum code:
[Serializable]
public class MapGenerator {
public enum TileType {
Floor,
Ramp,
Wall
}
}
If someone has any ideas, that would be great!
Answer by wlfbck · Jun 14, 2017 at 10:47 AM
To answer my own question: rectangular arrays currently cannot be serialized. Even normal arrays can't with unitys build in serializer. You have to use a wrapper for it.
For my case, i switched to a jagged array, serialized each row with the wrapper. Put those strings in a string array and then serialized that again. It's stupid and should be build into unity itself, but it works.
Answer by edyuto · May 05, 2018 at 02:09 PM
@wlfbck, I know maybe it's too late, but have you tried this:
public class MapGenerator {
public enum TileType {
Floor,
Ramp,
Wall
};
[Serializable]
public TileType tileType;
}
THAN$$anonymous$$ YOU! It worked for me. I' ve been trying to make it work for almost an hour :D. <3
Answer by Jamster · Jun 13, 2017 at 07:24 PM
I suspect you need to add the serializable attribute to the enum as well
[Serializable]
public class MapGenerator {
[Serializable]
public enum TileType {
Floor,
Ramp,
Wall
}
}
I'm potentially wrong though
Already tried that, thank you for the suggestion though ;)
Your answer

Follow this Question
Related Questions
c# How to read a string from Json and convert it to an enum 3 Answers
JsonData to Enum 2 Answers
Problem with second enum selection inspector 0 Answers
how to retrive an array from SQL database in Javascript? 1 Answer
MiniJSON Multiple Rows 1 Answer