I’m using JSON files to ease my level building, with each file representing a different level and now that my game works with minimal bugs, I went to focus a more on the UI side inside unity. One of the things I have to do are little “tutorial” segments for each level, basically squares fixed on the top of the screen with hints,the problem is how can I get the corresponding tutorial from the JSON file and paste it on the unity object?
Relevant scripts if anyone want to check:
[198237-scripts.zip|198237]
I have not used them, but a quick check in the Asset Store showed several JSON packages that might do what you need.
I took a look after seeing your question because I am also considering a way for people to configure levels in a game I am designing. I was going to provide a GUI tool to use in the level design phase, but your question reminded me that JSON could be a good way to save the design in both for the game and the design tool.
public static String LoadFile(string fileLocation)
{
byte[] buffer;
FileStream fs = new FileStream(fileLocation, FileMode.Open, FileAccess.Read);
using (fs)
{
buffer = new byte[fs.Length];
fs.Read(buffer, 0, (int)fs.Length);
}
return JsonUtility.FromJson<String>(buffer);
}
That is not tested code (but it is a pared-down version of what I’m using in my program).
Note that Unity’s JsonUtility doesn’t handle certain data types. I ended up having to use Newtonsoft JSON to save/load my custom class (which includes dictionaries).
Some suggestions on how to break the text up for the tutorial boxes:
I recommend you using ScriptableObject for avoiding complication, unless your using web API and/or JSON on the web. for this purpose just make a very simple class like this:
[System.Serializable]
public class UserTutorial {
public int Id;
public string Message;
}
and now create a list of UserTutorial in you ScriptableObject:
[CreateAssetMenu(menuName("Tutorial"))]
public class TutorialDatabase : ScriptableObject {
public List UserTutorials;
public string GetText(int id) {
retun UserTutorials.First(m => m.Id == id);
}
}
Now you can easily manage your tutorial visually whenever you want