Skip to content

Localized Assets

This example shows how to localize images, audio, and other assets per locale.

Asset Table Setup

Create an Asset Table with entries for each asset:

KeyEnglishGermanJapanese
flags.countryflag_us.pngflag_de.pngflag_jp.png
voice.greetinggreeting_en.wavgreeting_de.wavgreeting_ja.wav
splash.screensplash_en.pngsplash_de.pngsplash_ja.png

Basic Usage

csharp
using Lexis;
using UnityEngine;
using UnityEngine.UI;

public class LocalizedMediaExample : MonoBehaviour
{
    [SerializeField] private Image flagImage;
    [SerializeField] private AudioSource audioSource;

    void Start()
    {
        Localization.Initialize();
        UpdateMedia();
        Localization.OnLocaleChanged += _ => UpdateMedia();
    }

    void UpdateMedia()
    {
        // Get localized sprite
        if (Localization.TryGetAsset<Sprite>("flags.country", out Sprite flag))
        {
            flagImage.sprite = flag;
        }

        // Get localized audio
        if (Localization.TryGetAsset<AudioClip>("voice.greeting", out AudioClip clip))
        {
            audioSource.clip = clip;
            audioSource.Play();
        }
    }
}

Using LocalizedImage Component

Zero-code approach for sprite localization:

  1. Add LocalizedImage component to a GameObject with Image
  2. Set the Default Sprite (fallback)
  3. Add locale overrides in the Inspector
  4. The component updates automatically
csharp
// The component handles everything:
// - Loading correct sprite for current locale
// - Updating when locale changes
// - Falling back to default when locale-specific is missing

Localizing Textures

csharp
using Lexis;
using UnityEngine;

public class LocalizedTexture : MonoBehaviour
{
    [SerializeField] private Renderer targetRenderer;
    [SerializeField] private string materialPropertyName = "_MainTex";

    void Start()
    {
        Localization.Initialize();
        UpdateTexture();
        Localization.OnLocaleChanged += _ => UpdateTexture();
    }

    void UpdateTexture()
    {
        var texture = Localization.GetAsset<Texture2D>("textures", "splash");
        if (texture != null)
        {
            targetRenderer.material.SetTexture(materialPropertyName, texture);
        }
    }
}

Localizing Audio

Voiceover System

csharp
using Lexis;
using UnityEngine;

public class VoiceoverManager : MonoBehaviour
{
    [SerializeField] private AudioSource voiceSource;

    public void PlayVoiceover(string key)
    {
        if (Localization.TryGetAsset<AudioClip>("voiceover", key, out AudioClip clip))
        {
            voiceSource.clip = clip;
            voiceSource.Play();
        }
    }

    public void PlayGreeting() => PlayVoiceover("greeting");
    public void PlayTutorialIntro() => PlayVoiceover("tutorial.intro");
}

With Subtitles

csharp
using Lexis;
using UnityEngine;

public class SubtitledVoiceover : MonoBehaviour
{
    [SerializeField] private AudioSource voiceSource;
    [SerializeField] private Text subtitleText;

    public void PlayWithSubtitles(string voiceKey, string subtitleKey)
    {
        // Play localized audio
        var clip = Localization.GetAsset<AudioClip>("voice", voiceKey);
        voiceSource.clip = clip;
        voiceSource.Play();

        // Show localized subtitle
        subtitleText.text = Localization.Get(subtitleKey);

        // Hide subtitle when audio finishes
        Invoke(nameof(HideSubtitle), clip.length);
    }

    void HideSubtitle() => subtitleText.text = "";
}

Localizing Prefabs

csharp
using Lexis;
using UnityEngine;

public class LocalizedPrefabSpawner : MonoBehaviour
{
    [SerializeField] private Transform spawnPoint;

    public void SpawnLocalizedPrefab(string key)
    {
        var prefab = Localization.GetAsset<GameObject>("prefabs", key);
        if (prefab != null)
        {
            Instantiate(prefab, spawnPoint.position, spawnPoint.rotation);
        }
    }
}

Mixed Example

csharp
using Lexis;
using UnityEngine;
using UnityEngine.UI;

public class SplashScreen : MonoBehaviour
{
    [SerializeField] private Image backgroundImage;
    [SerializeField] private Text welcomeText;
    [SerializeField] private AudioSource musicSource;

    void Start()
    {
        Localization.Initialize();
        LoadLocalizedContent();
    }

    void LoadLocalizedContent()
    {
        // Localized background image
        var background = Localization.GetAsset<Sprite>("splash.background");
        if (background != null)
            backgroundImage.sprite = background;

        // Localized text
        welcomeText.text = Localization.Get("splash.welcome");

        // Localized music (cultural appropriateness)
        var music = Localization.GetAsset<AudioClip>("music.splash");
        if (music != null)
        {
            musicSource.clip = music;
            musicSource.Play();
        }
    }
}

Best Practices

  1. Use fallbacks - Always provide default assets
  2. Consider file size - Large assets for all locales increase build size
  3. Use Addressables - For large assets, use async loading
  4. Organize by type - Separate tables for textures, audio, prefabs
  5. Test all locales - Ensure no missing assets

Professional Unity Development Tools