Last active
October 2, 2018 07:50
-
-
Save Demkeys/32af1600dfb981155ad4005a09901b71 to your computer and use it in GitHub Desktop.
Examples showing how to serialize and deserialize data in Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Attach this script to a gameobject in the scene. myChar01 is serialized into JSON and stored | |
// in a string. Then the JSON is deserialized and cast to Character type, and stored into myChar02. | |
// The data of myChar02 can be seen in the Inspector. | |
using UnityEngine; | |
public class JsonExample01 : MonoBehaviour { | |
public Character myChar01; | |
public Character myChar02; | |
// Use this for initialization | |
void Start () { | |
// Serialize to JSON and store in a string. | |
string s = JsonUtility.ToJson(myChar01, true); | |
Debug.Log(s); | |
// Deserialize from JSON and cast to Character type. | |
myChar02 = (Character)JsonUtility.FromJson(s, typeof(Character)); | |
} | |
// Update is called once per frame | |
void Update () { | |
} | |
} | |
[System.Serializable] | |
public class Character | |
{ | |
public string Name; | |
public int HP; | |
public int MP; | |
public string Power; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment