Add generated voice to a Unity game: when to pre-generate, when to call the API at runtime, a UnityWebRequest pattern, caching, and the credit math for game audio. Start from $9.
Unity gives you two honest ways to use text to speech: generate the audio before the build and ship it as assets, or call the API at runtime and play what comes back. Most games should do the first for everything they can predict, and the second only for the lines they cannot.
This guide covers the decision, a minimal UnityWebRequest pattern, caching, and the credit math.

| Path | Best for | Trade-off |
|---|---|---|
| Pre-generated assets | Barks, combat lines, fixed dialogue, localisation passes | No runtime cost or latency; rebuild when the script changes |
| Runtime API | Player-driven text, procedural names, live events, mods | Needs a network call, a key and a credit budget |
A practical split: pre-generate every line that ships with the game, and use runtime generation for text the player creates or the game composes. That keeps the frame budget clean and the credit bill predictable.
Keep one key per environment. A game-staging key and a game-prod key mean a leaked build key can be rotated without touching production.
The example below is deliberately small: it posts a JSON body, downloads the response as an MPEG AudioClip and plays it. Copy the endpoint and field names from your workspace before running it.
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
public class SpeechRequester : MonoBehaviour
{
// Copy the current text to speech endpoint from Settings -> API Keys.
private const string Endpoint = "https://api.example.com/v1/tts";
[SerializeField] private string apiKey = "YOUR_API_KEY";
[SerializeField] private AudioSource source;
public IEnumerator Speak(string text, string voice)
{
var payload = JsonUtility.ToJson(new Request { text = text, voice = voice });
using var request = new UnityWebRequest(Endpoint, "POST");
request.uploadHandler = new UploadHandlerRaw(
System.Text.Encoding.UTF8.GetBytes(payload));
request.downloadHandler = new DownloadHandlerAudioClip(Endpoint, AudioType.MPEG);
request.SetRequestHeader("Content-Type", "application/json");
request.SetRequestHeader("Authorization", "Bearer " + apiKey);
yield return request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success)
{
Debug.LogError(request.error);
yield break;
}
source.clip = DownloadHandlerAudioClip.GetContent(request);
source.Play();
}
[System.Serializable]
private class Request
{
public string text;
public string voice;
}
}Two details matter more than the code. First, never hard-code the endpoint from a blog post — copy it from the workspace, because request formats change. Second, treat the API key as a secret: do not ship a production key in a client build if the build can be inspected; route runtime calls through your own backend when the key must stay private.
A repeated line should never spend credits twice:
For a conversational NPC, the pattern changes: you want the first audio to start playing before the whole paragraph is generated. The platform exposes streaming for this, and the voice agent guide covers the latency budget, turn-taking and where each part of the stack usually becomes the bottleneck.
One credit covers about 1,000 characters:
| Content | Characters | Approximate cost |
|---|---|---|
| Combat bark | 60 | 0.06 credits |
| Quest line | 400 | 0.4 credits |
| 1,000 barks | 60,000 | 60 credits |
| 15,000 credit Studio pack | 15,000,000 | the whole pack |
The Studio pack is the one that includes API access; commercial release rights start with the $29 Creator pack for pre-generated audio. The game voice guide covers casting and batch production, and the API guide covers keys and concurrency.
Only if the game generates audio at runtime. If the lines are known before build, generate them in the workspace and ship the audio files with the game; that needs no API and no runtime cost. Runtime generation needs the API included in the $99 Studio pack.
The UnityWebRequest pattern works across current Unity LTS versions. The example in this guide uses only built-in Unity APIs, so it does not depend on a third-party SDK.
Download the response as an AudioClip with DownloadHandlerAudioClip, assign it to an AudioSource and play it. For longer clips, cache the bytes by a hash of the text so a repeated line does not spend credits twice.
It is fast enough for lines that can be prepared a moment before they play, such as a merchant greeting or a quest line triggered by entering a zone. For instant barks in the middle of combat, pre-generate the clips: no network call belongs in a frame budget.
One credit covers about 1,000 characters. A 60-character bark costs about 0.06 credits; a 400-character quest line costs about 0.4 credits. A 15,000 credit Studio pack covers roughly 250,000 characters of runtime audio.