/// <summary> /// Update the sound on the specified audio source /// </summary> public void UpdateSound(AudioParams.SoundPoolGroups soundPoolGroupTag, AudioParams.SoundPools soundPoolTag, GameObject caller, float volumeEffector) { int audioSourceKey = (soundPoolGroupTag.ToString() + soundPoolTag.ToString() + caller.GetInstanceID().ToString()).GetHashCode(); if (createdAudioSourcesDictionnary.ContainsKey(audioSourceKey)) { AudioSource modifiedAudioSource = createdAudioSourcesDictionnary[audioSourceKey].audioSource; modifiedAudioSource.volume *= volumeEffector; } }
/// <summary> /// Stop playing a sound on the specified audio source /// </summary> public void StopSound(AudioParams.SoundPoolGroups soundPoolGroupTag, AudioParams.SoundPools soundPoolTag, GameObject caller) { int audioSourceKey = (soundPoolGroupTag.ToString() + soundPoolTag.ToString() + caller.GetInstanceID().ToString()).GetHashCode(); if (createdAudioSourcesDictionnary.ContainsKey(audioSourceKey)) { Destroy(createdAudioSourcesDictionnary[audioSourceKey].audioSource); createdAudioSourcesDictionnary.Remove(audioSourceKey); } }
/// <summary> /// Play sound on the game object without interruption if already playing /// </summary> public void PlaySoundOverFlow(AudioParams.SoundPoolGroups soundPoolGroupTag, AudioParams.SoundPools soundPoolTag, GameObject caller) { int audioSourceKey = (soundPoolGroupTag.ToString() + soundPoolTag.ToString() + caller.GetInstanceID().ToString()).GetHashCode(); if (!createdAudioSourcesDictionnary.ContainsKey(audioSourceKey)) { //Debug.Log("FIRST CALL CREATING THE DICT !! key = " + audioSourceKey); SoundPoolGroup foundSoundPoolGroup = soundPoolGroups.FirstOrDefault(currentSoundPoolGroup => currentSoundPoolGroup.Tag == soundPoolGroupTag); if (foundSoundPoolGroup == null) { //Debug.LogWarning("Sound Pool Group : " + soundPoolGroupTag.ToString() + " was not found !"); return; } else { SoundPool foundSoundPool = foundSoundPoolGroup.GetSoundPool(soundPoolTag); if (foundSoundPool != null) { AudioSource newAudioSource = caller.AddComponent <AudioSource>(); newAudioSource = foundSoundPool.UpdateAudioSource(newAudioSource); if (newAudioSource != null) { newAudioSource.Play(); createdAudioSourcesDictionnary.Add(audioSourceKey, new CreatedAudioSources(foundSoundPool, newAudioSource)); } } } } //here is the case where we already made this search but the audio source got destroyed between the privious call and this one else if (createdAudioSourcesDictionnary[audioSourceKey].audioSource == null) { //Debug.Log("RECREATING THE AUDIO SOURCE"); AudioSource recreatedAudioSource = caller.AddComponent <AudioSource>(); recreatedAudioSource = createdAudioSourcesDictionnary[audioSourceKey].soundPool .UpdateAudioSource(recreatedAudioSource); createdAudioSourcesDictionnary[audioSourceKey].audioSource = recreatedAudioSource; createdAudioSourcesDictionnary[audioSourceKey].audioSource.Play(); } //here is the case where we already made tgis search and the audio source is still there else { //Debug.Log("ALREADY THERE"); createdAudioSourcesDictionnary[audioSourceKey].audioSource = createdAudioSourcesDictionnary[audioSourceKey].soundPool .UpdateAudioSource(createdAudioSourcesDictionnary[audioSourceKey].audioSource); if (!createdAudioSourcesDictionnary[audioSourceKey].audioSource.isPlaying) { createdAudioSourcesDictionnary[audioSourceKey].audioSource.Play(); } } }
/// <summary> /// Play sound on the game object with modified volume /// </summary> public void PlaySoundModified(AudioParams.SoundPoolGroups soundPoolGroupTag, AudioParams.SoundPools soundPoolTag, GameObject caller, float volumeEffector) { int audioSourceKey = (soundPoolGroupTag.ToString() + soundPoolTag.ToString() + caller.GetInstanceID().ToString()).GetHashCode(); if (!createdAudioSourcesDictionnary.ContainsKey(audioSourceKey)) { SoundPoolGroup foundSoundPoolGroup = soundPoolGroups.FirstOrDefault(currentSoundPoolGroup => currentSoundPoolGroup.Tag == soundPoolGroupTag); if (foundSoundPoolGroup == null) { return; } else { SoundPool foundSoundPool = foundSoundPoolGroup.GetSoundPool(soundPoolTag); if (foundSoundPool != null) { AudioSource newAudioSource = caller.AddComponent <AudioSource>(); newAudioSource = foundSoundPool.UpdateAudioSource(newAudioSource); if (newAudioSource != null) { createdAudioSourcesDictionnary.Add(audioSourceKey, new CreatedAudioSources(foundSoundPool, newAudioSource)); AudioSource modifiedAudioSource = newAudioSource; modifiedAudioSource.volume *= volumeEffector; modifiedAudioSource.Play(); } } } } //here is the case where we already made this search but the audio source got destroyed between the privious call and this one else if (createdAudioSourcesDictionnary[audioSourceKey].audioSource == null) { AudioSource recreatedAudioSource = caller.AddComponent <AudioSource>(); recreatedAudioSource = createdAudioSourcesDictionnary[audioSourceKey].soundPool .UpdateAudioSource(recreatedAudioSource); createdAudioSourcesDictionnary[audioSourceKey].audioSource = recreatedAudioSource; AudioSource modifiedAudioSource = recreatedAudioSource; modifiedAudioSource.volume *= volumeEffector; modifiedAudioSource.Play(); } //here is the case where we already made tgis search and the audio source is still there else { createdAudioSourcesDictionnary[audioSourceKey].audioSource = createdAudioSourcesDictionnary[audioSourceKey].soundPool .UpdateAudioSource(createdAudioSourcesDictionnary[audioSourceKey].audioSource); AudioSource modifiedAudioSource = createdAudioSourcesDictionnary[audioSourceKey].audioSource; modifiedAudioSource.volume *= volumeEffector; modifiedAudioSource.Play(); } }