Switching Languages
Switching Languages Using the LanguageButton and LanguageDropdown Components
1. LanguageButton Component
The LanguageButton component is a simple, user-friendly solution for switching languages when a button is clicked. Each button is tied to a specific language via the languageID
variable. When the button is clicked, the language is updated in the game.
How to Use the LanguageButton Component
Step 1: Add the LanguageButton component to a Unity Button in your scene.
Step 2: In the Inspector, you will see a field to input the
languageID
. Enter the language code (e.g.,"en"
,"pt-br"
,"es"
) for the language that the button should switch to.Step 3: Now, when the button is clicked, the LanguageManager will switch the language of the game to the corresponding
languageID
.
Here’s an example:
[RequireComponent(typeof(Button))]
public class LanguageButton : MonoBehaviour
{
public string languageID;
private Button button;
private void Awake()
{
button = GetComponent<Button>();
button.onClick.AddListener(OnButtonClicked);
}
private void OnButtonClicked()
{
if (LanguageManager.Instance != null)
{
LanguageManager.Instance.SetLanguage(languageID);
}
else
{
Debug.LogError("LanguageManager instance not found.");
}
}
}
Example Scenario
You could create multiple buttons, one for each language, and assign each a different
languageID
. Clicking the button will instantly change the game’s language.
2. LanguageDropdown Component
The LanguageDropdown component allows players to select a language from a dropdown list. It works with both Unity's built-in Dropdown and TextMeshPro TMP_Dropdown. When a language is selected, the LanguageManager updates the game’s language accordingly.
How to Use the LanguageDropdown Component
Step 1: Add the LanguageDropdown component to a Unity Dropdown or TextMeshPro TMP_Dropdown in your scene.
Step 2: The component automatically populates the dropdown with the languages available in the LanguageManager.
Step 3: When the player selects a language from the dropdown, the language is switched in the game.
Here’s an example:
public class LanguageDropdown : MonoBehaviour
{
private Dropdown dropdown;
private TMP_Dropdown tmpDropdown;
private bool isTMPDropdown = false;
private void Awake()
{
dropdown = GetComponent<Dropdown>();
if (dropdown != null)
{
isTMPDropdown = false;
PopulateDropdown();
dropdown.onValueChanged.AddListener(OnDropdownValueChanged);
}
else
{
tmpDropdown = GetComponent<TMP_Dropdown>();
if (tmpDropdown != null)
{
isTMPDropdown = true;
PopulateDropdown();
tmpDropdown.onValueChanged.AddListener(OnTMPDropdownValueChanged);
}
else
{
Debug.LogError("No Dropdown or TMP_Dropdown component found on this GameObject.");
}
}
}
private void PopulateDropdown()
{
if (LanguageManager.Instance != null)
{
List<string> options = new List<string>();
foreach (Language language in LanguageManager.Instance.Languages)
{
options.Add(language.LanguageTitle);
}
if (isTMPDropdown && tmpDropdown != null)
{
tmpDropdown.ClearOptions();
tmpDropdown.AddOptions(options);
int currentIndex = LanguageManager.Instance.Languages.FindIndex(lang => lang.LanguageID == LanguageManager.Instance.currentLanguageID);
if (currentIndex >= 0)
{
tmpDropdown.value = currentIndex;
}
}
else if (dropdown != null)
{
dropdown.ClearOptions();
dropdown.AddOptions(options);
int currentIndex = LanguageManager.Instance.Languages.FindIndex(lang => lang.LanguageID == LanguageManager.Instance.currentLanguageID);
if (currentIndex >= 0)
{
dropdown.value = currentIndex;
}
}
}
else
{
Debug.LogError("LanguageManager instance not found.");
}
}
private void OnDropdownValueChanged(int index)
{
if (LanguageManager.Instance != null && index >= 0 && index < LanguageManager.Instance.Languages.Count)
{
string selectedLanguageID = LanguageManager.Instance.Languages[index].LanguageID;
LanguageManager.Instance.SetLanguage(selectedLanguageID);
}
else
{
Debug.LogError("Invalid language index selected.");
}
}
private void OnTMPDropdownValueChanged(int index)
{
if (LanguageManager.Instance != null && index >= 0 && index < LanguageManager.Instance.Languages.Count)
{
string selectedLanguageID = LanguageManager.Instance.Languages[index].LanguageID;
LanguageManager.Instance.SetLanguage(selectedLanguageID);
}
else
{
Debug.LogError("Invalid language index selected.");
}
}
}
Example Scenario
Imagine you have a Settings menu where users can select their preferred language. The LanguageDropdown component would be perfect for this scenario. As soon as the user selects a language from the dropdown, the game will update the language accordingly.
Key Points
LanguageButton: Simple buttons that switch the language when clicked. Each button is assigned a language via the
languageID
field.LanguageDropdown: A dropdown menu that allows the player to choose from a list of available languages, automatically populated from the LanguageManager.
Both components rely on the LanguageManager to handle the actual switching of the language and updating of in-game text elements like UI.
Last updated