SetLanguageDropdown.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. namespace I2.Loc
  4. {
  5. [AddComponentMenu("I2/Localization/SetLanguage Dropdown")]
  6. public class SetLanguageDropdown : MonoBehaviour
  7. {
  8. #if UNITY_5_2 || UNITY_5_3 || UNITY_5_4_OR_NEWER
  9. void OnEnable()
  10. {
  11. var dropdown = GetComponent<Dropdown>();
  12. if (dropdown==null)
  13. return;
  14. var currentLanguage = LocalizationManager.CurrentLanguage;
  15. if (LocalizationManager.Sources.Count==0) LocalizationManager.UpdateSources();
  16. var languages = LocalizationManager.GetAllLanguages();
  17. // Fill the dropdown elements
  18. dropdown.ClearOptions();
  19. dropdown.AddOptions( languages );
  20. dropdown.value = languages.IndexOf( currentLanguage );
  21. dropdown.onValueChanged.RemoveListener( OnValueChanged );
  22. dropdown.onValueChanged.AddListener( OnValueChanged );
  23. }
  24. void OnValueChanged( int index )
  25. {
  26. var dropdown = GetComponent<Dropdown>();
  27. if (index<0)
  28. {
  29. index = 0;
  30. dropdown.value = index;
  31. }
  32. LocalizationManager.CurrentLanguage = dropdown.options[index].text;
  33. }
  34. #endif
  35. }
  36. }