- Home /
Binding JSON contents to ui sliders
Context:
I have a json file that stores flat data (no nested values etc.). In a screen the data is loaded and you should be able to see and change that data using ui.Slider instances. When pressing "save" or something the data should be stored and another scene loads using the data from the (saved) json.
Essentially I want:
to set an object's variables/properties based on the value of keys in a loaded json file.
ui.Slider instances to display those values as well as change them
save the values for future reference (this or next run of the application).
I think:
I should use the ui.Slider's OnValueChanged to call a property/field setting function
I should bind the ui.Slider's value somehow to the object's fields/properties
However:
A property cannot be set by JsonUtility.FromJsonOverwrite(). Nor is it serialized to json with JsonUtility.ToJson();
A field cannot be set by the UI.Slider's OnValueChanged in the inspector;
There is no standard way to bind to an object's properties/fields in the ui.Slider's inspector.
Questions:
1. and 2. feel like a catch 22. Is there a way around this? Am I missing something with Serializable?
3. feels like there could/should be. Do I need to write my own binding stuff or am I just missing something?
More general:
If I want to bind a ui element to a property or field, what's the best way to do it? Reflection? Something else? Reflection makes me feel like I'm running around in circles a lot (more or less the first time I look at C# reflection)...
Any tips?
Core of the code I currently have:
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using System;
/// <summary>
/// A model to use for JSON serialisation
/// </summary>
[System.Serializable]
public class RotationModel : AbstractModel
{
public AbstractWriter writer;
public RotationData data;
// the name of the scene to load after data was saved to the store
public string SceneName;
void Start()
{
LoadJsonIntoData ();
}
/// <summary>
/// Let the Writer read(?!) the JSON file and load it into the data struct
/// </summary>
void LoadJsonIntoData ()
{
string json = writer.ReadData ();
data = new RotationData();
JsonUtility.FromJsonOverwrite( json, data );
}
public void SetRotationSpeed( float value )
{
data.RotationSpeed = value;
}
public void SetSize( float value )
{
data.Size = value;
}
/// <summary>
/// Writes the data stored in the Data struct
/// </summary>
public override void WriteData()
{
string json = JsonUtility.ToJson( data );
writer.WriteData( json );
SceneManager.LoadScene( SceneName );
}
}
And (seperate class to prevent serialization issue I experience like: getting id's in the json when using ToJson() and possible mixes between stuff I want in the inspector and the json),
using UnityEngine;
using System.Collections;
public class RotationData
{
public float RotationSpeed;
public float Size;
}
Your answer
Follow this Question
Related Questions
How to Scaling object with new UI slider ? 1 Answer
Fill Slider slowly to a given value. 2 Answers
Slider moving on hover 0 Answers
Balance UI Sliders 1 Answer