AI NPC Framework

Description

AI NPC Brain is a Unity-focused system that turns NPCs into LLM-driven characters: they chat in character, remember facts about the player, track relationship stats (affinity, trust, fear, respect, flags), and can suggest game actions as structured events. Those suggestions are validated against a whitelist (your event catalog) before anything runs, so the model can’t arbitrarily trigger unknown gameplay.

It ships with pluggable backends (OpenAI, local Ollama, and a mock fallback for offline/UI testing), file-based persistence for conversation and memory, sample UI, demo scene, and editor tools (e.g. animation/catalog setup).

Technical Features

  • LLM-backed NPC dialogue — NPCs reply in character from a persona you define (ScriptableObject + JSON), with OpenAI, local Ollama, or an offline mock for testing.
  • Persistent memory — Stores facts about the player across sessions (file-based), with limits so prompts stay bounded.
  • Relationship model — Tracks affinity, trust, fear, respect, narrative flags, and stage; each turn can apply clamped deltas from the model.
  • Structured, safe game events — The model returns JSON; only catalog-whitelisted events can fire, with typed args, optional allowed values, and a confidence threshold.
  • Conversation history — Rolling turn history in the prompt (configurable depth) plus relevant memories retrieval.
  • World context — worldStateCompact on the brain injects free-form game state into prompts (time, location, inventory snippets, etc.).
  • Unity integration — Central AiNpcBrain component with UnityEvents (OnNpcReply, OnEmotionChanged, OnAnyNpcEventPayload, etc.) for UI, animation, and gameplay hooks.
  • Animation bridge — NpcAnimationMap maps semantic animation IDs to Animator triggers; NpcAnimationReceiver plays them from validated events.
  • UI workflow — Sample TMP chat UIs (simple and scroll-view), optional window IDs and WindowRegistryRuntime for opening shop/quest/relationship panels.
  • Editor tooling — Tools > AI NPC Brain (e.g. NPC Animation Setup) to sync Animator triggers with the animation map and event catalog.
  • Demo content — Demo scene, sample assets, and prefabs so buyers can press Play after wiring an API key or Ollama.
  • Unity 6 + URP oriented — Documented for Unity 6 and URP, with standard dependencies (Input System, Newtonsoft JSON, TextMesh Pro).

Documentation

Editor Tools

Editor Tools

The AI NPC Brain package includes several editor-only tools and custom inspectors that automate common setup tasks. All tools are under the Tools > AI NPC Brain menu.


NPC Animation Setup

Menu: Tools > AI NPC Brain > NPC Animation Setup
Script: Editor/NpcAnimationSetupWindow.cs

The full animation pipeline tool. This is the recommended way to set up NPC animations.

What It Does

  1. Scans an AnimatorController for Trigger parameters
  2. Creates AnimTriggerBinding entries in your NpcAnimationMap asset
  3. Auto-generates semantic animId values (lowercased trigger names)
  4. Suggests usageHint text so the LLM knows when to use each animation
  5. Assigns moodTag categories for filtering and prompt logic
  6. Syncs allowed anim_id values into the NpcEventCatalog
  7. Syncs default move_id values into the catalog

How to Use

  1. Open Tools > AI NPC Brain > NPC Animation Setup.
  2. Assign your three assets:
  3. Animator Controller — the controller with trigger parameters
  4. NPC Animation Map — the map asset to fill
  5. NPC Event Catalog — the catalog to sync allowed values into
  6. Configure options:
  7. Overwrite existing map entries — replaces entries that already exist
  8. Overwrite existing usage hints / mood tags — updates hints even if they were manually edited
  9. Convert animId to lowercaseWave becomes wave (recommended)
  10. Sort animation map entries — alphabetical ordering
  11. Run the steps in order, or click Do Everything to run all three:
  12. Step 1: Scan Animator -> Fill Animation Map — reads triggers, creates bindings
  13. Step 2: Sync anim.trigger Allowed Values -> Event Catalog — copies all animId values into the anim.trigger event's allowed values
  14. Step 3: Sync Default move_id Allowed Values -> Event Catalog — writes default movement IDs into anim.move_to

Default Movement IDs

Step 3 writes these movement IDs into the catalog:

move_id Description
step_forward Small step forward
step_back Small step backward
approach_player Walk to player anchor
leave_player Walk to idle anchor
circle_left Circle left
circle_right Circle right

Usage Hint Auto-Generation

The tool suggests usage hints based on trigger name keywords:

Keyword Suggested Hint
nod "Use when agreeing, confirming, or showing understanding."
shrug "Use when unsure, uncertain, or unable to answer."
wave "Use for greetings, farewells, or friendly acknowledgement."
smile "Use when warm, pleased, affectionate, or encouraging."
laugh "Use when amused, playful, or joking."
point "Use when giving directions or emphasizing something specific."
angry "Use when frustrated, irritated, or upset."
sad "Use when sympathetic, regretful, or disappointed."
bow "Use when respectful, formal, apologetic, or deferential."
idle "Use as a subtle neutral motion when speaking normally."
think "Use when considering something carefully."
talk "Use as a general speaking gesture if no more specific animation fits."

