Created
March 7, 2018 19:35
-
-
Save alxcancado/158f80d308147809609b563f963e97ed to your computer and use it in GitHub Desktop.
Lean Localization TextMeshPro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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