How to get a string from a JSON and put it on a Text component?

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:

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.

Hope that is useful for you.

What I think you want is to be able to pull text from a JSON file and stick it on those UI elements?

So you’d probably want something like a text box on those elements, that you will modify with a String by calling its .text.

You’d want to be able to pull the JSON data from a file, convert that to a String, then stick that String on the appropriate text box.

It is easy to load JSON as a string. Your issue will be how to break up the string and place it in the appropriate text boxes.

Check out Unity - Scripting API: JsonUtility.FromJson


    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’m pretty new to coding and not too familiar with everything that can be done w/ JSON.

Hope this gives you some ideas, at least.

There are also some good tutorials on Youtube for using JSON to save/load/populate lists in Unity.

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:

public class UserTutorial {
   public int Id;
   public string Message;

and now create a list of UserTutorial in you ScriptableObject:

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

Good luck @Volfaer