57 lines
1.1 KiB
C#
57 lines
1.1 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using XPlugin.Localization;
|
|
|
|
namespace XLocalization {
|
|
|
|
public class TextHelper : MonoBehaviour {
|
|
|
|
public string Key;
|
|
public string OriginStringValue;//用于恢复
|
|
|
|
[ContextMenu("调试-恢复文字")]
|
|
void DebugRecover() {
|
|
Text text = this.gameObject.GetComponent<Text>();
|
|
if (null == text) {
|
|
return;
|
|
}
|
|
if (string.IsNullOrEmpty(text.text)) {
|
|
text.text = OriginStringValue;
|
|
}
|
|
}
|
|
|
|
[ContextMenu("调试-清空引用")]
|
|
void DebugClean() {
|
|
Text text = this.gameObject.GetComponent<Text>();
|
|
if (null == text) {
|
|
return;
|
|
}
|
|
text.font = null;
|
|
text.text = "";
|
|
}
|
|
|
|
void Awake() {
|
|
Refresh();
|
|
Localization.OnLanguageChanged += Refresh;
|
|
}
|
|
|
|
private void OnDestroy() {
|
|
Localization.OnLanguageChanged -= Refresh;
|
|
}
|
|
|
|
private void Refresh() {
|
|
Text text = this.gameObject.GetComponent<Text>();
|
|
if (null == text) {
|
|
return;
|
|
}
|
|
// text
|
|
if (!string.IsNullOrEmpty(Key))
|
|
{
|
|
var tmpValue = Key.ToLocalized();
|
|
text.text = tmpValue == Key ? LocalizedUtil.RecoverNewLine(OriginStringValue) : Key.ToLocalized();
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|