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/LeanLocalizedAudioSource.cs | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedAudioSource.cs (limited to 'Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedAudioSource.cs') diff --git a/Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedAudioSource.cs b/Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedAudioSource.cs new file mode 100644 index 0000000..7b45595 --- /dev/null +++ b/Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedAudioSource.cs @@ -0,0 +1,47 @@ +using UnityEngine; + +namespace Lean.Localization +{ + /// This component will update an AudioSource component with localized text, or use a fallback if none is found. + [ExecuteInEditMode] + [DisallowMultipleComponent] + [RequireComponent(typeof(AudioSource))] + [HelpURL(LeanLocalization.HelpUrlPrefix + "LeanLocalizedAudioSource")] + [AddComponentMenu(LeanLocalization.ComponentPathPrefix + "Localized AudioSource")] + public class LeanLocalizedAudioSource : LeanLocalizedBehaviour + { + [Tooltip("If PhraseName couldn't be found, this clip will be used")] + public AudioClip FallbackAudioClip; + + // This gets called every time the translation needs updating + public override void UpdateTranslation(LeanTranslation translation) + { + // Get the AudioSource component attached to this GameObject + var audioSource = GetComponent(); + + // Use translation? + if (translation != null && translation.Data is AudioClip) + { + audioSource.clip = (AudioClip)translation.Data; + } + // Use fallback? + else + { + audioSource.clip = FallbackAudioClip; + } + } + + protected virtual void Awake() + { + // Should we set FallbackAudioClip? + if (FallbackAudioClip == null) + { + // Get the AudioSource component attached to this GameObject + var audioSource = GetComponent(); + + // Copy current sprite to fallback + FallbackAudioClip = audioSource.clip; + } + } + } +} \ No newline at end of file -- cgit v1.2.3