Language.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine.Localization.Settings;
  5. using UnityEngine.SceneManagement;
  6. using UnityEngine.UI;
  7. public class Language : MonoBehaviour
  8. {
  9. public Rect windowRect = new Rect(640, 240, 300, 200);
  10. public Color selectColor = Color.yellow;
  11. public Color defaultColor = Color.gray;
  12. Vector2 m_ScrollPos;
  13. Dictionary<UnityEngine.Localization.Locale, string> m_Labels = new Dictionary<UnityEngine.Localization.Locale, string>();
  14. public bool useActiveLocalizationSettings = false;
  15. void Start()
  16. {
  17. var localizationSettings = LocalizationSettings.GetInstanceDontCreateDefault();
  18. // Use included settings if one is available.
  19. if (useActiveLocalizationSettings && localizationSettings != null)
  20. {
  21. Debug.Log("Using included localization data");
  22. return;
  23. }
  24. Debug.Log("Creating default localization data");
  25. // Create our localization settings. If a LocalizationSettings asset has been created and configured in the editor then we can leave this step out.
  26. localizationSettings = ScriptableObject.CreateInstance<LocalizationSettings>();
  27. // Replace the default Locales Provider with something we can manage locally for the example
  28. var localesProvider = new LocalesProvider();
  29. localizationSettings.SetAvailableLocales(localesProvider);
  30. // Add the locales we support
  31. localesProvider.AddLocale(UnityEngine.Localization.Locale.CreateLocale(new UnityEngine.Localization.LocaleIdentifier("zh-CN")));
  32. localesProvider.AddLocale(UnityEngine.Localization.Locale.CreateLocale(new UnityEngine.Localization.LocaleIdentifier("en-US")));
  33. var startupSelectors = localizationSettings.GetStartupLocaleSelectors();
  34. startupSelectors.Clear();
  35. startupSelectors.Add(new SpecificLocaleSelector { LocaleId = "zh-CN" });
  36. // Listen to any Locale changed events
  37. localizationSettings.OnSelectedLocaleChanged += OnSelectedLocaleChanged;
  38. // Make this our new LocalizationSettings
  39. LocalizationSettings.Instance = localizationSettings;
  40. }
  41. static void OnButtonClick()
  42. {
  43. SceneManager.LoadScene("SceneMain");
  44. Debug.Log("OnButtonClick: Change the scene to ");
  45. }
  46. static void OnSelectedLocaleChanged(UnityEngine.Localization.Locale newLocale)
  47. {
  48. Debug.Log("OnSelectedLocaleChanged: The locale just changed to " + newLocale);
  49. }
  50. void OnGUI()
  51. {
  52. windowRect = GUI.Window(GetHashCode(), windowRect, DrawWindowContents, "Select Language");
  53. }
  54. string GetLocaleLabel(UnityEngine.Localization.Locale locale)
  55. {
  56. // Cache our generated labels.
  57. if (m_Labels.TryGetValue(locale, out var label))
  58. return label;
  59. // Create a label which shows the English name and the native name.
  60. var cultureInfo = locale.Identifier.CultureInfo;
  61. // If the Locale is custom then it may not have a CultureInfo
  62. if (cultureInfo != null)
  63. {
  64. // We will show a label in the form "<EnglishName>(<NativeName>)" when the Native name is not the same as the English name.
  65. if (cultureInfo.EnglishName != cultureInfo.NativeName)
  66. label = $"{cultureInfo.EnglishName}({cultureInfo.NativeName})";
  67. else
  68. label = cultureInfo.EnglishName;
  69. }
  70. else
  71. {
  72. label = locale.ToString();
  73. }
  74. m_Labels[locale] = label;
  75. return label;
  76. }
  77. void DrawWindowContents(int id)
  78. {
  79. // We need to wait for the Locales to be ready. If we are using the default LocalesProvider then they may still be loading.
  80. // SelectedLocaleAsync will wait for the Locales to be ready before selecting the SelectedLocale.
  81. if (!LocalizationSettings.SelectedLocaleAsync.IsDone)
  82. {
  83. GUILayout.Label("Initializing Locales: " + LocalizationSettings.SelectedLocaleAsync.PercentComplete);
  84. GUI.DragWindow();
  85. return;
  86. }
  87. // If we wanted to wait for everything to be initialized including preloading tables then we could check LocalizationSettings.InitializationOperation instead.
  88. //if (!LocalizationSettings.InitializationOperation.IsDone)
  89. //{
  90. // GUILayout.Label("Initializing Localization: " + LocalizationSettings.InitializationOperation.PercentComplete);
  91. // GUI.DragWindow();
  92. // return;
  93. //}
  94. var availableLocales = LocalizationSettings.AvailableLocales;
  95. if (availableLocales.Locales.Count == 0)
  96. {
  97. GUILayout.Label("No Locales included in the active Localization Settings.");
  98. }
  99. else
  100. {
  101. // We will use a color to indicate the SelectedLocale
  102. var originalColor = GUI.contentColor;
  103. m_ScrollPos = GUILayout.BeginScrollView(m_ScrollPos);
  104. for (int i = 0; i < availableLocales.Locales.Count; ++i)
  105. {
  106. var locale = availableLocales.Locales[i];
  107. GUI.contentColor = LocalizationSettings.SelectedLocale == locale ? selectColor : defaultColor;
  108. if (GUILayout.Button(GetLocaleLabel(locale)))
  109. {
  110. LocalizationSettings.SelectedLocale = locale;
  111. OnButtonClick();
  112. }
  113. }
  114. GUILayout.EndScrollView();
  115. GUI.contentColor = originalColor;
  116. }
  117. GUI.DragWindow();
  118. }
  119. }