Get a list of locales that you've created
Verification environment
- Windows
-
- Windows 11
- Unity Editor
-
- 2021.3.12f1
- Input System (Unity Technologies)
-
- 1.4.4
- Localization (Unity Technologies)
-
- 1.3.2
Prerequisites for this tip
The following settings have been made in advance as a premise for the description of this tip.
Goals of our tips
Displays a list of corresponding locales in the drop-down UI and toggles the display text to match the locale selected by the user.
Prerequisite knowledge and preparation
This tip assumes localization of the following text: First, please refer to the following tips to prepare to display localized text.
Locale change drop-down placement
Provides a drop-down to display a list of locales. You only need to be able to display and select, so make sure that the UI parameters are set appropriately.
Get a list of locales
You can get a list of locales LocalizationSettings.AvailableLocales.Locales
created in .
If you use this and set it to the drop-down at the start of the scene, you are OK.
However, initialization may not have completed when Localization
you start the game, so you may need to wait for it to complete.
First, create a script. DropDownEvent
Leave the name as .
The script looks like this:
using UnityEngine;
using UnityEngine.Localization.Settings;
using UnityEngine.UI;
public class DropDownEvent : MonoBehaviour
{
// 最初のフレームアップデートの前に開始が呼び出されます
async void Start()
{
// Localization の初期化が完了するまで待機します
await LocalizationSettings.InitializationOperation.Task;
// 処理対象の Dropdown コンポーネントを取得します
var dropdownLocale = GetComponent<Dropdown>();
dropdownLocale.options.Clear();
// LocalizationSettings.AvailableLocales.Locales からロケール一覧を取得してオプションを追加します
foreach (var locale in LocalizationSettings.AvailableLocales.Locales)
{
dropdownLocale.options.Add(new Dropdown.OptionData(locale.name));
}
// 初期選択されているロケールのインデックスを取得して選択します
dropdownLocale.value = LocalizationSettings.AvailableLocales.Locales.IndexOf(LocalizationSettings.SelectedLocale);
// ドロップダウンのアイテムが選択されたときにロケールを変更します
dropdownLocale.onValueChanged.AddListener((index) =>
{
LocalizationSettings.SelectedLocale = LocalizationSettings.AvailableLocales.Locales[index];
});
}
}
Start
The method performs processing at the start of the scene, but at this point Localization
, the initialization of may not have been completed.
await LocalizationSettings.InitializationOperation.Task
Wait for initialization to complete.
await
, so Start
please add it to the async
method.
LocalizationSettings.AvailableLocales.Locales
Next, contains a list of locales that you created, so foreach
process as many locales as you want.
Dropdown.OptionData
Set Dropdown
the locale name to and add it to .
To select Dropdown.value
the current locale in the drop-down, search for the target locale in the property and set the index.
This is the end of creating the list, but I also want to include a process that changes the locale when the drop-down item is selected.
onValueChanged
Adds locale switching to the event. This is the same as the previous tip.
Attach the script you created to the dropdown.
Run and verify
Try running the game to check it out.
There is a time when the locale list is not set immediately after the game starts, Localization
because is not initialized.
After a little time, you should be able to see the list of locales registered in the dropdown. If you try switching items, you should be able to see that the content of the text changes.