Skip to content

Commit

Permalink
Merge pull request #10 from CandyCoded/feature/load-from-json
Browse files Browse the repository at this point in the history
[feat] LoadFromJSON
  • Loading branch information
neogeek authored Jan 9, 2021
2 parents 5445cb4 + f1bbe90 commit 49bb5d5
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 10 deletions.
45 changes: 42 additions & 3 deletions Assets/Plugins/CandyCoded.Forms/Scripts/Form.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,7 +17,9 @@ namespace CandyCoded.Forms
public class Form : MonoBehaviour
{

public SubmitEvent FormSubmitted;
public SubmitEventObject FormSubmittedObject;

public SubmitEventJSON FormSubmittedJSON;

public Button submitButton;

Expand Down Expand Up @@ -80,7 +83,9 @@ private void HandleTabPress(Selectable selectable, Selectable[] allSelectable)
private void HandleReturnPress()
{

FormSubmitted?.Invoke(GetFormRawValues());
FormSubmittedObject?.Invoke(GetFormRawValues());

FormSubmittedJSON?.Invoke(ToJSON());

}

Expand Down Expand Up @@ -122,6 +127,34 @@ public Dictionary<string, object> GetFormRawValues()

}

public string ToJSON()
{

return JsonConvert.SerializeObject(GetFormRawValues());

}

public string ToJSON<T>() where T : class, new()
{

return JsonConvert.SerializeObject(GetFormValues<T>());

}

public void LoadFromJSON(string json)
{

LoadFormRawValues(JsonConvert.DeserializeObject<Dictionary<string, object>>(json));

}

public void LoadFromJSON<T>(string json)
{

LoadFormValues(JsonConvert.DeserializeObject<T>(json));

}

public void LoadFormRawValues(Dictionary<string, object> values)
{

Expand Down Expand Up @@ -212,7 +245,13 @@ private void OnDisable()
}

[Serializable]
public class SubmitEvent : UnityEvent<Dictionary<string, object>>
public class SubmitEventObject : UnityEvent<Dictionary<string, object>>
{

}

[Serializable]
public class SubmitEventJSON : UnityEvent<string>
{

}
Expand Down
23 changes: 19 additions & 4 deletions Assets/Plugins/CandyCoded.Forms/Scripts/FormField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,28 +79,43 @@ public object value
if (gameObject.TryGetComponent<InputField>(out var inputField))
{

inputField.text = (string)value;
inputField.text = value.ToString();

}

if (gameObject.TryGetComponent<Toggle>(out var toggle))
{

toggle.isOn = (bool)value;
if (bool.TryParse(value.ToString(), out var valueBool))
{

toggle.isOn = valueBool;

}

}

if (gameObject.TryGetComponent<Dropdown>(out var dropdown))
{

dropdown.value = (int)value;
if (int.TryParse(value.ToString(), out var valueInt))
{

dropdown.value = valueInt;

}

}

if (gameObject.TryGetComponent<Slider>(out var slider))
{

slider.value = (float)value;
if (float.TryParse(value.ToString(), out var valueFloat))
{

slider.value = valueFloat;

}

}

Expand Down
3 changes: 2 additions & 1 deletion Packages/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object>` object.

```csharp
Expand All @@ -101,11 +112,22 @@ public void Start()
Create a submit event handler that takes `Dictionary<string, object>` as it's only property.

```csharp
public void SubmitForm(Dictionary<string, object> formRawValues)
public void SubmitFormObject(Dictionary<string, object> formRawValues)
{

Debug.Log(formRawValues);
Debug.Log(JsonConvert.SerializeObject(formRawValues));
Debug.Log(JsonConvert.SerializeObject(_form.GetFormValues<Profile>()));

}
```

Or a submit event handler that takes `string` as it's only property.

```csharp
public void SubmitFormJSON(string json)
{

Debug.Log(json);

}
```
Expand Down

0 comments on commit 49bb5d5

Please sign in to comment.