Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- public enum SoundEffectChannels
- {
- StartRolling,
- Rolling,
- PebbleCollision,
- PebbleCollision_OtherPebble,
- ProjectileBreak,
- AttachmentBlowOff,
- Explosion,
- LaunchSound,
- DescendSound,
- TaftSound,
- BoingSound,
- SpeedFlowerBoostSound,
- Pickup,
- Swipe,
- Others,
- MonsterKilled,
- MonsterHit
- }
- public class SoundEffectPlayer : MonoBehaviour
- {
- private static SoundEffectPlayer _player = null;
- public static SoundEffectPlayer Instance
- {
- get
- {
- if(_player == null)
- {
- GameObject go = new GameObject("SoundEffectPlayer");
- _player = go.AddComponent<SoundEffectPlayer>();
- DontDestroyOnLoad(go);
- }
- return _player;
- }
- }
- /// <summary>
- /// Channel instance. Just holds cooldown and timer
- /// </summary>
- public class PlayChannel
- {
- public float Cooldown = 0f;
- public float Timer = 0.1f;
- }
- /// <summary>
- /// Holds all active channels
- /// </summary>
- Dictionary<SoundEffectChannels, PlayChannel> _channels = new Dictionary<SoundEffectChannels, PlayChannel>();
- /// <summary>
- /// Plays a sound effect using a cooldown to avoid spam
- /// </summary>
- /// <param name="cooldown">
- /// A <see cref="System.Single"/>
- /// </param>
- /// <param name="clip">
- /// A <see cref="AudioClip"/>
- /// </param>
- /// <param name="channel">
- /// A <see cref="System.Int32"/>
- /// </param>
- public void PlayWithCooldown(float cooldown, AudioClip clip, SoundEffectChannels channel, float volume)
- {
- PlayWithCooldown(cooldown, clip, channel, Vector3.zero, volume);
- }
- /// <summary>
- /// Plays a sound effect using a cooldown to avoid spam
- /// </summary>
- /// <param name="cooldown">
- /// A <see cref="System.Single"/>
- /// </param>
- /// <param name="clip">
- /// A <see cref="AudioClip"/>
- /// </param>
- /// <param name="channel">
- /// A <see cref="System.Int32"/>
- /// </param>
- public void PlayWithCooldown(float cooldown, AudioClip clip, SoundEffectChannels channel)
- {
- PlayWithCooldown(cooldown, clip, channel, Vector3.zero, 1f);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="cooldown">
- /// A <see cref="System.Single"/>
- /// </param>
- /// <param name="clip">
- /// A <see cref="AudioClip"/>
- /// </param>
- /// <param name="channel">
- /// A <see cref="SoundEffectChannels"/>
- /// </param>
- /// <param name="position">
- /// A <see cref="Vector3"/>
- /// </param>
- public void PlayWithCooldown(float cooldown, AudioClip clip, SoundEffectChannels channel, Vector3 position, float volume)
- {
- if(clip == null)
- {
- Debug.LogWarning("Trying to play null-sound in channel " + channel.ToString());
- return;
- }
- bool play = false;
- if(!_channels.ContainsKey(channel))
- {
- _channels[channel] = new SoundEffectPlayer.PlayChannel();
- play = true;
- }
- else if(_channels[channel].Timer >= _channels[channel].Cooldown)
- {
- play = true;
- }
- // Reset play when playing
- if(play)
- {
- AudioSource.PlayClipAtPoint(clip, position, volume);
- _channels[channel].Cooldown = cooldown;
- _channels[channel].Timer = 0f;
- }
- }
- /// <summary>
- /// Update timers in all channels
- /// </summary>
- void Update()
- {
- foreach(PlayChannel channel in _channels.Values)
- {
- channel.Timer += Time.smoothDeltaTime;
- }
- }
- }
Add Comment
Please, Sign In to add comment