Version: Unity 6.7 Alpha (6000.7)
LanguageEnglish
  • C#

LocalizationSettings.DeferInitialization

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public bool DeferInitialization;

Description

Whether the system waits for an explicit call to LocalizationSettings.InitializeAsync before it initializes.

Off by default, so the first value resolved starts initialization. Turn it on, through Project Settings > Localization, when the startup locale depends on something that loads first, such as a save file or a platform sign-in. Call LocalizationSettings.InitializeAsync once the data is ready; it lifts the block for the rest of the session. While it is on, an awaited read simply waits and resolves once initialization runs. A synchronous read cannot wait, so it throws instead, and the stack trace names what asked too early. Nothing starts the run but you, which is the point: the alternative is loading the wrong language and reloading every table. Note that an awaited read never completes if initialization is never asked for. This applies to players and to Play Mode only. In the Editor outside Play Mode the system still initializes on demand, so the table windows and inspectors keep working with no game running to ask for a value.

Additional resources: LocalizationSettings.InitializeAsync, LocalizationSettings.IsInitialized

<para>Initialize localization only once a save file has loaded.</para>

using Unity.Localization;
using UnityEngine;

namespace Unity.Localization.Samples { // Requires Defer Initialization in Project Settings > Localization, so nothing resolves a value before this runs. public class DeferInitializationExample : MonoBehaviour { async void Start() { var saved = await LoadSaveFileAsync();

var settings = LocalizationSettings.Instance; if (settings != null && !string.IsNullOrEmpty(saved)) settings.StartupSelectors.Insert(0, new SpecificLocaleSelector { LocaleId = saved });

await LocalizationSettings.InitializeAsync();

Debug.Log($"Started in {LocalizationSettings.SelectedLocale?.LocaleName}"); }

static async Awaitable<string> LoadSaveFileAsync() { await Awaitable.NextFrameAsync(); return "de"; } } }