CodeBlanks Logo Icon CodeBlanks

Dialogue JSON format

Learn to structure dialogues as JSON.

Structuring Dialogues with JSON

Storing dialogue scripts in external JSON files rather than hard-coding them in C# offers massive flexibility. Writers and localizers can update lines without touching game code.

{
    "nodes": [
        { "id": 1, "speaker": "Guard", "text": "Halt! Who goes there?", "nextId": 2 }
    ]
}

JSON structures are both human-readable and rapidly parsed into in-memory objects by Unity.

Loading JSON Dialogue Data

In Unity, a project JSON file is represented as a TextAsset. Reading it via Resources.Load and passing it to JsonUtility.FromJson instantiates the in-memory object graph.

TextAsset jsonFile = Resources.Load<TextAsset>("Dialogues/npc_01");
DialogueContainer container = JsonUtility.FromJson<DialogueContainer>(jsonFile.text);

Consequently, the corresponding JSON payload is deserialized into memory the moment an NPC is triggered.

Practice Dialogue JSON data simulation

Define a Start method. Inside, declare int nodeSayisi = 3; and print "Yuklenen diyalog dugumu: " + nodeSayisi to the console using Debug.Log.

Starter code

Player.cs
using UnityEngine;

public class DialogueLoader : MonoBehaviour
{
    // Start icinde nodeSayisi tanimla ve yazdir
}

Sample solution

Player.cs
using UnityEngine;

public class DialogueLoader : MonoBehaviour
{
    void Start()
    {
        int nodeSayisi = 3;
        Debug.Log("Yuklenen diyalog dugumu: " + nodeSayisi);
    }
}

Expected output

Yuklenen diyalog dugumu: 3

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the primary benefit of storing dialogue scripts in an external JSON file instead of embedding them in C# code?

  • Disabling the game's physics engine
  • Allowing designers and localizers to edit text without modifying code Correct answer
  • Forcing the game to run on single-core processors only
  • Resetting GPU memory completely

Explanation: External JSON files allow dialogue content and translations to be updated without requiring code recompilation.

2 Load dialogue asset

Question
TextAsset jsonFile = Resources.____<TextAsset>("Dialogues/npc_1");
Answer
TextAsset jsonFile = Resources.Load<TextAsset>("Dialogues/npc_1");

Explanation: The 'Resources.Load' method is used to load an asset from the Resources directory.

3 JSON loading concepts

Term Meaning
TextAsset Unity text asset type
JsonUtility.FromJson Converts JSON text to dialogue object
Resources.Load Loading asset from project resources
nodes dizisi List containing conversation nodes

All lessons in Advanced