bad-piggies-0.1.3-porting/Assets/Scripts/Assembly-CSharp/TextMeshLocale.cs
2024-02-24 22:40:44 -05:00

71 lines
1.8 KiB
C#

using UnityEngine;
public class TextMeshLocale : MonoBehaviour
{
private TextMesh targetTextMesh;
private string originalTextContents = string.Empty;
private float originalCharacterSize;
private float originalLineSpacing;
private string postfix = string.Empty;
private Localizer.LocaleParameters localeParameters;
public string Postfix
{
set
{
postfix = value;
targetTextMesh.text = localeParameters.translation + postfix;
}
}
private void Start()
{
ApplyLocale();
}
private void ApplyLocale()
{
localeParameters = Localizer.Instance.Resolve(originalTextContents);
if ((bool)Localizer.Instance.LanguageFont)
{
Color color = targetTextMesh.renderer.material.color;
targetTextMesh.font = Localizer.Instance.LanguageFont;
targetTextMesh.renderer.material = Localizer.Instance.LanguageFont.material;
targetTextMesh.renderer.material.color = color;
}
targetTextMesh.text = localeParameters.translation + postfix;
targetTextMesh.characterSize = originalCharacterSize * localeParameters.characterSizeFactor;
targetTextMesh.lineSpacing = originalLineSpacing * localeParameters.lineSpacingFactor;
}
public void RefreshTranslation()
{
originalTextContents = targetTextMesh.text;
ApplyLocale();
}
private void Awake()
{
targetTextMesh = GetComponent<TextMesh>();
Assert.IsValid(targetTextMesh, "targetTextMesh");
originalCharacterSize = targetTextMesh.characterSize;
originalLineSpacing = targetTextMesh.lineSpacing;
originalTextContents = targetTextMesh.text;
EventManager.Connect<LocalizationReloaded>(ReceiveLocalizationReloaded);
}
private void OnDestroy()
{
EventManager.Disconnect<LocalizationReloaded>(ReceiveLocalizationReloaded);
}
private void ReceiveLocalizationReloaded(LocalizationReloaded localizationReloaded)
{
ApplyLocale();
}
}