using TMPro; using UnityEngine; using UnityEngine.Localization; using UnityEngine.Localization.Components; using UnityEngine.ResourceManagement.AsyncOperations; namespace XRLib.UI { public class LocalizationHelper : LocalizeStringEvent { protected override void OnEnable() { if (!Application.isPlaying) return; base.OnEnable(); Localization(); } void Localization() { //Debug.Log($"Localization..."); var texts = GetTexts(); foreach (var t in texts) { var localizedString = new LocalizedString() { TableReference = "UI", TableEntryReference = t.text }; AsyncOperationHandle stringOperation = localizedString.GetLocalizedStringAsync(); stringOperation.Completed += (oper) => StringOperation_Completed(oper, t); } } private void StringOperation_Completed(AsyncOperationHandle obj, TextMeshProUGUI textUI) { if (obj.Status != AsyncOperationStatus.Succeeded) { //Debug.Log($"Localization String Operation Error. text={textUI}"); return; } //Debug.Log($"Localizationing Success. text={textUI}"); textUI.SetText(obj.Result); } TextMeshProUGUI[] GetTexts() { return GetComponentsInChildren(); } } }