Like the title said the unity json utility does not seem to allow me to serialize the DateTime structure. Just to clarify the problem is that I'm serializing this class:
[Serializable]
public class SaveData {
public Vector3 playerPosition;
public Quaternion playerRot;
public List<Item> inventory;
public DateTime saveTime;
public int playerHealth;
public int strength;
public int attack;
public int defense;
public int agility;
}
When I actually write to the json file I get everything but the DateTime and no errors. This is the result:
{
"playerPosition": {
"x": -142.27000427246095,
"y": -7.000000476837158,
"z": -112.80999755859375
},
"playerRot": {
"x": 0.0,
"y": 0.0,
"z": 0.0,
"w": 1.0
},
"inventory": [],
"playerHealth": 100,
"strength": 0,
"attack": 0,
"defense": 0,
"agility": 0
}
Is there a way around this?
A little update to this question so that I can show another way I've worked around this problem if the answer below isn't for you.
I was recently working on a similar problem and solved it by doing the follow:
I created a new class called "NewDateTime":
[System.Serializable]
public class NewDateTime {
public int second;
public int minute;
public int hour;
public int day;
public int month;
public int year;
public NewDateTime CreateNewDateTime(DateTime dateTime) {
NewDateTime newDateTime = new NewDateTime();
newDateTime.second = dateTime.Second;
newDateTime.minute = dateTime.Minute;
newDateTime.hour = dateTime.Hour;
newDateTime.day = dateTime.Day;
newDateTime.month = dateTime.Month;
newDateTime.year = dateTime.Year;
return newDateTime;
}
public override string ToString() {
return day + "/" + month + "/" + year + " at " + hour + ":" + minute + ":" + second;
}}
and then for example when I want to save the date to json I would do something like the following:
saveDateTime = newDateTime.CreateNewDateTime(DateTime.Now)
it's a rather simple workaround but I thought it might help.

