aboutsummaryrefslogtreecommitdiff
path: root/Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedRenderer.cs
blob: 701e53885d65f800b60c3da6b2b5db5d6ead5f7f (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
49
50
51
52
53
54
55
using UnityEngine;

namespace Lean.Localization
{
	/// <summary>This component will update a Renderer component's sharedMaterial with a localized material, or use a fallback if none is found.</summary>
	[ExecuteInEditMode]
	[DisallowMultipleComponent]
	[RequireComponent(typeof(Renderer))]
	[HelpURL(LeanLocalization.HelpUrlPrefix + "LeanLocalizedRenderer")]
	[AddComponentMenu(LeanLocalization.ComponentPathPrefix + "Localized Renderer")]
	public class LeanLocalizedRenderer : LeanLocalizedBehaviour
	{
		[Tooltip("If PhraseName couldn't be found, this material will be used")]
		public Material FallbackMaterial;

		[Tooltip("The material index you want to replace.")]
		public int Index;

		// This gets called every time the translation needs updating
		public override void UpdateTranslation(LeanTranslation translation)
		{
			// Get the Renderer component attached to this GameObject
			var renderer = GetComponent<Renderer>();

			// Get the shared materials of this component
			var sharedMaterials = renderer.sharedMaterials;

			// Use translation?
			if (translation != null && translation.Data is Material)
			{
				sharedMaterials[Index] = (Material)translation.Data;
			}
			// Use fallback?
			else
			{
				sharedMaterials[Index] = FallbackMaterial;
			}

			renderer.sharedMaterials = sharedMaterials;
		}

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

				// Copy current material to fallback
				FallbackMaterial = renderer.sharedMaterials[Index];
			}
		}
	}
}