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 --- .../Scripts/Behaviours/LeanLocalizedRenderer.cs | 55 ++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedRenderer.cs (limited to 'Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedRenderer.cs') diff --git a/Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedRenderer.cs b/Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedRenderer.cs new file mode 100644 index 0000000..701e538 --- /dev/null +++ b/Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedRenderer.cs @@ -0,0 +1,55 @@ +using UnityEngine; + +namespace Lean.Localization +{ + /// This component will update a Renderer component's sharedMaterial with a localized material, or use a fallback if none is found. + [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(); + + // 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(); + + // Copy current material to fallback + FallbackMaterial = renderer.sharedMaterials[Index]; + } + } + } +} \ No newline at end of file -- cgit v1.2.3