using System.Collections.Generic;
using UnityEngine;
namespace LanguageManager
{
///
/// Represents an audio clip localized for a specific language.
///
[System.Serializable]
public class LocalizedAudioClip
{
public string languageID;
public AudioClip audioClip;
}
///
/// AudioWarper component that handles language-specific audio clips.
///
public class AudioWarper : MonoBehaviour
{
///
/// List of audio clips for different languages.
///
public List audioClips = new List();
///
/// Change the AudioSource's clip based on the current language.
///
[Header("Change AudioSource Clip based on Language")]
public bool changeAudioSourceClip = false;
///
/// The AudioSource to change the clip of.
///
public AudioSource audioSource;
private void Awake()
{
// Get the AudioSource component if not assigned
if (changeAudioSourceClip && audioSource == null)
{
audioSource = GetComponent();
}
// Subscribe to language change event
LanguageManager.onLanguageChanged += UpdateAudioClip;
}
private void OnDestroy()
{
// Unsubscribe from language change event
LanguageManager.onLanguageChanged -= UpdateAudioClip;
}
private void Start()
{
// Update the audio clip at the start
UpdateAudioClip();
}
///
/// Updates the audio clip based on the current language.
///
public void UpdateAudioClip()
{
string currentLanguageID = LanguageManager.Instance.currentLanguageID;
AudioClip clip = GetAudioClipForLanguage(currentLanguageID);
if (clip != null && changeAudioSourceClip && audioSource != null)
{
audioSource.clip = clip;
}
}
///
/// Gets the audio clip for the specified language.
///
/// The language ID.
/// The AudioClip for the language, or null if not found.
public AudioClip GetAudioClipForLanguage(string languageID)
{
foreach (LocalizedAudioClip localizedClip in audioClips)
{
if (localizedClip.languageID == languageID)
{
return localizedClip.audioClip;
}
}
return null;
}
///
/// Plays the audio clip for the current language.
///
public void PlayLocalizedAudio()
{
AudioClip clip = GetAudioClipForLanguage(LanguageManager.Instance.currentLanguageID);
if (clip != null)
{
if (audioSource != null)
{
audioSource.PlayOneShot(clip);
}
else
{
AudioSource.PlayClipAtPoint(clip, transform.position);
}
}
}
///
/// Stops the audio playback.
///
public void StopAudio()
{
if (audioSource != null)
{
audioSource.Stop();
}
}
}
}