using System; using TMPro; using UnityEngine; using UnityEngine.UI; namespace OctoCore.Localization { public class OctoLocalizedText : MonoBehaviour { [SerializeField] OctoLocalizedTextData data; public OctoLocalizedTextData Data { get => data; set => data = value; } [SerializeField] Text text; [SerializeField] TextMeshPro textMeshPro; [SerializeField] TextMeshProUGUI textMeshProUGUI; Action localize; #if UNITY_EDITOR void OnValidate() { if (Application.isPlaying) return; bool setDirty = false; if (!text && !textMeshPro && !textMeshProUGUI) { text = GetComponent(); textMeshPro = GetComponent(); textMeshProUGUI = GetComponent(); if (text || textMeshPro || textMeshProUGUI) setDirty = true; } if (text && text.transform != transform) { text = null; setDirty = true; } if (textMeshPro && textMeshPro.transform != transform) { textMeshPro = null; setDirty = true; } if (textMeshProUGUI && textMeshProUGUI.transform != transform) { textMeshProUGUI = null; setDirty = true; } if (setDirty) UnityEditor.EditorUtility.SetDirty(this); Localize(); } #endif void Awake() { if (!OctoCore.Instance) { Debug.LogWarning($"[OctoLocalizedText] OctoCore.Instance not found"); return; } Localize(); OctoCore.Instance.Localization.SubscribeOnLanguageWrittenChange(localize = Localize); if (OctoCore.Instance.Development.Log && data) Debug.Log($"{name} subscribed on language written change"); } void OnDestroy() { if (!OctoCore.Instance) return; if (localize != null) { OctoCore.Instance.Localization.UnsubscribeOnLanguageWrittenChange(localize); localize = null; if (OctoCore.Instance.Development.Log && data) Debug.Log($"{name} unsubscribed on language written change"); } } public void Localize() { if (!data) return; string localizedText = OctoLocalization.GetLocalizedText(data); if (text) { text.text = localizedText; } else if (textMeshPro) { textMeshPro.text = localizedText; } else if (textMeshProUGUI) { textMeshProUGUI.text = localizedText; //LayoutRebuilder.ForceRebuildLayoutImmediate(textMeshProUGUI.rectTransform); } } } }