/* * @Author: 白哉 * @Description: * @Date: 2025年05月26日 星期一 14:05:50 * @Modify: */ using System.Collections.Generic; using System.Text.RegularExpressions; using TMPro; using UnityEngine; namespace Logic.Multilingual { public class MultilingualTextMono : MonoBehaviour { public bool Ban; [HideInInspector] public uint ID = 0; [HideInInspector] public uint FontID = 0; [HideInInspector] public List TextCfg = new List(); private List _paramList; public MultilingualType PreviewType { get => _previewType; set => _previewType = value; } private MultilingualType _previewType = MultilingualType.ZH; public List ParamList { get => _paramList ??= new List(); set => _paramList = value; } private void OnEnable() { if (Ban) return; OnMultilingualChanged(); } public void OnMultilingualChanged() { if (Ban) return; BindFontID(); var text = GetComponent(); if (!text) return; if (ID != 0) text.text = MultilingualManager.Instance.GetMultilingualText(ID); if (FontID != 0) { var font = MultilingualManager.Instance.GetMultilingualFont(FontID); if (font)text.font = font; } if (_paramList != null && _paramList.Count != 0) { int index = 0; text.text = Regex.Replace(text.text, "{param}", m => index < _paramList.Count ? _paramList[index++] : m.Value); } MatchTextCfg(text, MultilingualManager.Instance.CurrentType); } public void BindFontID() { if (Ban) return; if (FontID != 0) return; var text = GetComponent(); if (!text) return; FontID = MultilingualManager.Instance.GetFontGroupID(text.font); } public MultiTextConfig GetMultiTextConfig(MultilingualType type) { foreach (var cfg in TextCfg) { if (cfg.Type != type) continue; return cfg; } return null; } private void MatchTextCfg(TextMeshProUGUI text, MultilingualType type) { foreach (var cfg in TextCfg) { if (cfg.Type != type) continue; cfg.ExcuteConfig(text); } } public void SetMultilingualText(string str, MultilingualType type, TMP_FontAsset font) { var text = GetComponent(); if (!text) return; text.text = str; if (font != null) text.font = font; MatchTextCfg(text, type); } } }