Game Development
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.
Exercise 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.
Show example solution
using UnityEngine;
public class DialogueLoader : MonoBehaviour
{
void Start()
{
int nodeSayisi = 3;
Debug.Log("Yuklenen diyalog dugumu: " + nodeSayisi);
}
}
Test yourself
-
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
- Forcing the game to run on single-core processors only
- Resetting GPU memory completely
Show answer
Allowing designers and localizers to edit text without modifying code — External JSON files allow dialogue content and translations to be updated without requiring code recompilation.
-
Load dialogue asset
TextAsset jsonFile = Resources.____<TextAsset>("Dialogues/npc_1");
Show answer
TextAsset jsonFile = Resources.Load<TextAsset>("Dialogues/npc_1");
-
JSON loading concepts
- 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
Next lesson: Dialogue runner