๐ Agent API
Fully control and monitor your agents with a clean and lightweight API.
๐น๏ธ Handle Agent State
The Agent API is simple and elegant โ just 6 methods and 2 classes.
To use the UnityNeuroSpeech Agent API
, you need to:
- Create a new
MonoBehaviour
script. - Add
using UnityNeuroSpeech.Runtime;
at the top. - Derive your class from
AgentBehaviour
.
Once you do that, you must implement four abstract methods:
Awake
BeforeTTS
AfterTTS
AfterSTT
Youโll also need a reference to your YourAgentNameController
type (in this example, AlexController
).
Your script should look like this:
using UnityEngine;
using UnityNeuroSpeech.Runtime;
public class AlexBehaviour : AgentBehaviour
{
[SerializeField] private AlexController _alex;
public override void Awake() {}
public override void AfterTTS() {}
public override void BeforeTTS(int responseCount, string agentMessage, string emotion, string action) {}
public override void AfterSTT(string playerMessage) {}
}
๐ Methods Overview
- AfterTTS โ Called after the audio playback finishes.
- BeforeTTS โ Called before sending text to the TTS model.
- AfterSTT โ Called after the STT model finishes transcribing microphone input.
- Awake โ Works like
MonoBehaviour.Awake()
, but required.
Use it to link your behaviour to an agent:
public override void Awake()
{
AgentManager.SetBehaviourToAgent(_alex, this);
AgentManager.SetJsonDialogHistory(_alex, "AlexDialogHistory");
// or, if you use encryption:
AgentManager.SetJsonDialogHistory(_alex, "AlexDialogHistory", "yBIWJczdP7aSbSxB");
}
๐ก What Is SetBehaviourToAgent()
?
The SetBehaviourToAgent()
method connects your AgentBehaviour
to the agentโs internal event hooks:
[HideInInspector] public Action<int, string, string, string> BeforeTTS { get; set; }
[HideInInspector] public Action AfterTTS { get; set; }
[HideInInspector] public Action AfterSTT { get; set; }
This ensures UnityNeuroSpeech calls your methods at the correct moments.
๐ก What Is SetJsonDialogHistory()
?
If you use SetJsonDialogHistory()
, all dialog data between the player and the LLM will be stored in a .json
file inside StreamingAssets/
, one file per agent.
The second parameter is the file name (without the .json
extension).
The optional third parameter is a 16-character AES encryption key.
If encryption is enabled (highly recommended), the player wonโt be able to view dialog history in builds since itโs encrypted.
To decrypt it in the Editor, see the โUseful Toolsโ section in the docs.
โ
Donโt forget to attach your behaviour script to a GameObject
in the scene.
๐ You now have full control over your agents!