Add Sound From Path
What it does: Adds the given 2D sound to the list of possible playable sounds and returns an AudioError (see Possible Errors), showing wheter and how adding the sound from the given path failed.
- INVALID_PATH
- MISSING_PARENT
- ALREADY_EXISTS
How to call it:
SoundName
is thename
the new sound should havePath
is the path to theAudioClip
we want to add to the new sound in the Resource folderVolume
is the volume we want the new sound to havePitch
is the pitch we want the new sound to haveLoop
defines wheter we want to repeat the new sound after completing it or notSource
is theAudioSource
object we want to add to the new soundMixerGroup
is theAudioMixerGroup
the sound is connected to
string soundName = "SoundName";
string path = "Audio/audioClip01";
float volume = 1f;
float pitch = 1f;
bool loop = false;
AudioSource source = null;
AudioMixerGroup mixerGroup = null;
AudioError err = am.AddSoundFromPath(soundName, path, volume, pitch, loop, source, mixerGroup);
if (err != AudioError.OK) {
Debug.Log("Adding the sound called: " + soundName + " from the given path failed with error id: " + err);
}
else {
Debug.Log("Adding the sound called: " + soundName + " from the given path succesfull");
}
Alternatively you can call the methods with less paramters as some of them have default arguments.
string soundName = "SoundName";
string path = "Audio/audioClip01";
AudioError err = am.AddSoundFromPath(soundName, path);
if (err != AudioError.OK) {
Debug.Log("Adding the sound called: " + soundName + " from the given path failed with error id: " + err);
}
else {
Debug.Log("Adding the sound called: " + soundName + " from the given path succesfull");
}
When to use it: When you want to add a new 2D sound at runtime, could be useful if you need to add a lot of songs and don’t want to add them manually through the GameObject the AudioManagerSettings
script for v1.7.0 and above see (GitHub release) or AudioManager
for older versions resides on.
Remarks: If 3D functionality and settings want to be added, additionaly call the Set3DAudioOptions
method.