Importing Audio into Custom Script
Importing Audio into Custom Script
Overview
The LanguageSystem PRO supports language-specific audio clips, allowing you to play different audio files based on the current language setting. This system is managed by the LanguageAudioManager, which uses audio IDs to retrieve the appropriate audio clip for the selected language.
In this section, you will learn how to:
Retrieve and play an audio clip based on the selected language using its audio ID.
Implement the CustomAudioImport component to integrate this feature into your custom scripts.
Retrieving Language-Specific Audio Clips
To access an audio clip based on the current language, use the LanguageAudioManager. It manages all audio clips by associating them with an audioID
and a list of language-specific AudioClip
objects.
Example: How to Retrieve and Play an Audio Clip
Here is a basic example of how to retrieve and play an audio clip using the LanguageAudioManager.
public class PlayLocalizedAudio : MonoBehaviour
{
public string audioID; // Unique audio ID for this sound
public AudioSource audioSource; // The AudioSource that will play the audio
private void Start()
{
// Get the audio clip based on the current language and audioID
AudioClip clip = LanguageAudioManager.Instance.GetAudioByID(audioID);
// Check if the clip exists, then play it
if (clip != null)
{
audioSource.clip = clip;
audioSource.Play();
}
else
{
Debug.LogError($"Audio clip for '{audioID}' not found.");
}
}
}
In this example:
The
audioID
is used to find the appropriate audio clip for the current language.The audio is played via the provided
AudioSource
on the GameObject.
CustomAudioImport Example
To make this process easier, you can use the CustomAudioImport component, which serves as a practical implementation of the LanguageAudioManager in a reusable format. This component automatically retrieves and plays the correct audio clip for the current language based on a given audioID
.
How to Use CustomAudioImport
Follow these steps to integrate CustomAudioImport:
Attach CustomAudioImport: Add the CustomAudioImport component to any GameObject with an
AudioSource
component. If it doesn't have anAudioSource
, you'll need to add one.Set the Audio ID: In the CustomAudioImport component, set the
audioID
field to the desired audio ID (e.g., "greeting", "farewell").Automatic Playback: Once set up, CustomAudioImport will automatically retrieve the appropriate audio clip for the current language when the GameObject is activated.
CustomAudioImport Script Example
csharpCopiar códigousing UnityEngine;
public class CustomAudioImport : MonoBehaviour
{
public string audioID; // The ID of the audio to play
private AudioSource audioSource;
private void Awake()
{
audioSource = GetComponent<AudioSource>();
// Automatically play the audio when the component starts
PlayAudioManually();
}
/// <summary>
/// Manually plays the audio associated with the current language and audio ID.
/// </summary>
public void PlayAudioManually()
{
if (audioSource == null)
{
Debug.LogError("No AudioSource found on this GameObject.");
return;
}
AudioClip clip = LanguageAudioManager.Instance.GetAudioByID(audioID);
if (clip != null)
{
audioSource.clip = clip;
audioSource.Play();
}
else
{
Debug.LogError($"Audio clip for '{audioID}' not found.");
}
}
}
Customizing Audio for Each Language
Using CustomAudioImport or manual scripting, you can customize audio playback for each language. This flexibility allows for localized voiceovers, sound effects, or notifications tailored to your audience’s language preference.
Summary of Key Functions in LanguageAudioManager
GetAudioByID(audioID): Retrieves the
AudioClip
for the current language and the givenaudioID
.PlayAudioByID(audioID, AudioSource): Plays the audio clip for the
audioID
using the providedAudioSource
.AudioExists(audioID): Checks if an audio clip exists for the given
audioID
in the current language.
Last updated