Prerequisites
Unity 6 or newer
Scripts target
.NET Standard 2.1
and useasync/await
.
TextMesh Pro
Required for the sample chat UI.
Newtonsoft.Json
Pulled in via UPM; serializes request/response payloads.
Player2 Client
Download from player2.game.
Step‑by‑Step Setup
1. Add an NpcManager to Your Scene
Insert a NpcManager (singleton) under your scene root. Only one can exist per scene.
2. Configure the NpcManager
Game ID: Name your game or mod.
Functions → +: Define each action the LLM can call:
Name: Identifier (e.g.
flame
)Description: Hint for the model
Arguments: Typed parameters (e.g.
radius: number
), mark required as needed.
In this example, we expose
flame(radius:number)
to spawn a fiery VFX cloud.
3. Attach Player2Npc to Your NPC
Select the GameObject representing your NPC (e.g. Person 1
) and add Player2Npc.cs.
4. Set Up the Player2Npc Component
Npc Manager: Drag in your scene’s
NpcManager
.Short / Full Name: For UI labels.
Character Description: The persona text sent at spawn.
Input Field / Output Message: Link your TextMesh Pro components.
Persistent: Tick if the NPC should survive client restarts.
Now press Play—your NPC is live and ready to chat!
Adding Rich NPC Functions (Optional)
To handle LLM-decided actions:
Create a handler script (e.g., ExampleFunctionHandler.cs
) in your scene root:
using UnityEngine;
public class ExampleFunctionHandler : MonoBehaviour
{
public void HandleFunctionCall(FunctionCall call)
{
if (call.name == "flame")
{
float radius = call.ArgumentAsFloat("radius", defaultValue: 3f);
SpawnFlameCloud(radius);
}
}
void SpawnFlameCloud(float r)
{
// Your VFX / gameplay code here
}
}
In NpcManager → Function Handler:
Click +
Drag the object with
ExampleFunctionHandler
Select ExampleFunctionHandler → HandleFunctionCall
Whenever the model invokes flame
, your handler runs on the main thread, so you never manually intervene.
Under the Hood: Quick Architecture Tour
NpcManager
Scene‑singleton holding configuration and the function catalog.
Serializes to
snake_case
via Newtonsoft.Json.
Player2Npc
Spawns NPCs (
POST /npcs
) and routes UI events toSendChatMessageAsync
.
Player2NpcResponseListener
Consumes
text/event-stream
viaUnityWebRequest
; raises UnityEvents for text and function calls.
ExampleFunctionHandler
Demonstrates reading function arguments and executing gameplay code.
✨ That’s it! With two inspector tweaks and a tiny handler script, you have LLM‑powered, function‑calling NPCs in Unity. Layer on animations, speech synthesis, or your favorite dialogue UI—and ship your game!