public static void PlayLoopingMusic(AudioSource audioSource, float volume) { if (!instantiated) { Instantiate(); } //Debug.Log("Music count:" + musics.Count); //Check if we have this audiosource already added for (int i = musics.Count - 1; i >= 0; i--) { LoopingAudioSource loopingAS = musics[i]; if (loopingAS.audioSource == audioSource) { musics.RemoveAt(i); } loopingAS.Stop(); } LoopingAudioSource loopingAudioSource = new LoopingAudioSource(audioSource); float targetVolume = volume * realMusicVolume; loopingAudioSource.persistTag = persistTag; loopingAudioSource.Play(targetVolume); audioSource.gameObject.transform.parent = null; GameObject.DontDestroyOnLoad(audioSource.gameObject); musics.Add(loopingAudioSource); }
public static void PlayLoopingSound(AudioSource audioSource, AudioClip audioClip, float volume, Vector3 position, bool is3D) { if (!instantiated) { Instantiate(); } //Debug.Log("Ambient sounds:" + ambientSounds.Count); for (int i = ambientSounds.Count - 1; i >= 0; i--) { LoopingAudioSource loopingAS = ambientSounds[i]; if (loopingAS.audioSource == audioSource) { ambientSounds.RemoveAt(i); } loopingAS.Stop(); } LoopingAudioSource loopingAudioSource = new LoopingAudioSource(audioSource, false, 1.0f, is3D); float targetVolume = volume * realSoundVolume; if (is3D) { loopingAudioSource.audioSource.gameObject.transform.position = position; //enable 3D settings //Debug.Log("enable 3D settings!"); loopingAudioSource.audioSource.spatialBlend = 1; } loopingAudioSource.Play(targetVolume); ambientSounds.Add(loopingAudioSource); }
public static void StopLoopingSound(AudioSource audioSource) { for (int i = 0; i < ambientSounds.Count; i++) { LoopingAudioSource l = ambientSounds[i]; if (l.audioSource == audioSource) { l.Stop(); ambientSounds.RemoveAt(i); return; } } }