Get localized text and assets at any time
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
When you click the button, the target text from the localization table is displayed on the loading screen.
Prerequisite knowledge and preparation
This tip assumes localization of the following text: However, since we will not implement all of them, we will post the procedure from 1 here, but we will simplify the duplicate content.
Installing the Localization Package
The procedure is the same here.
Creating a Localization
This is the same as before, so I will post only the image.
Here, we create a "Localization" folder in the "Scenes" folder and create it in it.
Creating a Locale
This is also the same as before, so I will omit the detailed procedure. The locales to add are "Japanese (ja)", "English (en)", and "Spanish (es)".
I specified the "Localization" folder mentioned earlier.
Leave the default locale as "Japanese (ja)".
Creating Texts
Leave the table name TextTable
as .
Localization
Create a folder inside TextTable
the folder and specify it.
Create a text. This time, the purpose is to get it programmatically, so you can register one for the time being.
UI Placement
I would like to place the text and the button, and when I click the button, I want to display the text of the current locale.
Creating and Attaching Scripts
Create a script. ButtonEvent
Leave the name as .
using UnityEngine;
using UnityEngine.Localization.Settings;
using UnityEngine.UI;
public class ButtonEvent : MonoBehaviour
{
[SerializeField] private Text Text;
public void OnClick()
{
// Localization から指定したテーブル名とキーからエントリーを取得します
var entry = LocalizationSettings.StringDatabase.GetTableEntry("TextTable", "Hello").Entry;
Text.text = entry.Value;
}
}
Enter what happens when the button is clicked.
All we do here is one LocalizationSettings.StringDatabase.GetTableEntry
thing: you can get the entry of the found set by specifying the table name and key created in Localization in the method.
This time we are doing it with text, but assets can also LocalizationSettings.AssetDatabase
be obtained by using .
Once you have the entry, all you have to do is get the value you have. The contents are the text of the selected locale. This time it is obtained by synchronous processing, but you can also use asynchronous methods.
Attach the script to the EventSystem. Set the text as well.
Assign click handling to the button.
Run and verify
Try running it after you finish setting it up. Since no localization events are set in the UI, the text does not change when executed.
Click the button to switch to the Japanese text. This is because the current locale of the Japanese is selected.
Try changing the locale in the upper right corner of the screen and then clicking the button. It should change to the text for the target locale.