Storing levels data
Hello. I am making a mobile game and I would like to implement an easy way to create new levels. I have tried to make it via Json and I've got to this, trying to implement just 2 levels:
{
"Levels": [
{
"locationCode": "Earth",
"enemies": 5,
"bossAvailable": false
},
{
"locationCode": "Earth",
"enemies": 6,
"bossAvailable": false
}
]
}
How could I get that info inside an array or list in Unity?
Would this be the best option to store/create levels quickly?
Are there other and better ways to do this?
Thank you very much.
The best way to interact with json in unity is usually to create a serializable structure that you write and read from a file. This can also work with arrays of ints or serializable structures.
If you generate levels procedurally this is not a bad system. Otherwise, having a scene for each level is better.
Thank you! That documentation seems pretty clear, can't wait to test it :).
Answer by goutham12 · Nov 13, 2019 at 07:12 AM
you can simply do like the below bro.
public class DataManager : MonoBehaviour {
public Level[] allLevels;
}
[Serializable]
public class Level{
public int StarCount;
public bool isLevelLocked;
public int bestMoves;
}
now attach the above script to an empty object and declare your array elemetns (level elemetns) there.
Hello! Thanks for the answer. That was what I first thought but I was trying to create an independent file to store all levels info. I think I will finally do it using scriptable objects :).
Scriptable objects are only readble ,changed values will not save. you can do but you need storage permission. if the values are fixed (i mean you won't change it in runtime) then scriptable object is the best way. but if you want change it in runtime and want save the changed values you can consider the aproach what i mentioned in before answer
like void SaveData(){
string s = JsonUtility.ToJson (yourclassname); PlayerPrefs.SetString ("data", s); }
for loading void loadData(){ yourclassname BigData = JsonUtility.FromJson(PlayerPrefs.GetString("data")); }
Yes I want them to be readonly, just to hold fixed information about levels that will be loaded ingame. Anyway I will take into account your previous answer in case I need it in the future. Thanks!