using System.Collections.Generic;
using UnityEngine;
namespace LanguageManager
{
///
/// Singleton class that manages audio clips for different languages based on audio IDs.
///
public class LanguageAudioManager : MonoBehaviour
{
public static LanguageAudioManager Instance;
///
/// Audio structure that holds a list of audio clips for each language.
///
[System.Serializable]
public class AudioEntry
{
public string audioID; // Unique identifier for the audio
public List languageAudios = new List(); // List of language-specific audio clips
}
///
/// Structure that associates a language ID with its corresponding audio clip.
///
[System.Serializable]
public class LanguageAudio
{
public string languageID; // The ID of the language (e.g., "en", "pt-BR", etc.)
public AudioClip audioClip; // The audio clip associated with the language
}
public List audioEntries = new List(); // List of audio entries to configure
private void Awake()
{
// Singleton pattern
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
///
/// Returns the audio clip for the given audio ID and the current language.
///
/// The ID of the audio to retrieve.
/// The AudioClip associated with the audioID and current language, or null if not found.
public AudioClip GetAudioByID(string audioID)
{
string currentLanguage = LanguageManager.Instance.GetCurrentLanguage();
foreach (AudioEntry entry in audioEntries)
{
if (entry.audioID == audioID)
{
foreach (LanguageAudio languageAudio in entry.languageAudios)
{
if (languageAudio.languageID == currentLanguage)
{
return languageAudio.audioClip;
}
}
}
}
Debug.LogError($"Audio for ID {audioID} and language {currentLanguage} not found.");
return null;
}
///
/// Checks if a specific audio clip exists for the given audio ID and current language.
///
/// The ID of the audio to check.
/// True if the audio clip exists for the current language, otherwise false.
public bool AudioExists(string audioID)
{
return GetAudioByID(audioID) != null;
}
///
/// Plays the audio clip for the given audio ID using the provided AudioSource.
///
/// The ID of the audio to play.
/// The AudioSource where the audio will be played.
public void PlayAudioByID(string audioID, AudioSource audioSource)
{
AudioClip clip = GetAudioByID(audioID);
if (clip != null)
{
audioSource.clip = clip;
audioSource.Play();
}
}
///
/// Retrieves all available languages for a specific audio ID.
///
/// The ID of the audio to retrieve the languages for.
/// A list of language IDs that have audio clips for the given audioID.
public List GetAvailableLanguagesForAudio(string audioID)
{
List languages = new List();
foreach (AudioEntry entry in audioEntries)
{
if (entry.audioID == audioID)
{
foreach (LanguageAudio languageAudio in entry.languageAudios)
{
languages.Add(languageAudio.languageID);
}
}
}
return languages;
}
}
}