aboutsummaryrefslogtreecommitdiff
path: root/Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedTextFont.cs
blob: 6730956b25f5fdf1a90f97cf4153fb728b6c523d (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 a Text component's Font with a localized font, or use a fallback if none is found.</summary>
	[ExecuteInEditMode]
	[DisallowMultipleComponent]
	[RequireComponent(typeof(Text))]
	[HelpURL(LeanLocalization.HelpUrlPrefix + "LeanLocalizedTextFont")]
	[AddComponentMenu(LeanLocalization.ComponentPathPrefix + "Localized TextFont")]
	public class LeanLocalizedTextFont : LeanLocalizedBehaviour
	{
		[Tooltip("If PhraseName couldn't be found, this font will be used")]
		public Font FallbackFont;

		// 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<Text>();

			// Use translation?
			if (translation != null && translation.Data is Font)
			{
				text.font = (Font)translation.Data;
			}
			// Use fallback?
			else
			{
				text.font = FallbackFont;
			}
		}

		protected virtual void Awake()
		{
			// Should we set FallbackFont?
			if (FallbackFont == null)
			{
				// Get the Text component attached to this GameObject
				var text = GetComponent<Text>();

				// Copy current font to fallback
				FallbackFont = text.font;
			}
		}
	}
}