| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- using TMPro;
- using UnityEngine;
- [DisallowMultipleComponent]
- [RequireComponent(typeof(TMP_Text))]
- public class LocalizedTMP : MonoBehaviour
- {
- [SerializeField] string key;
- TMP_Text label;
- public string Key
- {
- get => key;
- set
- {
- key = value;
- Apply();
- }
- }
- void Awake()
- {
- label = GetComponent<TMP_Text>();
- }
- void OnEnable()
- {
- SaveService.EnsureLoaded();
- LocaleService.Changed += Apply;
- Apply();
- }
- void OnDisable()
- {
- LocaleService.Changed -= Apply;
- }
- public void Apply()
- {
- if (label == null)
- label = GetComponent<TMP_Text>();
- if (label == null || string.IsNullOrEmpty(key))
- return;
- label.text = L.Get(key);
- }
- }
|