aboutsummaryrefslogtreecommitdiff
path: root/Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedImage.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedImage.cs')
-rw-r--r--Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedImage.cs48
1 files changed, 48 insertions, 0 deletions
diff --git a/Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedImage.cs b/Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedImage.cs
new file mode 100644
index 0000000..664ec95
--- /dev/null
+++ b/Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedImage.cs
@@ -0,0 +1,48 @@
+using UnityEngine;
+using UnityEngine.UI;
+
+namespace Lean.Localization
+{
+ /// <summary>This component will update an Image component with a localized sprite, or use a fallback if none is found</summary>
+ [ExecuteInEditMode]
+ [DisallowMultipleComponent]
+ [RequireComponent(typeof(Image))]
+ [HelpURL(LeanLocalization.HelpUrlPrefix + "LeanLocalizedImage")]
+ [AddComponentMenu(LeanLocalization.ComponentPathPrefix + "Localized Image")]
+ public class LeanLocalizedImage : 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 Image component attached to this GameObject
+ var image = GetComponent<Image>();
+
+ // Use translation?
+ if (translation != null && translation.Data is Sprite)
+ {
+ image.sprite = (Sprite)translation.Data;
+ }
+ // Use fallback?
+ else
+ {
+ image.sprite = FallbackSprite;
+ }
+ }
+
+ protected virtual void Awake()
+ {
+ // Should we set FallbackSprite?
+ if (FallbackSprite == null)
+ {
+ // Get the SpriteRenderer component attached to this GameObject
+ var spriteRenderer = GetComponent<Image>();
+
+ // Copy current sprite to fallback
+ FallbackSprite = spriteRenderer.sprite;
+ }
+ }
+ }
+} \ No newline at end of file