SetLanguageDropdown.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.UI;
  6. namespace I2.Loc
  7. {
  8. [AddComponentMenu("I2/Localization/SetLanguage Dropdown")]
  9. public class SetLanguageDropdown : MonoBehaviour
  10. {
  11. private void OnEnable()
  12. {
  13. Dropdown component = base.GetComponent<Dropdown>();
  14. if (component == null)
  15. {
  16. return;
  17. }
  18. string currentLanguage = LocalizationManager.CurrentLanguage;
  19. if (LocalizationManager.Sources.Count == 0)
  20. {
  21. LocalizationManager.UpdateSources();
  22. }
  23. List<string> allLanguages = LocalizationManager.GetAllLanguages(true);
  24. component.ClearOptions();
  25. component.AddOptions(allLanguages);
  26. component.value = allLanguages.IndexOf(currentLanguage);
  27. component.onValueChanged.RemoveListener(new UnityAction<int>(this.OnValueChanged));
  28. component.onValueChanged.AddListener(new UnityAction<int>(this.OnValueChanged));
  29. }
  30. private void OnValueChanged(int index)
  31. {
  32. Dropdown component = base.GetComponent<Dropdown>();
  33. if (index < 0)
  34. {
  35. index = 0;
  36. component.value = index;
  37. }
  38. LocalizationManager.CurrentLanguage = component.options[index].text;
  39. }
  40. }
  41. }