public void RecycleSound(SKSound sound) { if (sound == backgroundSound) { return; } int index = 0; while (index < playingSounds.Count) { if (playingSounds[index] == sound) { break; } index++; } playingSounds.RemoveAt(index); if (availableSounds.Count + playingSounds.Count >= maxCapacity) { Destroy(sound.audioSource); } else { availableSounds.Push(sound); } }
/// <summary> /// used internally to recycle SKSounds and their AudioSources /// </summary> /// <param name="sound">Sound.</param> public void recycleSound(SKSound sound) { var index = 0; while (index < _playingSounds.Count) { if (_playingSounds[index] == sound) { break; } index++; } _playingSounds.RemoveAt(index); // if we are already over capacity dont recycle this sound but destroy it instead if (_availableSounds.Count + _playingSounds.Count >= maxCapacity) { Destroy(sound.audioSource); } else { _availableSounds.Push(sound); } }
/// <summary> /// used internally to recycle SKSounds and their AudioSources /// </summary> /// <param name="sound">Sound.</param> public void RecycleSound(SKSound sound) { // we dont recycle the BackgroundSound since it always stays alive if (sound == BackgroundSound || sound == OneShotSound) { return; } var index = 0; while (index < PlayingSounds.Count) { if (PlayingSounds[index] == sound) { break; } index++; } PlayingSounds.RemoveAt(index); // if we are already over capacity dont recycle this sound but destroy it instead if (AvailableSounds.Count + PlayingSounds.Count >= MaxCapacity) { Destroy(sound.AudioSource); } else { AvailableSounds.Push(sound); } }
/// <summary> /// used internally to recycle SKSounds and their AudioSources /// </summary> /// <param name="sound">Sound.</param> public void recycleSound(SKSound sound) { // we dont recycle the backgroundSound since it always stays alive if (sound == backgroundSound) { return; } var index = 0; while (index < _playingSounds.Count) { if (_playingSounds[index] == sound) { break; } index++; } _playingSounds.RemoveAt(index); // if we are already over capacity dont recycle this sound but destroy it instead if (_availableSounds.Count + _playingSounds.Count >= maxCapacity) { Destroy(sound.audioSource); } else { _availableSounds.Push(sound); } }
/// <summary> /// fetches any AudioSource it can find and uses the standard PlayOneShot to play. Use this if you don't require any /// extra control over a clip and don't care about when it completes. It avoids the call to StartCoroutine. /// nb. pan/pitch are not supported as the chosen AudioSource might be in use with another pan/pitch setting and Unity does not support setting /// them natively in PlayOneShot, so updating them here can result in bad audio. /// </summary> /// <param name="audioClip">Audio clip.</param> /// <param name="volumeScale">Volume scale.</param> public void playOneShot( AudioClip audioClip, float volumeScale = 1f ) { if (oneShotSound == null) oneShotSound = new SKSound(this); oneShotSound.audioSource.PlayOneShot( audioClip, volumeScale * soundEffectVolume ); }
public SKSound PlaySound(AudioClip audioClip, float volumeScale, float pitch, float pan) { SKSound sound = NextAvailableSound(); sound.PlayAudioClip(audioClip, volumeScale * soundEffectVolume, pitch, pan); return(sound); }
private void checkSound(SKSound sound) { sound._elapsedTime += Time.deltaTime; if (sound._elapsedTime > sound.audioSource.clip.length) { sound.stop(); } }
/// <summary> /// starts up the background music and optionally loops it. You can access the SKSound via the backgroundSound field. /// </summary> /// <param name="audioClip">Audio clip.</param> /// <param name="loop">If set to <c>true</c> loop.</param> public void playBackgroundMusic( AudioClip audioClip, float volume, bool loop = true ) { if( backgroundSound == null ) backgroundSound = new SKSound( this ); backgroundSound.playAudioClip( audioClip, volume, 1f, 0f ); backgroundSound.setLoop( loop ); }
public SKSound PlaySoundLooped(AudioClip audioClip) { SKSound sound = NextAvailableSound(); sound.PlayAudioClip(audioClip, soundEffectVolume, 1f, 0f); sound.SetLoop(true); return(sound); }
/// <summary> /// fetches any AudioSource it can find and uses the standard PlayOneShot to play. Use this if you don't require any /// extra control over a clip and don't care about when it completes. It avoids the call to StartCoroutine. /// nb. pan/pitch are not supported as the chosen AudioSource might be in use with another pan/pitch setting and Unity does not support setting /// them natively in PlayOneShot, so updating them here can result in bad audio. /// </summary> /// <param name="audioClip">Audio clip.</param> /// <param name="volumeScale">Volume scale.</param> public void playOneShot(AudioClip audioClip, float volumeScale = 1f, SoundType soundType = SoundType.SFX) { if (oneShotSound == null) { oneShotSound = new SKSound(this); } oneShotSound.audioSource.PlayOneShot(audioClip, volumeScale * getVolume(soundType)); }
/// <summary> /// fetches any AudioSource it can find and uses the standard PlayOneShot to play. Use this if you don't require any /// extra control over a clip and don't care about when it completes. It avoids the call to StartCoroutine. /// nb. pan/pitch are not supported as the chosen AudioSource might be in use with another pan/pitch setting and Unity does not support setting /// them natively in PlayOneShot, so updating them here can result in bad audio. /// </summary> /// <param name="audioClip">Audio clip.</param> /// <param name="volumeScale">Volume scale.</param> public void playOneShot(AudioClip audioClip, float volumeScale = 1f) { if (oneShotSound == null) { oneShotSound = new SKSound(this); } oneShotSound.audioSource.PlayOneShot(audioClip, volumeScale * soundEffectVolume); }
/// <summary> /// plays the AudioClip with the specified volumeScale, pitch and pan /// </summary> /// <returns>The sound.</returns> /// <param name="audioClip">Audio clip.</param> /// <param name="volume">Volume.</param> /// <param name="pitch">Pitch.</param> /// <param name="pan">Pan.</param> public SKSound playSound(AudioClip audioClip, float volumeScale, float pitch, float pan, SoundType soundType = SoundType.SFX) { // Find the first SKSound not being used. if they are all in use, create a new one SKSound sound = nextAvailableSound(); sound.playAudioClip(audioClip, volumeScale * getVolume(soundType), pitch, pan, soundType); return(sound); }
/// <summary> /// plays the AudioClip with the specified volumeScale, pitch and pan /// </summary> /// <returns>The sound.</returns> /// <param name="audioClip">Audio clip.</param> /// <param name="volume">Volume.</param> /// <param name="pitch">Pitch.</param> /// <param name="pan">Pan.</param> public SKSound playSound(AudioClip audioClip, float volumeScale, float pitch, float pan) { // Find the first SKSound not being used. if they are all in use, create a new one SKSound sound = nextAvailableSound(); sound.playAudioClip(audioClip, volumeScale * soundEffectVolume, pitch, pan); return(sound); }
/// <summary> /// starts up the background music and optionally loops it. You can access the SKSound via the backgroundSound field. /// </summary> /// <param name="audioClip">Audio clip.</param> /// <param name="loop">If set to <c>true</c> loop.</param> public void playBackgroundMusic(AudioClip audioClip, float volume, bool loop = true) { if (backgroundSound == null) { backgroundSound = new SKSound(this); } backgroundSound.playAudioClip(audioClip, volume, 1f, 0f); backgroundSound.setLoop(loop); }
/// <summary> /// loops the AudioClip. Do note that you are responsible for calling either stop or fadeOutAndStop on the SKSound /// or it will not be recycled /// </summary> /// <returns>The sound looped.</returns> /// <param name="audioClip">Audio clip.</param> public SKSound playSoundLooped(AudioClip audioClip, SoundType soundType = SoundType.SFX) { // find the first SKSound not being used. if they are all in use, create a new one SKSound sound = nextAvailableSound(); sound.playAudioClip(audioClip, getVolume(soundType), 1f, 0f, soundType); sound.setLoop(true); return(sound); }
/// <summary> /// loops the AudioClip. Do note that you are responsible for calling either stop or fadeOutAndStop on the SKSound /// or it will not be recycled /// </summary> /// <returns>The sound looped.</returns> /// <param name="audioClip">Audio clip.</param> public SKSound playSoundLooped(AudioClip audioClip) { // find the first SKSound not being used. if they are all in use, create a new one SKSound sound = nextAvailableSound(); sound.playAudioClip(audioClip, soundEffectVolume, 1f, 0f); sound.setLoop(true); return(sound); }
/// <summary> /// starts up the background music and optionally loops it. You can access the SKSound via the backgroundSound field. /// </summary> /// <param name="audioClip">Audio clip.</param> /// <param name="loop">If set to <c>true</c> loop.</param> public SKSound playBackgroundMusic(AudioClip audioClip, float volume, bool loop = true) { if (backgroundSound == null) { backgroundSound = new SKSound(this); } backgroundSound.playAudioClip(audioClip, volume * getVolume(SoundType.BGM), 1f, 0f, SoundType.BGM); backgroundSound.setLoop(loop); return(backgroundSound); }
void OnLevelWasLoaded(int level) { if (dontDestroyOnLoad && clearAllAudioClipsOnLevelLoad) { for (int index = playingSounds.Count - 1; index >= 0; index--) { SKSound sound = playingSounds[index]; sound.audioSource.clip = null; availableSounds.Push(sound); playingSounds.RemoveAt(index); } } }
/// <summary> /// starts up the background music and optionally loops it. You can access the SKSound via the backgroundSound field. /// </summary> /// <param name="audioClip">Audio clip.</param> /// <param name="loop">If set to <c>true</c> loop.</param> public void playBackgroundMusic(AudioClip audioClip, float volume, bool loop = true) { if (audioClip == null) { Debug.LogWarning("playBackgroundMusic was called with a null AudioClip."); return; } if (backgroundSound == null) { backgroundSound = new SKSound(this); } backgroundSound.playAudioClip(audioClip, volume * bgmVolume, 1f, 0f, bgmGroup); backgroundSound.setLoop(loop); }
/// <summary> /// plays the AudioClip with the specified volumeScale, pitch and pan /// </summary> /// <returns>The sound.</returns> /// <param name="audioClip">Audio clip.</param> /// <param name="volume">Volume.</param> /// <param name="pitch">Pitch.</param> /// <param name="pan">Pan.</param> public SKSound PlaySound(AudioClip audioClip, float volumeScale, float pitch, float pan) { if (audioClip == null) { Debug.LogError("Null sound played"); return(null); } // Find the first SKSound not being used. if they are all in use, create a new one SKSound sound = NextAvailableSound(); sound.playAudioClip(audioClip, volumeScale * SoundVolume, pitch, pan); return(sound); }
/// <summary> /// plays the AudioClip with the specified volumeScale, pitch and pan /// </summary> /// <returns>The sound.</returns> /// <param name="audioClip">Audio clip.</param> /// <param name="volume">Volume.</param> /// <param name="pitch">Pitch.</param> /// <param name="pan">Pan.</param> public SKSound playSound(AudioClip audioClip, float volumeScale, float pitch, float pan) { if (audioClip == null) { Debug.LogWarning("playSound was called with a null AudioClip."); return(null); } // Find the first SKSound not being used. if they are all in use, create a new one SKSound sound = nextAvailableSound(); sound.playAudioClip(audioClip, volumeScale * soundEffectVolume, pitch, pan, sfxGroup); return(sound); }
/// <summary> /// fetches any AudioSource it can find and uses the standard PlayOneShot to play. Use this if you don't require any /// extra control over a clip and don't care about when it completes. It avoids the call to StartCoroutine. /// nb. pan/pitch are not supported as the chosen AudioSource might be in use with another pan/pitch setting and Unity does not support setting /// them natively in PlayOneShot, so updating them here can result in bad audio. /// </summary> /// <param name="audioClip">Audio clip.</param> /// <param name="volumeScale">Volume scale.</param> public void playOneShot(AudioClip audioClip, float volumeScale = 1f) { if (audioClip == null) { Debug.LogWarning("playOneShot was called with a null AudioClip."); return; } if (oneShotSound == null) { oneShotSound = new SKSound(this); } oneShotSound.audioSource.outputAudioMixerGroup = sfxGroup; oneShotSound.audioSource.PlayOneShot(audioClip, volumeScale * soundEffectVolume); }
/// <summary> /// fetches any AudioSource it can find and uses the standard PlayOneShot to play. Use this if you don't require any /// extra control over a clip and don't care about when it completes. It avoids the call to StartCoroutine. /// nb. pan/pitch are not supported as the chosen AudioSource might be in use with another pan/pitch setting and Unity does not support setting /// them natively in PlayOneShot, so updating them here can result in bad audio. /// </summary> /// <param name="audioClip">Audio clip.</param> /// <param name="volumeScale">Volume scale.</param> public void PlayOneShot(AudioClip audioClip, float volumeScale = 1f) { if (audioClip == null) { Debug.LogError("Null sound played"); return; } if (OneShotSound == null) { OneShotSound = new SKSound(this); } OneShotSound.AudioSource.PlayOneShot(audioClip, volumeScale * SoundVolume); }
/// <summary> /// loops the AudioClip. Do note that you are responsible for calling either stop or fadeOutAndStop on the SKSound /// or it will not be recycled /// </summary> /// <returns>The sound looped.</returns> /// <param name="audioClip">Audio clip.</param> public SKSound playSoundLooped(AudioClip audioClip) { if (audioClip == null) { Debug.LogWarning("playSoundLooped was called with a null AudioClip."); return(null); } // find the first SKSound not being used. if they are all in use, create a new one SKSound sound = nextAvailableSound(); sound.playAudioClip(audioClip, soundEffectVolume, 1f, 0f, sfxGroup); sound.setLoop(true); return(sound); }
/// <summary> /// loops the AudioClip. Do note that you are responsible for calling either stop or fadeOutAndStop on the SKSound /// or it will not be recycled /// </summary> /// <returns>The sound looped.</returns> /// <param name="audioClip">Audio clip.</param> public SKSound PlaySoundLooped(AudioClip audioClip) { if (audioClip == null) { Debug.LogError("Null sound played"); return(null); } // find the first SKSound not being used. if they are all in use, create a new one SKSound sound = NextAvailableSound(); sound.playAudioClip(audioClip, SoundVolume, 1f, 0f); sound.setLoop(true); return(sound); }
/// <summary> /// starts up the background music and optionally loops it. You can access the SKSound via the BackgroundSound field. /// </summary> /// <param name="audioClip">Audio clip.</param> /// <param name="volume">Volume scale.</param> /// <param name="loop">If set to <c>true</c> loop.</param> public void PlayBackgroundMusic(AudioClip audioClip, float volume = 1.0f, bool loop = true) { if (audioClip == null) { Debug.LogError("Null sound played"); return; } if (BackgroundSound == null) { BackgroundSound = new SKSound(this); } BackgroundVolume = volume; BackgroundSound.playAudioClip(audioClip, volume * MusicVolume, 1f, 0f); BackgroundSound.setLoop(loop); }
/// <summary> /// fetches the next available sound and adds the sound to the _playingSounds List /// </summary> /// <returns>The available sound.</returns> private SKSound nextAvailableSound() { SKSound sound = null; if (_availableSounds.Count > 0) { sound = _availableSounds.Pop(); } // if we didnt find an available found, bail out if (sound == null) { sound = new SKSound(this); } _playingSounds.Add(sound); return(sound); }
void Update() { for (int index = playingSounds.Count - 1; index >= 0; index--) { SKSound sound = playingSounds[index]; if (sound.playingLoopingAudio) { continue; } sound.elapsedTime += Time.deltaTime; if (sound.elapsedTime > sound.audioSource.clip.length) { sound.Stop(); } } }
private SKSound NextAvailableSound() { SKSound sound = null; if (availableSounds.Count > 0) { sound = availableSounds.Pop(); } if (sound == null) { sound = new SKSound(this); } playingSounds.Add(sound); return(sound); }
/// <summary> /// fetches any AudioSource it can find and uses the standard PlayOneShot to play. Use this if you don't require any /// extra control over a clip and don't care about when it completes. It avoids the call to StartCoroutine. /// nb. pan/pitch are not supported as the chosen AudioSource might be in use with another pan/pitch setting and Unity does not support setting /// them natively in PlayOneShot, so updating them here can result in bad audio. /// </summary> /// <param name="audioClip">Audio clip.</param> /// <param name="volumeScale">Volume scale.</param> public void playOneShot( AudioClip audioClip, float volumeScale = 1f ) { if (audioClip == null) throw new ArgumentNullException("audioClip"); if (oneShotSound == null) oneShotSound = new SKSound(this); oneShotSound.audioSource.PlayOneShot( audioClip, volumeScale * soundEffectVolume ); }
/// <summary> /// used internally to recycle SKSounds and their AudioSources /// </summary> /// <param name="sound">Sound.</param> public void recycleSound( SKSound sound ) { // we dont recycle the backgroundSound since it always stays alive if( sound == backgroundSound ) return; var index = 0; while( index < _playingSounds.Count ) { if( _playingSounds[index] == sound ) break; index++; } _playingSounds.RemoveAt( index ); // if we are already over capacity dont recycle this sound but destroy it instead if( _availableSounds.Count + _playingSounds.Count >= maxCapacity ) Destroy( sound.audioSource ); else _availableSounds.Push( sound ); }
/// <summary> /// fetches the next available sound and adds the sound to the _playingSounds List /// </summary> /// <returns>The available sound.</returns> private SKSound nextAvailableSound() { SKSound sound = null; if( _availableSounds.Count > 0 ) sound = _availableSounds.Pop(); // if we didnt find an available found, bail out if( sound == null ) sound = new SKSound( this ); _playingSounds.Add( sound ); return sound; }