Created
April 24, 2023 09:06
-
-
Save alisahanyalcin/c9512807d6d418019ca87c8454938110 to your computer and use it in GitHub Desktop.
Unity Localize Dropdown Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Localization; | |
using UnityEngine.Localization.Settings; | |
using TMPro; | |
using UnityEditor; | |
public class LocalizeDropdown : MonoBehaviour | |
{ | |
[SerializeField] private List<LocalizedString> dropdownOptions; | |
private TMP_Dropdown _tmpDropdown; | |
private void Awake() | |
{ | |
if (_tmpDropdown == null) | |
_tmpDropdown = GetComponent<TMP_Dropdown>(); | |
LocalizationSettings.SelectedLocaleChanged += ChangedLocale; | |
} | |
private void ChangedLocale(Locale newLocale) | |
{ | |
List<TMP_Dropdown.OptionData> tmpDropdownOptions = new List<TMP_Dropdown.OptionData>(); | |
foreach (var dropdownOption in dropdownOptions) | |
{ | |
tmpDropdownOptions.Add(new TMP_Dropdown.OptionData(dropdownOption.GetLocalizedString())); | |
} | |
_tmpDropdown.options = tmpDropdownOptions; | |
} | |
} | |
public abstract class AddLocalizeDropdown | |
{ | |
[MenuItem("CONTEXT/TMP_Dropdown/Localize", false, 1)] | |
private static void AddLocalizeComponent() | |
{ | |
// add localize dropdown component to selected gameobject | |
GameObject selected = Selection.activeGameObject; | |
if (selected != null) | |
{ | |
selected.AddComponent<LocalizeDropdown>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How To
Right-click on the Dropdown Component and select the "Localize" option at the bottom
Add localize options