Skip to content

Instantly share code, notes, and snippets.

@Demkeys
Created October 2, 2018 07:51
Show Gist options
  • Save Demkeys/81914fd750ae48557b058f0cc593a35a to your computer and use it in GitHub Desktop.
Save Demkeys/81914fd750ae48557b058f0cc593a35a to your computer and use it in GitHub Desktop.
Example showing how to serialize and deserialize arrays of data in Unty
// 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