Created
October 2, 2018 07:51
-
-
Save Demkeys/81914fd750ae48557b058f0cc593a35a to your computer and use it in GitHub Desktop.
Example showing how to serialize and deserialize arrays of data in Unty
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. objectDetails01 is serialized into JSON and stored | |
// in a string. Then the JSON string is deserialized and cast to ObjectDetails type, and stored into | |
// objectDetails02. The data of objectDetails02 can be seen in the Inspector. | |
using UnityEngine; | |
public class JsonExample02 : MonoBehaviour { | |
public ObjectDetails objectDetails01; | |
public ObjectDetails objectDetails02; | |
// Use this for initialization | |
void Start () { | |
// Serialize to JSON and store in a string. | |
string s = JsonUtility.ToJson(objectDetails01, true); | |
Debug.Log(s); | |
// Deserialize from JSON and cast to ObjectDetails type. | |
objectDetails02 = (ObjectDetails)JsonUtility.FromJson(s, typeof(ObjectDetails)); | |
} | |
// Update is called once per frame | |
void Update () { | |
} | |
} | |
[System.Serializable] | |
public class ObjectDetail | |
{ | |
public string Name; | |
public Vector3 WorldPosition; | |
} | |
[System.Serializable] | |
public class ObjectDetails | |
{ | |
public ObjectDetail[] ObjectDetailArr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment