aboutsummaryrefslogtreecommitdiff
path: root/Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedImage.cs
blob: 664ec95150f248f2b6276891c0bead980a7ad593 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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;
			}
		}
	}
}