Compare commits
4 Commits
4ef4a96f74
...
df5946e0a6
| Author | SHA1 | Date |
|---|---|---|
|
|
df5946e0a6 | |
|
|
d3c5a582ad | |
|
|
99c604424f | |
|
|
5ad3ef7eb6 |
|
|
@ -104,4 +104,77 @@ public class VELConnectTesting : MonoBehaviour
|
||||||
VELConnectManager.SetUserData("key1", "val1_later");
|
VELConnectManager.SetUserData("key1", "val1_later");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
JSON.Net Example that illustrates how to persist a complex object of data using VEL-Connect, initializing at start, and saving on application quit
|
||||||
|
|
||||||
|
```cs
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
using VELConnect;
|
||||||
|
public class VelConnectDemo1 : MonoBehaviour
|
||||||
|
{
|
||||||
|
class ExampleJSON
|
||||||
|
{
|
||||||
|
public string a_string="a"; //you can use initializers
|
||||||
|
public int a_int=0;
|
||||||
|
public List<ExampleChildJSON> a_list = new List<ExampleChildJSON>(); // you can use lists of objects
|
||||||
|
}
|
||||||
|
class ExampleChildJSON
|
||||||
|
{
|
||||||
|
public string a_string; //if you don't, that's fine too, but you probably want a constructor then
|
||||||
|
public int a_int;
|
||||||
|
public ExampleChildJSON() { } //you need to make sure you have a blank constructor for deserialization
|
||||||
|
public ExampleChildJSON(string a_string, int a_int)
|
||||||
|
{
|
||||||
|
this.a_string = a_string;
|
||||||
|
this.a_int = a_int;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ExampleJSON dataToPersist = null;
|
||||||
|
|
||||||
|
IEnumerator Start()
|
||||||
|
{
|
||||||
|
VELConnectManager.OnInitialState += (state) =>
|
||||||
|
{
|
||||||
|
var s = VELConnectManager.GetUserData("mydata");
|
||||||
|
Debug.Log("Retrieved: " + s);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
dataToPersist = JsonConvert.DeserializeObject<ExampleJSON>(s);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Debug.Log("Error serializing state: " + e.Message);
|
||||||
|
}
|
||||||
|
if(dataToPersist == null)
|
||||||
|
{
|
||||||
|
Debug.Log("Null state, initializing");
|
||||||
|
dataToPersist =new ExampleJSON();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
while (dataToPersist == null) yield return null;
|
||||||
|
|
||||||
|
dataToPersist.a_list.Add(
|
||||||
|
new ExampleChildJSON("" + UnityEngine.Random.Range(0, 10),
|
||||||
|
UnityEngine.Random.Range(0, 10))
|
||||||
|
);
|
||||||
|
Debug.Log(JsonConvert.SerializeObject(dataToPersist));
|
||||||
|
|
||||||
|
}
|
||||||
|
private void OnApplicationQuit()
|
||||||
|
{
|
||||||
|
VELConnectManager.SetUserData("mydata", JsonConvert.SerializeObject(dataToPersist));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue