15 · Advanced Applications in C# and Unity | Generative AI for Game Development

This chapter companion builds the Emberlight Tavern, a modular Unity scene with three NPCs and a mood meter. It shows how JSON trees, four C# scripts and a single use-AI flag blend scripted and AI-generated dialogue.

4:31 video3 min readWatch on YouTube

Adding AI to a Unity project sounds like it means rewriting the dialogue system, but this video shows it does not. A companion to chapter 15 of Generative AI for Game Development by Shesh Narayan Gupta, it builds the Emberlight Tavern, a modular scene where modular scripts and JSON dialogue trees let AI plug in through a single flag.

Terms and the tavern scene

The video defines six terms. A dialogue tree is a set of nodes holding lines and choices. The NPC controller binds a character to its JSON file. The dialogue manager loads and runs the trees. The emotion engine reads tone from keywords. The AI response handler prompts the model and returns text. The mood meter is the UI element showing inferred tone.

The Emberlight Tavern is a candlelit medieval room with three characters: a stoic, factual bartender, a sarcastic and evasive rogue, and an empathic, reactive minstrel. A mood meter reads the tone of each player choice.

A modular project

The project uses four C# scripts, JSON dialogue files and prefabs, each with one job. The video compresses seven setup steps into one motion: scene, player, tavern, UI, NPC and dialogue system. The pipeline runs from the NPC controller to the dialogue manager, which reads a node. AI nodes go to the response handler, everything passes through the emotion engine, and the Unity UI displays the result.

JSON as the source of truth

Each JSON node holds a speaker, a line, choices and a use AI flag. A new NPC is simply a new JSON file, with no code changes. The flag is the whole trick: scripted nodes play exactly as written and AI nodes generate their text. Mixed together, the chapter says, players cannot tell where the writing ends.

How the scripts divide the work

  • The dialogue manager loads trees from JSON, displays each node, spawns the choice buttons and hooks every line into the emotion engine.
  • The emotion engine looks for keywords for angry, sad, happy, fearful and neutral tones and colors the mood meter.
  • The AI response handler builds the prompt and calls a local Llama model running on the player's own machine, capped at about 35 tokens so it returns one short sentence. There is no cloud and no bill.
  • The NPC controller binds a name to a dialogue file, such as the bartender and the bartender JSON.

The rogue walk-through

Pressing E, the player meets the rogue, who greets them with a static line asking whether they are brave or foolish. Two choices follow. Choosing confrontation reaches a use-AI node, so the handler builds a prompt describing a sarcastic rogue with dry wit and misdirection, and the model answers in character. The player's tone shifts from hostile to curious, and a second AI reply follows. No two players hear the same tavern.

Prefabs make the pieces reusable: choice buttons, the mood meter and the interact prompt are built once and stamped everywhere. The meter shows red for anger, blue for sorrow and green for joy.

An honest limit

Keyword lists are brittle. In the video's demonstration, words like hostile and kill read as neutral. The chapter's advice is to extend the lists or graduate to real sentiment analysis.

Key takeaways

  • Six boxes form the pipeline, and JSON trees are the source of truth.
  • A use-AI flag lets scripted and generated dialogue coexist.
  • Four C# scripts split the work: dialogue, emotion, AI and character binding.
  • A local model keeps the system free of cloud costs.
  • Prefabs keep the scene reusable.
  • Keyword emotion detection is simple but limited.

Try it yourself

The video ends with a prompt to paste into Claude. Describe one NPC and ask for a JSON dialogue node with two player choices, one scripted and one use-AI, along with the prompt the handler would send and six mood keywords covering at least two emotions. Then check that the prompt constrains the character's voice, and that your keyword list catches the words players really type.

Full transcript(auto-generated, with timestamps)

