void AssignSoundToSource(AudioSource source, SoundValue sound) { if (sound.loop) { if (_permanentSources.ContainsKey(sound)) { Destroy(source.gameObject); source = _permanentSources[sound]; } else { _permanentSources[sound] = source; } } else { _usedSources.Add(source); } source.clip = sound.GetValue(); source.volume = sound.volume; source.loop = sound.loop; source.pitch = sound.pitch; if (sound.mixerGroup == null) { source.outputAudioMixerGroup = defaultSoundGroup; } else { source.outputAudioMixerGroup = sound.mixerGroup; } }
public void StopSound(SoundValue sound) { if (_permanentSources.ContainsKey(sound)) { Destroy(_permanentSources[sound].gameObject); _permanentSources.Remove(sound); } }
public void PlayMusic(SoundValue music, bool forceRestart = false) { if (musicSource == null) //first song { musicSource = new GameObject().AddComponent <AudioSource>(); musicSource.transform.parent = transform; musicSource.gameObject.name = "Music"; } if (musicSource.clip != music.value || forceRestart) { AssignSoundToSource(musicSource, music); musicSource.Play(); } }
void AssignSoundToSource(AudioSource source, SoundValue sound) { source.clip = sound.value; source.volume = sound.volume; source.loop = sound.loop; source.pitch = sound.pitch; if (sound.mixerGroup == null) { source.outputAudioMixerGroup = defaultSoundGroup; } else { source.outputAudioMixerGroup = sound.mixerGroup; } }
public AudioSource PlaySound(SoundValue sound) { AudioSource source; if (_freeSources.Count == 0) { source = new GameObject().AddComponent <AudioSource>(); source.transform.parent = transform; } else { source = _freeSources[0]; _freeSources.RemoveAt(0); } source.gameObject.name = "Sound: " + sound.name; AssignSoundToSource(source, sound); source.Play(); return(source); }