diff --git a/Assets/Plugins/CandyCoded.Forms/Scripts/Form.cs b/Assets/Plugins/CandyCoded.Forms/Scripts/Form.cs index 500a09c..3c20e75 100644 --- a/Assets/Plugins/CandyCoded.Forms/Scripts/Form.cs +++ b/Assets/Plugins/CandyCoded.Forms/Scripts/Form.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; +using Newtonsoft.Json; using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; @@ -16,7 +17,9 @@ namespace CandyCoded.Forms public class Form : MonoBehaviour { - public SubmitEvent FormSubmitted; + public SubmitEventObject FormSubmittedObject; + + public SubmitEventJSON FormSubmittedJSON; public Button submitButton; @@ -80,7 +83,9 @@ private void HandleTabPress(Selectable selectable, Selectable[] allSelectable) private void HandleReturnPress() { - FormSubmitted?.Invoke(GetFormRawValues()); + FormSubmittedObject?.Invoke(GetFormRawValues()); + + FormSubmittedJSON?.Invoke(ToJSON()); } @@ -122,6 +127,34 @@ public Dictionary GetFormRawValues() } + public string ToJSON() + { + + return JsonConvert.SerializeObject(GetFormRawValues()); + + } + + public string ToJSON() where T : class, new() + { + + return JsonConvert.SerializeObject(GetFormValues()); + + } + + public void LoadFromJSON(string json) + { + + LoadFormRawValues(JsonConvert.DeserializeObject>(json)); + + } + + public void LoadFromJSON(string json) + { + + LoadFormValues(JsonConvert.DeserializeObject(json)); + + } + public void LoadFormRawValues(Dictionary values) { @@ -212,7 +245,13 @@ private void OnDisable() } [Serializable] - public class SubmitEvent : UnityEvent> + public class SubmitEventObject : UnityEvent> + { + + } + + [Serializable] + public class SubmitEventJSON : UnityEvent { } diff --git a/Assets/Plugins/CandyCoded.Forms/Scripts/FormField.cs b/Assets/Plugins/CandyCoded.Forms/Scripts/FormField.cs index 990ca6a..91aa8e8 100644 --- a/Assets/Plugins/CandyCoded.Forms/Scripts/FormField.cs +++ b/Assets/Plugins/CandyCoded.Forms/Scripts/FormField.cs @@ -79,28 +79,43 @@ public object value if (gameObject.TryGetComponent(out var inputField)) { - inputField.text = (string)value; + inputField.text = value.ToString(); } if (gameObject.TryGetComponent(out var toggle)) { - toggle.isOn = (bool)value; + if (bool.TryParse(value.ToString(), out var valueBool)) + { + + toggle.isOn = valueBool; + + } } if (gameObject.TryGetComponent(out var dropdown)) { - dropdown.value = (int)value; + if (int.TryParse(value.ToString(), out var valueInt)) + { + + dropdown.value = valueInt; + + } } if (gameObject.TryGetComponent(out var slider)) { - slider.value = (float)value; + if (float.TryParse(value.ToString(), out var valueFloat)) + { + + slider.value = valueFloat; + + } } diff --git a/Packages/manifest.json b/Packages/manifest.json index 978c37c..48b28ca 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -44,6 +44,7 @@ "com.unity.modules.video": "1.0.0", "com.unity.modules.vr": "1.0.0", "com.unity.modules.wind": "1.0.0", - "com.unity.modules.xr": "1.0.0" + "com.unity.modules.xr": "1.0.0", + "com.unity.nuget.newtonsoft-json": "2.0.0" } } diff --git a/README.md b/README.md index a5ce0eb..c94f55b 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,17 @@ public void Start() } ``` +Data can also be loaded via a JSON `string` object. + +```csharp +public void Start() +{ + + _form.LoadFromJSON(jsonString); + +} +``` + Data can also be loaded via a `Dictionary` object. ```csharp @@ -101,11 +112,22 @@ public void Start() Create a submit event handler that takes `Dictionary` as it's only property. ```csharp -public void SubmitForm(Dictionary formRawValues) +public void SubmitFormObject(Dictionary formRawValues) { + Debug.Log(formRawValues); Debug.Log(JsonConvert.SerializeObject(formRawValues)); - Debug.Log(JsonConvert.SerializeObject(_form.GetFormValues())); + +} +``` + +Or a submit event handler that takes `string` as it's only property. + +```csharp +public void SubmitFormJSON(string json) +{ + + Debug.Log(json); } ```