Skip to content

๐Ÿ“ 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:

  1. Create a new MonoBehaviour script.
  2. Add using UnityNeuroSpeech.Runtime; at the top.
  3. 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!