Simplify JSON.net persist example

pull/1/head
kjjohnsen 2024-03-07 14:13:15 -05:00 committed by GitHub
parent 5ad3ef7eb6
commit 99c604424f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 12 deletions

View File

@ -139,23 +139,22 @@ public class VelConnectDemo1 : MonoBehaviour
ExampleJSON dataToPersist = null; ExampleJSON dataToPersist = null;
void persist()
{
VELConnectManager.SetUserData("mydata", JsonConvert.SerializeObject(dataToPersist));
}
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
VELConnectManager.AddUserDataListener("mydata", this, (s) => { VELConnectManager.OnInitialState += (state) =>
try { //this will deal with any bad data (or null) {
dataToPersist = JsonConvert.DeserializeObject<ExampleJSON>(s); try
} catch (Exception e) { {
dataToPersist = JsonConvert.DeserializeObject<ExampleJSON>(state.device.TryGetData("mydata"));
}
catch (Exception e)
{
dataToPersist = new ExampleJSON(); dataToPersist = new ExampleJSON();
} }
};
}, true);
StartCoroutine(exampleProcess()); StartCoroutine(exampleProcess());
} }
IEnumerator exampleProcess() IEnumerator exampleProcess()
@ -173,7 +172,9 @@ public class VelConnectDemo1 : MonoBehaviour
} }
private void OnApplicationQuit() private void OnApplicationQuit()
{ {
persist(); VELConnectManager.SetUserData("mydata", JsonConvert.SerializeObject(dataToPersist));
} }
} }
``` ```