| Layer | Role |
|---|---|
Agent (MonoBehaviour) | Handles Unity-side orchestration: captures input, triggers inference, and dispatches results. |
Provider (ScriptableObject) | Defines the backend — how and where inference happens (Cloud, Local, On-Device). Defines input/output structure. |
| Interface | Purpose |
|---|---|
IChatTask | Defines text or multimodal LLM chat tasks. |
IObjectDetectionTask | Handles object detection (boxes, labels, confidence). |
ISpeechToTextTask | Converts audio input to transcribed text. |
ITextToSpeechTask | Converts text into playable speech audio. |
ChatAsync(),
DetectAsync()) that accepts structured input and returns a strongly typed
result.using System;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
[CreateAssetMenu(menuName = "Meta/AI/Provider Assets/Cloud/My Chat Provider")]
public sealed class MyChatProvider : AIProviderBase, IChatTask
{
[SerializeField] private string apiKey;
[SerializeField] private string endpointUrl = "https://api.example.com/v1/chat";
[SerializeField] private string model = "my-chat-model";
[SerializeField] private bool supportsVision;
protected override InferenceType DefaultSupportedTypes => InferenceType.Cloud;
public bool SupportsVision => supportsVision;
public async Task<ChatResponse> ChatAsync(
ChatRequest req,
IProgress<ChatDelta> stream = null,
CancellationToken ct = default)
{
ValidateConfiguration(apiKey, endpointUrl, model);
var body = new ChatBody { model = model, prompt = req?.text ?? string.Empty };
var json = JsonUtility.ToJson(body);
var raw = await new HttpTransport(apiKey).PostJsonAsync(endpointUrl, json, ct: ct);
var parsed = JsonUtility.FromJson<ChatProviderResponse>(raw);
return new ChatResponse(parsed?.text ?? string.Empty, raw);
}
[Serializable]
private class ChatBody
{
public string model;
public string prompt;
}
[Serializable]
private class ChatProviderResponse
{
public string text;
}
}
PrepareRequestImagesAsync(), and include the prepared image payloads in your provider-specific request body.AIProviderBaseLlamaApiProvider — inherit from
AIProviderBase, which provides:| Responsibility | Description |
|---|---|
Capability Flags | Exposes SupportedInferenceTypes so editor tooling can filter compatible Providers. |
Configuration Checks | Provides ValidateConfiguration() for required API key, endpoint, and model fields. |
Request Helpers | Provides helpers for endpoint normalization, image preparation, and texture encoding. |
Parsing Utilities | Provides shared detection parsing helpers used by object-detection Providers. |
CreateAssetMenu attribute: Meta/AI/Provider Assets/<YourCategory>IChatTask, IObjectDetectionTask,
and so on).HttpTransport handles standard HTTP workflows, but you can extend
it for real-time streaming or gRPC/WebSocket backends.MyWebSocketTransport.cs.HttpTransport.using System.Threading.Tasks;
using Meta.XR.BuildingBlocks.AIBlocks;
using UnityEngine;
public class ContextualLlmHelper : MonoBehaviour
{
[SerializeField] private LlmAgent llmAgent;
public string Context = "You are a helpful assistant.";
public async Task SendContextualPrompt(string userInput)
{
var fullPrompt = Context + "\n" + userInput;
await llmAgent.SendPromptAsync(fullPrompt);
}
}