private string SerializePlayerObjectToString() { // Create example player (c# object) ExamplePlayerObject examplePlayer = new ExamplePlayerObject(); examplePlayer.SetTestValues(); // Print out current player data Debug.Log("Original player: " + examplePlayer); // Serialize ExamplePlayerObject to JSON object JSON json = JSON.Serialize(examplePlayer); // Output JSON string jsonString = json.CreateString(); Debug.Log(jsonString); // Content of 'jsonString' will be: // {"name":"Test player","position":{"x":1.0,"y":2.0,"z":3.0}, // "playerColor":{"r":0.0,"g":1.0,"b":0.1,"a":0.9},"score":42000,"levelTimes":[31.41,42.0,12.3], // "playerBackPack":[{"name":"axe","uses":99},{"name":"coin","uses":1}],"charClass":{"value__":1}} return(jsonString); }
private void DeserializeStringToPlayerObject(string jsonString) { // Create JSON object from string JSON json = JSON.ParseString(jsonString); // Re-create ExamplePlayerObject from JSON ExamplePlayerObject restoredPlayer = json.Deserialize <ExamplePlayerObject>(); // Print out Debug.Log("Restored player: " + restoredPlayer); }