using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using XRTool.Util;
public class MultTextHelper : MonoBehaviour
{
public bool isOnStart;
public bool isOnEnd;
public string startReg;
public string endReg = " : ";
public bool isSetDef = false;
[HideInInspector]
public bool isClearOnNothing = true;
private TextMeshProUGUI label;
///
/// 语言编号
///
[HideInInspector]
[SerializeField]
private string languageNum;
private bool isAutoChange = true;
public TextMeshProUGUI Label
{
get
{
if (!label)
{
label = GetComponent();
if (IsAutoChange)
{
LanguageMgr.Instance.changeLanuage += OnChangeLaunge;
}
//if (LanguageMgr.Instance.CurLanguage != null)
//{
// OnChangeLaunge(LanguageMgr.Instance.CurLanguage);
//}
}
return label;
}
}
public string LanguageNum { get => languageNum; set => SetLanuageNum(value.ToString()); }
public bool IsAutoChange
{
get => isAutoChange; set
{
isAutoChange = value;
if (!value)
{
LanguageMgr.Instance.changeLanuage -= OnChangeLaunge;
}
}
}
private void OnDestroy()
{
if (LanguageMgr.Instance != null)
{
LanguageMgr.Instance.changeLanuage -= OnChangeLaunge;
}
}
///
/// 监听到语言变化时
///
///
private void OnChangeLaunge(LanguagePackConf conf)
{
if (conf != null)
{
//UnityLog.Log(conf.Language);
LanguageConf lc = LanguageMgr.Instance.GetMessage(LanguageNum.ToString());
if (lc != null)
{
string msg = lc.Message;
if (isOnStart || isOnEnd)
{
if (isOnStart)
{
msg = startReg + msg;
}
else if (isOnEnd)
{
msg += endReg;
}
}
SetRealMsg(msg);
}
else
{
SetRealMsg("null:" + LanguageNum.ToString());
if (isClearOnNothing)
{
SetRealMsg("");
}
//UnityLog.LogError("Exception:语言编号不存在:" + UnityUtil.GetGameObjectPath(transform));
}
}
else
{
// UnityLog.LogError("Exception:语言包为空" + UnityUtil.GetGameObjectPath(transform));
}
}
private void Awake()
{
if (isSetDef)
{
if (int.TryParse(Label.text, out int num))
{
LanguageNum = num.ToString();
}
}
}
private void Start()
{
SetLanuageNum(LanguageNum.ToString());
//UnityLog.Log(LanguageMgr.Instance.CurLanguage + LanguageNum.ToString() + LanguageMgr.Instance.CurLanguage);
}
private void OnEnable()
{
if (Label)
SetLanuageNum(LanguageNum.ToString());
}
public void SetRealMsg(string msg)
{
try
{
if (gameObject && Label)
{
Label.text = msg;
}
}
catch
{
// UnityLog.LogError("设置文本错误=>" + name + msg);
}
}
///
/// 设置语言编号
///
///
public void SetLanuageNum(string num)
{
Debug.Log(num);
if (!string.IsNullOrEmpty(num) && Label)
{
int kk;
if (int.TryParse(num, out kk) || num.StartsWith("T"))
{
try
{
if (num.StartsWith("T"))
{
kk = int.Parse(num.Replace("T", ""));
}
this.languageNum = kk.ToString();
OnChangeLaunge(LanguageMgr.Instance.CurLanguage);
}
catch (Exception ex)
{
TimerMgr.Instance.CreateTimer(() =>
{
SetRealMsg(num);
}, 0.1f);
Debug.LogError("SetLanuageNum===>num ’s type is uncorrect:" + num);
}
}
else
{
TimerMgr.Instance.CreateTimer(() =>
{
SetRealMsg(num);
}, 0.1f);
}
//if (LanguageMgr.Instance.CurLanguage != null)
//{
// OnChangeLaunge(LanguageMgr.Instance.CurLanguage);
//}
//else
//{
// UnityLog.LogError(gameObject + "--" + num + " 当前语言包为空");
//}
}
else
{
// UnityLog.LogError(gameObject + "--" + num + " numkey is null!");
}
}
}