Skip to content

Instantly share code, notes, and snippets.

@alxcancado
Created March 7, 2018 19:35
Show Gist options
  • Save alxcancado/158f80d308147809609b563f963e97ed to your computer and use it in GitHub Desktop.
Save alxcancado/158f80d308147809609b563f963e97ed to your computer and use it in GitHub Desktop.
Lean Localization TextMeshPro
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace Lean.Localization
{
// This component will update a Text component with localized text, or use a fallback if none is found
[ExecuteInEditMode]
[DisallowMultipleComponent]
[RequireComponent(typeof(TextMeshProUGUI))]
public class LeanLocalizedTextMeshProUGUI : LeanLocalizedBehaviour
{
[Tooltip("If PhraseName couldn't be found, this text will be used")]
public string FallbackText;
// This gets called every time the translation needs updating
public override void UpdateTranslation(LeanTranslation translation)
{
// Get the Text component attached to this GameObject
var text = GetComponent<TextMeshProUGUI>();
// Use translation?
if (translation != null)
{
text.text = translation.Text;
}
// Use fallback?
else
{
text.text = FallbackText;
}
}
protected virtual void Awake()
{
// Should we set FallbackText?
if (string.IsNullOrEmpty(FallbackText) == true)
{
// Get the Text component attached to this GameObject
var text = GetComponent<TextMeshProUGUI>();
// Copy current text to fallback
FallbackText = text.text;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment