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:
| Key | English | German | Japanese |
|---|---|---|---|
flags.country | flag_us.png | flag_de.png | flag_jp.png |
voice.greeting | greeting_en.wav | greeting_de.wav | greeting_ja.wav |
splash.screen | splash_en.png | splash_de.png | splash_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:
- Add
LocalizedImagecomponent to a GameObject withImage - Set the Default Sprite (fallback)
- Add locale overrides in the Inspector
- 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 missingLocalizing 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
- Use fallbacks - Always provide default assets
- Consider file size - Large assets for all locales increase build size
- Use Addressables - For large assets, use async loading
- Organize by type - Separate tables for textures, audio, prefabs
- Test all locales - Ensure no missing assets
