diff options
| author | Andrew Lee <alee14498@gmail.com> | 2019-08-24 15:24:57 -0400 |
|---|---|---|
| committer | Andrew Lee <alee14498@gmail.com> | 2019-08-24 15:24:57 -0400 |
| commit | 85553832ead1a96f88726cd2b35cb6ff1d8b8cc8 (patch) | |
| tree | 7a2615034462d4296ed09d24038bb4c68107979d /Assets/Packages/Lean/Localization/Scripts/LeanLocalizedBehaviour.cs | |
| parent | e06acf066171670248b0b644c0eb8f6d895e051e (diff) | |
| download | Unicity-85553832ead1a96f88726cd2b35cb6ff1d8b8cc8.tar.gz Unicity-85553832ead1a96f88726cd2b35cb6ff1d8b8cc8.tar.bz2 Unicity-85553832ead1a96f88726cd2b35cb6ff1d8b8cc8.zip | |
Attempt number 2 on localization
Diffstat (limited to 'Assets/Packages/Lean/Localization/Scripts/LeanLocalizedBehaviour.cs')
| -rw-r--r-- | Assets/Packages/Lean/Localization/Scripts/LeanLocalizedBehaviour.cs | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/Assets/Packages/Lean/Localization/Scripts/LeanLocalizedBehaviour.cs b/Assets/Packages/Lean/Localization/Scripts/LeanLocalizedBehaviour.cs new file mode 100644 index 0000000..4163464 --- /dev/null +++ b/Assets/Packages/Lean/Localization/Scripts/LeanLocalizedBehaviour.cs @@ -0,0 +1,121 @@ +using UnityEngine; +using UnityEngine.Serialization; +using System.Collections.Generic; +using Lean.Common; +#if UNITY_EDITOR +using UnityEditor; + +namespace Lean.Localization +{ + [CanEditMultipleObjects] + [CustomEditor(typeof(LeanLocalizedBehaviour), true)] + public class LeanLocalizedBehaviour_Inspector : LeanInspector<LeanLocalizedBehaviour> + { + } +} +#endif + +namespace Lean.Localization +{ + /// <summary>This component simplifies the updating process, extend it if you want to cause a specific object to get localized</summary> + public abstract class LeanLocalizedBehaviour : MonoBehaviour + { + [Tooltip("The name of the phrase we want to use for this localized component")] + [SerializeField] + [LeanTranslationName] + [FormerlySerializedAs("phraseName")] + [FormerlySerializedAs("translationTitle")] + private string translationName; + + [System.NonSerialized] + private HashSet<LeanToken> tokens; + + /// <summary>This is the name of the translation this script uses.</summary> + public string TranslationName + { + set + { + if (translationName != value) + { + translationName = value; + + UpdateLocalization(); + } + } + + get + { + return translationName; + } + } + + public void Register(LeanToken token) + { + if (token != null) + { + if (tokens == null) + { + tokens = new HashSet<LeanToken>(); + } + + tokens.Add(token); + } + } + + public void Unregister(LeanToken token) + { + if (tokens != null) + { + tokens.Remove(token); + } + } + + public void UnregisterAll() + { + if (tokens != null) + { + foreach (var token in tokens) + { + token.Unregister(this); + } + + tokens.Clear(); + } + } + + // This gets called every time the translation needs updating + // NOTE: translation may be null if it can't be found + public abstract void UpdateTranslation(LeanTranslation translation); + + /// <summary>If you call this then this component will update using the translation for the specified phrase.</summary> + [ContextMenu("Update Localization")] + public void UpdateLocalization() + { + UpdateTranslation(LeanLocalization.GetTranslation(translationName)); + } + + protected virtual void OnEnable() + { + LeanLocalization.OnLocalizationChanged += UpdateLocalization; + + UpdateLocalization(); + } + + protected virtual void OnDisable() + { + LeanLocalization.OnLocalizationChanged -= UpdateLocalization; + + UnregisterAll(); + } + +#if UNITY_EDITOR + protected virtual void OnValidate() + { + if (isActiveAndEnabled == true) + { + UpdateLocalization(); + } + } +#endif + } +}
\ No newline at end of file |