Mood Tag Auto-Assignment

Keyword Mood Tag
nod, smile, laugh Positive
shrug, think Unsure
wave, greet Greeting
point Emphatic
angry, sad Negative
bow Farewell
idle, blink, talk Neutral

Animation Map Auto-Fill

Menu: Tools > AI NPC Brain > Animation Map Auto-Fill
Script: Editor/NpcAnimationMapAutoFillWindow.cs

A simpler version of the NPC Animation Setup tool. Only does Step 1 (scan triggers and fill the animation map). Does not sync to the catalog.

How to Use

  1. Open Tools > AI NPC Brain > Animation Map Auto-Fill.
  2. Assign:
  3. Animator Controller — the controller to scan
  4. NpcAnimationMap — the map to fill
  5. Options:
  6. Overwrite existing entries — replaces entries with the same animator trigger
  7. Use lowercase animId — converts trigger names to lowercase
  8. Click Add Trigger Parameters to Map.

Use Print Trigger List to Console to see what triggers exist on your controller without modifying anything.


Custom Inspector: NpcUiBindings

Script: Editor/NpcUiBindingsEditor.cs

When you select an NpcUiBindings asset, the Inspector shows two utility buttons:

Auto-Generate IDs from npcId

Fills in shopWindowId, questBoardWindowId, and relationshipWindowId from the npcId field using the pattern:

{npcId}.shop
{npcId}.quest_board
{npcId}.relationship

Example: If npcId = npc_aurelia_alchemist, clicking this button sets: - shopWindowId = npc_aurelia_alchemist.shop - questBoardWindowId = npc_aurelia_alchemist.quest_board - relationshipWindowId = npc_aurelia_alchemist.relationship

Sync Into Selected NpcEventCatalog Asset

  1. Select an NpcEventCatalog asset in the Project window.
  2. Click this button on the NpcUiBindings Inspector.
  3. The tool writes the window IDs into the matching event arg allowedValues:
  4. shop.openshop_id allowed = shopWindowId
  5. shop.sell_modeshop_id allowed = shopWindowId
  6. quest.open_boardquest_board_id allowed = questBoardWindowId

This ensures the event catalog's allowed values always match the NPC's actual window IDs.


Custom Inspector: WindowRegistryRuntime

Script: Editor/WindowRegistryRuntimeEditor.cs

When you select a GameObject with WindowRegistryRuntime, the Inspector shows these utility buttons:

Assign Window IDs to 3 Panels from NpcUiBindings

Requires uiBindings, shopPanel, questBoardPanel, and relationshipPanel to be assigned on the component.

  1. Auto-generates IDs on the NpcUiBindings if autoGenerateFromNpcId is enabled.
  2. Adds a WindowPanelId component to each panel (if not already present).
  3. Sets the windowId on each WindowPanelId from the bindings.

Auto-Populate From Scene

Finds every WindowPanelId component in the entire scene and rebuilds the bindings list from them. Each panel's windowId becomes a binding entry.

Auto-Populate ONLY Under searchRoot

Same as above, but only searches under the searchRoot transform (or this GameObject if searchRoot is null). Useful when you have multiple NPC registries in one scene.

Close All (Editor Preview)

Deactivates every panel in the bindings list. Useful for resetting panel states while editing.


NpcCatalogSyncUtility

Script: Editor/NpcCatalogSyncUtility.cs

A static utility class used internally by the NpcUiBindingsEditor. You can also call it from your own editor scripts:

NpcCatalogSyncUtility.Sync(myBindings, myCatalog);

This writes window IDs from NpcUiBindings into the corresponding event arg allowedValues in the NpcEventCatalog.


Typical Setup Workflow

Here is the recommended order of operations when setting up a new NPC:

  1. Create assets:
  2. Persona Definition (set npcId, displayName, personaJson)
  3. Brain Config (set provider, API key, tuning)
  4. Event Catalog (add your events with arg schemas)
  5. NPC Animation Map (leave empty for now)
  6. NPC UI Bindings (set npcId, click Auto-Generate IDs)

  7. Set up animations:

  8. Open Tools > AI NPC Brain > NPC Animation Setup
  9. Assign Animator Controller, Animation Map, and Event Catalog
  10. Click Do Everything

  11. Sync UI bindings to catalog:

  12. Select your NPC UI Bindings asset
  13. Select your Event Catalog asset in the Project window
  14. Click Sync Into Selected NpcEventCatalog Asset

  15. Set up scene panels:

  16. Add WindowPanelId to each panel root, or:
  17. Use the WindowRegistryRuntime Inspector to assign IDs and auto-populate

  18. Wire everything in the scene:

  19. Add AiNpcBrain to the NPC, assign all assets
  20. Add receivers, wire OnAnyNpcEventPayload
  21. Add chat UI, wire to brain
  22. Press Play