aboutsummaryrefslogtreecommitdiff
path: root/Assets/Packages/Lean/Localization/Scripts/Behaviours/LeanLocalizedAudioSource.cs
blob: 7b455951371e20f3118695ff56a2a47b788653c2 (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
using UnityEngine;

namespace Lean.Localization
{
	/// <summary>This component will update an AudioSource component with localized text, or use a fallback if none is found.</summary>
	[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<AudioSource>();

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

				// Copy current sprite to fallback
				FallbackAudioClip = audioSource.clip;
			}
		}
	}
}