Skip to content

First Inference in Unity

Install the Tryll Unity package, drop a TryllAgentComponent on a GameObject, connect to the running server, send one message, and watch the streamed reply appear in a UI Text element.

Before you start

  • Unity 6 (6000.x) or later, with a blank 3D project.
  • A running Tryll server on 127.0.0.1:9100. See Run the Tryll Server.
  • The server FlatBuffers C# bindings must be generated before opening Unity for the first time. Run from the server/ folder:

    cmake --preset default
    cmake --build --preset debug
    
  • At least one language model available in the server's models.json.

Step 1 — add the package

  1. Open your Unity project's Packages/manifest.json.
  2. Add an entry pointing at the server/client-unity/ folder (adjust the relative path from your Unity project root):

    {
      "dependencies": {
        "com.tryll.client": "file:../../../server/client-unity",
        ...
      }
    }
    
  3. Save the file. Unity will import the package; you should see no compile errors. If you see error CS0246: The type or namespace name 'Message' could not be found, the CMake build step above was skipped — run it and wait for Unity to reimport.

Step 2 — configure the server path (optional)

If you want Unity to launch the server automatically:

  1. Open Edit → Project Settings → Tryll Client.
  2. Enable Auto Launch Server.
  3. Set Editor Server Exe Path to the path of your built tryll_server.exe (e.g. ../server/build/server/Debug/tryll_server.exe).

Alternatively, leave this disabled and start the server manually before entering Play Mode.

Step 3 — create a workflow asset

  1. In the Project window, right-click → Create → Tryll → Workflow Asset.
  2. Name it SimpleWorkflow.
  3. In the Inspector, expand Graph and add one node:

    Field Value
    Nodes[0].Name gen
    Nodes[0].Type Generate
    Nodes[0].Params[0].Key model_name
    Nodes[0].Params[0].Value (name of a model in models.json)
  4. Add one exit route:

    Field Value
    Routes[0].SourceNode gen
    Routes[0].ExitName done
    Routes[0].TargetNode END
  5. Set StartNode to gen.

Alternatively, build the graph in code using TryllGraphBuilder (see Build a chat agent with a graph).

Step 4 — set up the scene

  1. Create a new empty GameObject in the Hierarchy — call it TryllDemo.
  2. Add a Tryll Agent Component (Add Component → Tryll → Tryll Agent Component).
  3. Assign your SimpleWorkflow asset to the Workflow Asset slot.
  4. Enable Auto Create On Connect.
  5. Add a UI → Text (TMP) element to your scene for the output — name it AnswerText.
  6. Create a C# script TryllDemo.cs and attach it to the same GameObject:
using UnityEngine;
using TMPro;
using Tryll.Client;

public class TryllDemo : MonoBehaviour
{
    public TMP_Text AnswerText;

    private TryllAgentComponent _agent;
    private string _accumulated = "";

    void Start()
    {
        _agent = GetComponent<TryllAgentComponent>();

        // Wire up streaming answer.
        _agent.OnAnswerText.AddListener((text, isDelta, isFinal) =>
        {
            if (isDelta)
                _accumulated += text;
            else
                _accumulated = text;

            AnswerText.text = _accumulated;

            if (isFinal)
                Debug.Log("Answer complete");
        });

        _agent.OnTurnComplete.AddListener((status, debugInfo, tokensGenerated) =>
        {
            if (status != TryllTurnStatus.Success)
                Debug.LogError($"Turn failed: {status}");
        });

        _agent.OnError.AddListener(error =>
            Debug.LogError($"Tryll error: {error}"));

        // Connect to the server. TryllAgentComponent will create the agent
        // automatically once connected (AutoCreateOnConnect = true).
        TryllClient.Instance.Connect();
    }

    // Call this from a UI button's OnClick.
    public void SendHello()
    {
        _accumulated = "";
        _agent.SendMessage("Hello, what can you tell me about yourself?");
    }
}
  1. Assign the AnswerText reference in the Inspector.
  2. Add a UI → Button and wire its OnClick to TryllDemo.SendHello.

Step 5 — enter Play Mode

  1. Press Play.
  2. The [TryllClient] singleton GameObject appears in the Hierarchy (created by TryllClientModule).
  3. Click your button. You should see the answer stream in one or more chunks in AnswerText.

If the agent fails to create, check:

  • The server is running and the model name matches exactly.
  • The FlatBuffers bindings were generated (Step 0).
  • The Console for any [Tryll]-prefixed error messages.

What's next?