Update quick-start.md
Adjusted JSON.net example a bit to make it consistent with other example.pull/1/head
parent
99c604424f
commit
d3c5a582ad
|
|
@ -108,7 +108,7 @@ public class VELConnectTesting : MonoBehaviour
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
JSON.Net Example
|
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
|
```cs
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
@ -139,36 +139,35 @@ public class VelConnectDemo1 : MonoBehaviour
|
||||||
|
|
||||||
ExampleJSON dataToPersist = null;
|
ExampleJSON dataToPersist = null;
|
||||||
|
|
||||||
// Start is called before the first frame update
|
private IEnumerator Start()
|
||||||
void Start()
|
{
|
||||||
{
|
|
||||||
VELConnectManager.OnInitialState += (state) =>
|
VELConnectManager.OnInitialState += (state) =>
|
||||||
{
|
{
|
||||||
|
var s = state.device.TryGetData("mydata");
|
||||||
|
Debug.Log("Retrieved: " + s);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
dataToPersist = JsonConvert.DeserializeObject<ExampleJSON>(state.device.TryGetData("mydata"));
|
dataToPersist = JsonConvert.DeserializeObject<ExampleJSON>(s);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
dataToPersist = new ExampleJSON();
|
Debug.Log("Error serializing state: " + e.Message);
|
||||||
|
}
|
||||||
|
if(dataToPersist == null)
|
||||||
|
{
|
||||||
|
Debug.Log("Null state, initializing");
|
||||||
|
dataToPersist =new ExampleJSON();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
StartCoroutine(exampleProcess());
|
while (dataToPersist == null) yield return null;
|
||||||
}
|
|
||||||
|
|
||||||
IEnumerator exampleProcess()
|
|
||||||
{
|
|
||||||
while(dataToPersist == null)
|
|
||||||
{
|
|
||||||
yield return null; //wait for persistent data
|
|
||||||
}
|
|
||||||
|
|
||||||
dataToPersist.a_list.Add(
|
dataToPersist.a_list.Add(
|
||||||
new ExampleChildJSON("" + UnityEngine.Random.Range(0, 10),
|
new ExampleChildJSON("" + UnityEngine.Random.Range(0, 10),
|
||||||
UnityEngine.Random.Range(0, 10))
|
UnityEngine.Random.Range(0, 10))
|
||||||
);
|
);
|
||||||
Debug.Log(JsonConvert.SerializeObject(dataToPersist));
|
Debug.Log(JsonConvert.SerializeObject(dataToPersist));
|
||||||
|
|
||||||
}
|
}
|
||||||
private void OnApplicationQuit()
|
private void OnApplicationQuit()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue