From 2f7b957fc3fa3c1e7968addbd46748710d9b065b Mon Sep 17 00:00:00 2001 From: Scott Doxey Date: Sat, 9 Jan 2021 13:38:39 -0500 Subject: [PATCH 1/6] Added com.unity.nuget.newtonsoft-json package to project. --- Packages/manifest.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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" } } From 63f7de6ac17afc4fa1db36f06fbb945d11bfb238 Mon Sep 17 00:00:00 2001 From: Scott Doxey Date: Sat, 9 Jan 2021 13:51:31 -0500 Subject: [PATCH 2/6] Use TryParse rather than casting. --- .../CandyCoded.Forms/Scripts/FormField.cs | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) 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; + + } } From b08c445a47e2f5e6877869db9cefca8f6092461b Mon Sep 17 00:00:00 2001 From: Scott Doxey Date: Sat, 9 Jan 2021 13:54:28 -0500 Subject: [PATCH 3/6] Added ToJSON and LoadFromJSON methods. --- Assets/Plugins/CandyCoded.Forms/Scripts/Form.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Assets/Plugins/CandyCoded.Forms/Scripts/Form.cs b/Assets/Plugins/CandyCoded.Forms/Scripts/Form.cs index 500a09c..fe3268e 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; @@ -122,6 +123,20 @@ public Dictionary GetFormRawValues() } + public string ToJSON() + { + + return JsonConvert.SerializeObject(GetFormRawValues()); + + } + + public void LoadFromJSON(string json) + { + + LoadFormRawValues(JsonConvert.DeserializeObject>(json)); + + } + public void LoadFormRawValues(Dictionary values) { From 3218e98f1c2e6a0c8ce3e58d6fe70d26840291bd Mon Sep 17 00:00:00 2001 From: Scott Doxey Date: Sat, 9 Jan 2021 13:58:08 -0500 Subject: [PATCH 4/6] Added generic methods. --- Assets/Plugins/CandyCoded.Forms/Scripts/Form.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Assets/Plugins/CandyCoded.Forms/Scripts/Form.cs b/Assets/Plugins/CandyCoded.Forms/Scripts/Form.cs index fe3268e..9a6256b 100644 --- a/Assets/Plugins/CandyCoded.Forms/Scripts/Form.cs +++ b/Assets/Plugins/CandyCoded.Forms/Scripts/Form.cs @@ -130,6 +130,13 @@ public string ToJSON() } + public string ToJSON() where T : class, new() + { + + return JsonConvert.SerializeObject(GetFormValues()); + + } + public void LoadFromJSON(string json) { @@ -137,6 +144,13 @@ public void LoadFromJSON(string json) } + public void LoadFromJSON(string json) + { + + LoadFormValues(JsonConvert.DeserializeObject(json)); + + } + public void LoadFormRawValues(Dictionary values) { From 218bc443c24a440cc8e4e57e584e9e051f73b863 Mon Sep 17 00:00:00 2001 From: Scott Doxey Date: Sat, 9 Jan 2021 14:02:19 -0500 Subject: [PATCH 5/6] Added new event for getting JSON on submit. --- Assets/Plugins/CandyCoded.Forms/Scripts/Form.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Assets/Plugins/CandyCoded.Forms/Scripts/Form.cs b/Assets/Plugins/CandyCoded.Forms/Scripts/Form.cs index 9a6256b..3c20e75 100644 --- a/Assets/Plugins/CandyCoded.Forms/Scripts/Form.cs +++ b/Assets/Plugins/CandyCoded.Forms/Scripts/Form.cs @@ -17,7 +17,9 @@ namespace CandyCoded.Forms public class Form : MonoBehaviour { - public SubmitEvent FormSubmitted; + public SubmitEventObject FormSubmittedObject; + + public SubmitEventJSON FormSubmittedJSON; public Button submitButton; @@ -81,7 +83,9 @@ private void HandleTabPress(Selectable selectable, Selectable[] allSelectable) private void HandleReturnPress() { - FormSubmitted?.Invoke(GetFormRawValues()); + FormSubmittedObject?.Invoke(GetFormRawValues()); + + FormSubmittedJSON?.Invoke(ToJSON()); } @@ -241,7 +245,13 @@ private void OnDisable() } [Serializable] - public class SubmitEvent : UnityEvent> + public class SubmitEventObject : UnityEvent> + { + + } + + [Serializable] + public class SubmitEventJSON : UnityEvent { } From f1bbe9068db007c60834d8a0f4277ea561f7f59e Mon Sep 17 00:00:00 2001 From: Scott Doxey Date: Sat, 9 Jan 2021 14:06:59 -0500 Subject: [PATCH 6/6] Update README.md --- README.md | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) 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); } ```