LocalizedText.cs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using UnityEngine;
  2. using UnityEngine.Localization;
  3. using TMPro;
  4. using UnityEngine.UI;
  5. using UnityEngine.Localization.Tables;
  6. using UnityEngine.Localization.Settings;
  7. public class LocalizedText : MonoBehaviour
  8. {
  9. public LocalizedString localizedString;
  10. private TextMeshProUGUI textComponent;
  11. private Text text;
  12. void OnDestroy()
  13. {
  14. localizedString.StringChanged -= UpdateText;
  15. }
  16. public void changeKey(string key)
  17. {
  18. localizedString.StringChanged -= UpdateText;
  19. localizedString.StringChanged += UpdateText;
  20. localizedString.TableEntryReference = key;
  21. localizedString.RefreshString();
  22. }
  23. void Start()
  24. {
  25. textComponent = GetComponent<TextMeshProUGUI>();
  26. text = GetComponent<Text>();
  27. if(localizedString!=null)
  28. changeKey(localizedString.TableEntryReference);
  29. }
  30. private void UpdateText(string value)
  31. {
  32. if(textComponent)
  33. textComponent.text = value;
  34. if(text)
  35. text.text = value;
  36. }
  37. }