[0:00]Generative AI for game development, advanced applications in C# and Unity. Build the Emberlight Tavern, Unity, C#, and AI-driven NPCs in one modular scene. Hello, this is Liam in for Bear. Unity plus AI sounds like rewriting your whole dialogue system. It isn't. Modular scripts and JSON trees, the AI plugs in through one flag. Six terms, then we're off. Dialogue tree, nodes of lines and choices. NPC controller binds a character to its JSON. Dialogue manager loads and runs the trees. Emotion engine reads tone from keywords. AI response handler prompts the model, returns text. Mood meter, the UI showing inferred tone. The Emberlight Tavern, a candlelit medieval room with three souls in it.

[0:43]The bartender, stoic and factual. The rogue, sarcastic and evasive. The minstrel, empathic and reactive. Plus a mood meter reading the tone of your every choice. The project is modular from the start. Four C# scripts, JSON dialogue files, and prefabs, each with one job. Seven setup steps, scene, player, tavern, UI, NPC, dialogue system, compressed into one motion. Build it, wire it, press play. The pipeline exactly as the chapter draws it. The NPC controller hands off to the dialogue manager, which reads a node. AI nodes go to the response handler. Everything passes the emotion engine, and the Unity UI shows it all. JSON is the source of truth. Each node

[1:24]Holds a speaker, a line, choices, and a use AI flag. A new NPC is a new JSON file, no code changes. The use AI flag is the whole trick. Scripted nodes play exactly as written. AI nodes generate. Mix them, the chapter says, and players can't tell where the writing ends. The dialogue manager orchestrates. It loads the trees from JSON, displays each node, spawns the choice buttons, and hooks every line into the emotion engine. The emotion engine reads tone from keywords, angry, sad, happy, fearful, neutral, and colors the mood meter. The chapter's exact logic run live. The AI response handler builds the prompt and calls a local model, llama, running on

[2:05]Your own machine, capped at about 35 tokens. One short sentence. No cloud, no bill. And the NPC controller binds it all, a name and a dialogue file. Bartender plus bartender JSON, the character and their words linked. Press E, the rogue greets you with a static line, "Well, well, someone brave or foolish. Which are you?" Two choices appear beneath. Choose confrontation and you hit a use AI node. The handler builds the prompt, sarcastic rogue, dry wit, misdirection, and the AI fires back. Then why start with a riddle? "I have secrets you'd kill to know." The tone shifts from hostile to curious, and a second AI reply lands in character in

[2:44]The moment. No two players hear the same tavern. Prefabs make it reusable. Choice buttons, the mood meter, the interact prompt built once, stamped everywhere. And the meter colors with the mood. Red for anger, blue for sorrow, green for joy. The honest limit keyword lists are brittle. Hostile reads neutral, kill reads neutral. Extend the lists, the chapter says, or graduate to real sentiment analysis. Generative AI, emotional analysis, modular architecture, one tavern, three souls, and a narrative system that feels alive. Let's recap with Claude, the Emberlight tavern, three NPCs, one mood meter, and a modular Unity scene. Six boxes form the pipeline, JSON trees are the source

[3:24]Of truth. AI plugs in through one flag. Four C# scripts split the work, dialogue, emotion, AI, and character binding. The rogue walk-through proves it. Static greetings, AI wit, shifting tone. Prefabs keep it reusable. The keyword engine's limits keep it honest. Generative AI plus modular architecture, narrative systems that feel alive. Your turn. Paste this into Claude. Help me design one NPC for a Unity dialogue system like the Emberlight tavern. I will describe the character. You write a JSON dialogue node with speaker text and two player choices. One choice leading to a scripted node, one to use AI node. Write the prompt the AI response handler would send for the AI node and list SIX

[4:04]Mood keywords covering at least two emotions for the emotion engine. Then test me, give me a player line and ask what mood the keyword engine detects. Then check two things yourself. Does the AI prompt constrain the character's voice, not just the topic? And would your keyword list actually catch the words your players type? If hostile reads as neutral, extend the lists before you ship. Advanced applications in C# and Unity at Nick Bear Brown.

More from Generative AI for Game Development — Shesh Narayan Gupta

Humanitarians AI Lyrical Literacy Project