PlayerPrefLocaleSelector
Stores the last selected locale in PlayerPrefs and restores it on startup.
Read time 1 minuteLast updated 13 days ago
Definition
- Type: Class
- Namespace: Unity.Localization
- Assembly: UnityEngine.LocalizationRuntimeModule
- Implements: IStartupLocaleSelector, IStartupLocaleInitialize
public class PlayerPrefLocaleSelector : IStartupLocaleSelector, IStartupLocaleInitialize
Remarks
On startup this selector reads the locale code saved under PlayerPrefLocaleSelector.PlayerPreferenceKey from PlayerPrefs and selects the matching locale. Because it also implements IStartupLocaleInitialize, it subscribes to LocalizationSettings.SelectedLocaleChanged after initialization and writes the new code whenever the locale changes, so the choice persists across sessions. If no code is stored or no available locale matches, GetStartupLocale returns null and the next selector runs. Add it to LocalizationSettings.StartupSelectors to enable persistence.
Reads the previously saved locale when the game starts.
Examples
using Unity.Localization;namespace Unity.Localization.Samples{ public class PlayerPrefLocaleSelectorExample { public Locale Run() { var selector = new PlayerPrefLocaleSelector { PlayerPreferenceKey = "selected-locale" }; Locale saved = selector.GetStartupLocale(LocalizationSettings.Instance); return saved; } }}
Properties
Property | Description |
|---|---|
| PlayerPreferenceKey | The PlayerPrefs key used to store the selected locale code. |