wlfbck
June 13, 2017, 7:12pm
1
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!
@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;
}
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
wlfbck
June 14, 2017, 10:47am
3
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.
edyuto:
[Serializable]
This worked for me
[Serializable] public enum TileType
{
Floor,
Ramp,
Wall
};
public TileType tileType;