OctoCore 0.0.1
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
using System;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace OctoCore.Localization
|
||||
{
|
||||
public class OctoLocalizedText : MonoBehaviour
|
||||
{
|
||||
[SerializeField] OctoLocalizedTextData data;
|
||||
[SerializeField] Text text;
|
||||
[SerializeField] TextMeshPro textMeshPro;
|
||||
[SerializeField] TextMeshProUGUI textMeshProUGUI;
|
||||
|
||||
Action localize;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
void OnValidate()
|
||||
{
|
||||
if (Application.isPlaying)
|
||||
return;
|
||||
|
||||
bool setDirty = false;
|
||||
if (!text && !textMeshPro && !textMeshProUGUI)
|
||||
{
|
||||
text = GetComponent<Text>();
|
||||
textMeshPro = GetComponent<TextMeshPro>();
|
||||
textMeshProUGUI = GetComponent<TextMeshProUGUI>();
|
||||
|
||||
if (text || textMeshPro || textMeshProUGUI)
|
||||
setDirty = true;
|
||||
}
|
||||
|
||||
if (text && text.transform != transform)
|
||||
{
|
||||
text = null;
|
||||
setDirty = true;
|
||||
}
|
||||
|
||||
if (textMeshPro && textMeshPro.transform != transform)
|
||||
{
|
||||
textMeshPro = null;
|
||||
setDirty = true;
|
||||
}
|
||||
|
||||
if (textMeshProUGUI && textMeshProUGUI.transform != transform)
|
||||
{
|
||||
textMeshProUGUI = null;
|
||||
setDirty = true;
|
||||
}
|
||||
|
||||
if (setDirty)
|
||||
UnityEditor.EditorUtility.SetDirty(this);
|
||||
|
||||
Localize();
|
||||
}
|
||||
#endif
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (!OctoCore.Instance)
|
||||
{
|
||||
Debug.LogWarning($"[OctoLocalizedText] OctoCore.Instance not found");
|
||||
return;
|
||||
}
|
||||
|
||||
Localize();
|
||||
OctoCore.Instance.Localization.SubscribeOnLanguageWrittenChange(localize = Localize);
|
||||
|
||||
if (OctoCore.Instance.Development.Log && data)
|
||||
Debug.Log($"{name} subscribed on language written change");
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
if (!OctoCore.Instance) return;
|
||||
|
||||
if (localize != null)
|
||||
{
|
||||
OctoCore.Instance.Localization.UnsubscribeOnLanguageWrittenChange(localize);
|
||||
localize = null;
|
||||
|
||||
if (OctoCore.Instance.Development.Log && data)
|
||||
Debug.Log($"{name} unsubscribed on language written change");
|
||||
}
|
||||
}
|
||||
|
||||
public void Localize()
|
||||
{
|
||||
if (!data) return;
|
||||
|
||||
string localizedText = OctoLocalization.GetLocalizedText(data);
|
||||
|
||||
if (text)
|
||||
{
|
||||
text.text = localizedText;
|
||||
}
|
||||
else if (textMeshPro)
|
||||
{
|
||||
textMeshPro.text = localizedText;
|
||||
}
|
||||
else if (textMeshProUGUI)
|
||||
{
|
||||
textMeshProUGUI.text = localizedText;
|
||||
//LayoutRebuilder.ForceRebuildLayoutImmediate(textMeshProUGUI.rectTransform);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user