From 85553832ead1a96f88726cd2b35cb6ff1d8b8cc8 Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Sat, 24 Aug 2019 15:24:57 -0400 Subject: Attempt number 2 on localization --- .../Behaviours/LeanLocalizedSpriteRenderer.cs | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedSpriteRenderer.cs (limited to 'Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedSpriteRenderer.cs') diff --git a/Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedSpriteRenderer.cs b/Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedSpriteRenderer.cs new file mode 100644 index 0000000..e1170b0 --- /dev/null +++ b/Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedSpriteRenderer.cs @@ -0,0 +1,47 @@ +using UnityEngine; + +namespace Lean.Localization +{ + /// This component will update a SpriteRenderer component with a localized sprite, or use a fallback if none is found + [ExecuteInEditMode] + [DisallowMultipleComponent] + [RequireComponent(typeof(SpriteRenderer))] + [HelpURL(LeanLocalization.HelpUrlPrefix + "LeanLocalizedSpriteRenderer")] + [AddComponentMenu(LeanLocalization.ComponentPathPrefix + "Localized SpriteRenderer")] + public class LeanLocalizedSpriteRenderer : LeanLocalizedBehaviour + { + [Tooltip("If PhraseName couldn't be found, this sprite will be used")] + public Sprite FallbackSprite; + + // This gets called every time the translation needs updating + public override void UpdateTranslation(LeanTranslation translation) + { + // Get the SpriteRenderer component attached to this GameObject + var spriteRenderer = GetComponent(); + + // Use translation? + if (translation != null && translation.Data is Sprite) + { + spriteRenderer.sprite = (Sprite)translation.Data; + } + // Use fallback? + else + { + spriteRenderer.sprite = FallbackSprite; + } + } + + protected virtual void Awake() + { + // Should we set FallbackSprite? + if (FallbackSprite == null) + { + // Get the SpriteRenderer component attached to this GameObject + var spriteRenderer = GetComponent(); + + // Copy current sprite to fallback + FallbackSprite = spriteRenderer.sprite; + } + } + } +} \ No newline at end of file -- cgit v1.2.3