Developer guide

Text to Speech in Unity: A Practical Integration Guide Fish Audio

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.

2026/10/19

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.

An abstract cube form with a play node and a glass voice ribbon for text to speech in Unity

Choose the path: pre-generated or runtime

PathBest forTrade-off
Pre-generated assetsBarks, combat lines, fixed dialogue, localisation passesNo runtime cost or latency; rebuild when the script changes
Runtime APIPlayer-driven text, procedural names, live events, modsNeeds 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.

What you need

  1. The $99 Studio pack, which includes API access.
  2. A key created under Settings → API Keys in the workspace.
  3. The current request format from the same page — the workspace is the source of truth for the endpoint, fields and limits.

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.

A minimal UnityWebRequest pattern

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.

Caching and repeat lines

A repeated line should never spend credits twice:

  • Hash the text (plus voice and any tags) and keep the resulting AudioClip or bytes in a dictionary for the session.
  • Persist downloaded clips for lines that recur across sessions, subject to your game's save format.
  • Pre-warm the lines a scene will need before the scene starts, so the network call happens during a loading screen rather than mid-dialogue.

Streaming and conversation

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.

What does runtime voice cost?

One credit covers about 1,000 characters:

ContentCharactersApproximate cost
Combat bark600.06 credits
Quest line4000.4 credits
1,000 barks60,00060 credits
15,000 credit Studio pack15,000,000the 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.

Frequently asked questions

Do I need the API to use text to speech in Unity?

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.

Which Unity versions does the pattern work with?

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.

How do I play the returned audio?

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.

Is runtime generation fast enough for dialogue?

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.

What does runtime voice cost in a game?

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.

Text to Speech in Unity: A Practical Integration Guide | Fish Audio