- Home /
JSON value assignment
Hello,
In my game I have a Json file that store values that I will later on use in my game. This is what I have:
Config.json
{
"SplashDuration" : 3.0,
"LoadingDuration" : 1.0,
"ButtonTriggerFunction" : 0.15
}
JsonConfig.cs
using UnityEngine; using System.IO;
public class JsonConfig : MonoBehaviour
{
string directory = "/JSON/Config.json";
string path = "";
string jsonString = "";
public float splashDuration;
public float loadingDuration;
public float buttonFunctionTrigger;
void Start()
{
if (Application.platform == RuntimePlatform.Android)
{
path = Application.streamingAssetsPath + directory;
WWW reader = new WWW(path);
while (!reader.isDone) { }
jsonString = reader.text;
}
else
{
path = Application.streamingAssetsPath + directory;
jsonString = File.ReadAllText(path);
}
JSONConfig jsonConfig = JsonUtility.FromJson<JSONConfig>(jsonString);
splashDuration = jsonConfig.SplashDuration;
loadingDuration = jsonConfig.LoadingDuration;
buttonFunctionTrigger = jsonConfig.ButtonFunctionTrigger;
}
}
[System.Serializable]
struct JSONConfig
{
public float SplashDuration;
public float LoadingDuration;
public float ButtonFunctionTrigger;
}
Everything is working as it should. My public variables get replaced with the values from the JSONConfig. Here is my question:
Let say I have a lot of values in my Config.json (instead of just 3, maybe 20) can I let the JsonConfig.cs script automatically assign the values without setting each one, one by one.
So the first float in the JSONConfig serializable class would choose the first line in Config.json which is SplashDuration with a value of 3.0.
The second float in the JSONConfig serializable class would choose the second line in Config.json which is Loading duration with a value of 1.0 and so on.
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Parsing multiple json files that are referencing each other 0 Answers