LocalizedTMP.cs 823 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using TMPro;
  2. using UnityEngine;
  3. [DisallowMultipleComponent]
  4. [RequireComponent(typeof(TMP_Text))]
  5. public class LocalizedTMP : MonoBehaviour
  6. {
  7. [SerializeField] string key;
  8. TMP_Text label;
  9. public string Key
  10. {
  11. get => key;
  12. set
  13. {
  14. key = value;
  15. Apply();
  16. }
  17. }
  18. void Awake()
  19. {
  20. label = GetComponent<TMP_Text>();
  21. }
  22. void OnEnable()
  23. {
  24. SaveService.EnsureLoaded();
  25. LocaleService.Changed += Apply;
  26. Apply();
  27. }
  28. void OnDisable()
  29. {
  30. LocaleService.Changed -= Apply;
  31. }
  32. public void Apply()
  33. {
  34. if (label == null)
  35. label = GetComponent<TMP_Text>();
  36. if (label == null || string.IsNullOrEmpty(key))
  37. return;
  38. label.text = L.Get(key);
  39. }
  40. }