| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- using System;
- using System.Collections;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- public class UILocalizeScript : MonoBehaviour
- {
- public string m_key;
- private string mContent;
- private object[] m_Values;
- private Text mText;
- private TextMeshProUGUI mTextMeshProUGUI;
- bool hasSeted = false;
- private void Awake()
- {
- mText = GetComponent<Text>();
- mTextMeshProUGUI = GetComponent<TextMeshProUGUI>();
- Inner_SetContent(m_key);
- EventMgr.AddEventListener<int>(ECoreEventType.EID_LANGUAGE_CHANGE, OnLanguageChange);
- }
- private void OnLanguageChange(CoreEvent<int> e)
- {
- Refresh();
- }
- void OnEnable()
- {
- #if UNITY_EDITOR
- string old = mContent;
- Inner_SetContent(m_key);
- if (old != mContent)
- Refresh();
- #endif
- }
- void Start()
- {
- if (hasSeted) return;
- if (!string.IsNullOrEmpty(m_key))
- {
- Inner_SetContent(m_key);
- }
- Refresh();
- }
- private void OnDestroy()
- {
- EventMgr.RemoveEventListener<int>(ECoreEventType.EID_LANGUAGE_CHANGE, OnLanguageChange);
- }
- public void OnChangeLang()
- {
- if (!string.IsNullOrEmpty(m_key))
- {
- Inner_SetContent(m_key);
- Refresh();
- }
- }
- void Refresh()
- {
- if (mText == null)
- {
- mText = GetComponent<Text>();
- }
- if (!mTextMeshProUGUI)
- {
- mTextMeshProUGUI = GetComponent<TextMeshProUGUI>();
- }
- if (!string.IsNullOrEmpty(mContent))
- {
- string content;
- if (m_Values != null && m_Values.Length > 0)
- {
- content = string.Format(mContent, m_Values);
- }
- else
- {
- content = mContent;
- }
- if (mText)
- {
- mText.text = content;
- }
- if (mTextMeshProUGUI)
- {
- mTextMeshProUGUI.text = content;
- }
- }
- }
- void RefreshWithColor(string colorText)
- {
- if (mText == null)
- {
- mText = GetComponent<Text>();
- }
- if (!mTextMeshProUGUI)
- {
- mTextMeshProUGUI = GetComponent<TextMeshProUGUI>();
- }
- if (!string.IsNullOrEmpty(mContent))
- {
- string content;
- if (m_Values != null && m_Values.Length > 0)
- {
- content = string.Format(mContent, m_Values);
- }
- else
- {
- content = mContent;
- }
- colorText = colorText.Replace("%s", "{0}");
- content = string.Format(colorText, content);
- if (mText)
- {
- mText.text = content;
- }
- if (mTextMeshProUGUI)
- {
- mTextMeshProUGUI.text = content;
- }
- }
- else
- {
- CleanContent();
- }
- }
- public void SetContent(string contentKey)
- {
- hasSeted = true;
- if (!string.IsNullOrEmpty(contentKey))
- {
- m_key = contentKey;
- Inner_SetContent(m_key);
- Refresh();
- }
- else
- {
- CleanContent();
- }
- }
- public void CleanContent()
- {
- m_key = "";
- if (mText)
- {
- mText.text = "";
- }
- if (mTextMeshProUGUI)
- {
- mTextMeshProUGUI.text = "";
- }
- }
- public void SetValues(object[] values)
- {
- hasSeted = true;
- if (m_Values != values)
- {
- m_Values = values;
- Refresh();
- }
- }
- public void SetContentAndValues(string contentKey, object[] values)
- {
- hasSeted = true;
- bool isChanged = false;
- if (!string.IsNullOrEmpty(contentKey))
- {
- m_key = contentKey;
- Inner_SetContent(m_key);
- isChanged = true;
- }
- if (m_Values != values)
- {
- m_Values = values;
- isChanged = true;
- }
- if (isChanged)
- {
- Refresh();
- }
- }
- public void SetContentWithColor(string contentKey, string colorText)
- {
- hasSeted = true;
- if (!string.IsNullOrEmpty(contentKey))
- {
- m_key = contentKey;
- Inner_SetContent(m_key);
- RefreshWithColor(colorText);
- }
- else
- {
- CleanContent();
- }
- }
- public void SetValuesWithColor(object[] values, string colorText)
- {
- hasSeted = true;
- if (m_Values != values)
- {
- m_Values = values;
- RefreshWithColor(colorText);
- }
- }
- public void SetContentAndValuesWithColor(string contentKey, object[] values, string colorText)
- {
- hasSeted = true;
- bool isChanged = false;
- if (!string.IsNullOrEmpty(contentKey))
- {
- m_key = contentKey;
- Inner_SetContent(m_key);
- isChanged = true;
- }
- if (m_Values != values)
- {
- m_Values = values;
- isChanged = true;
- }
- if (isChanged)
- {
- RefreshWithColor(colorText);
- }
- }
- private void Inner_SetContent(string contentKey)
- {
- mContent = I18N.T(contentKey);
- mContent = mContent.Replace("\\n", "\n");
- }
- }
|