Guest User

Untitled

a guest
May 21st, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.29 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. public enum SoundEffectChannels
  5. {
  6.     StartRolling,
  7.     Rolling,
  8.     PebbleCollision,
  9.     PebbleCollision_OtherPebble,
  10.     ProjectileBreak,
  11.     AttachmentBlowOff,
  12.     Explosion,
  13.     LaunchSound,
  14.     DescendSound,
  15.     TaftSound,
  16.     BoingSound,
  17.     SpeedFlowerBoostSound,
  18.     Pickup,
  19.     Swipe,
  20.     Others,
  21.     MonsterKilled,
  22.     MonsterHit
  23. }
  24.  
  25. public class SoundEffectPlayer : MonoBehaviour
  26. {
  27.     private static SoundEffectPlayer _player = null;
  28.     public static SoundEffectPlayer Instance
  29.     {
  30.         get
  31.         {
  32.             if(_player == null)
  33.             {
  34.                 GameObject go = new GameObject("SoundEffectPlayer");
  35.                 _player = go.AddComponent<SoundEffectPlayer>();
  36.                 DontDestroyOnLoad(go);
  37.             }
  38.        
  39.             return _player;
  40.         }
  41.     }
  42.    
  43.    
  44.     /// <summary>
  45.     /// Channel instance. Just holds cooldown and timer
  46.     /// </summary>
  47.     public class PlayChannel
  48.     {
  49.         public float Cooldown = 0f;
  50.         public float Timer = 0.1f;
  51.     }
  52.    
  53.     /// <summary>
  54.     /// Holds all active channels
  55.     /// </summary>
  56.     Dictionary<SoundEffectChannels, PlayChannel> _channels = new Dictionary<SoundEffectChannels, PlayChannel>();
  57.  
  58.         /// <summary>
  59.     /// Plays a sound effect using a cooldown to avoid spam
  60.     /// </summary>
  61.     /// <param name="cooldown">
  62.     /// A <see cref="System.Single"/>
  63.     /// </param>
  64.     /// <param name="clip">
  65.     /// A <see cref="AudioClip"/>
  66.     /// </param>
  67.     /// <param name="channel">
  68.     /// A <see cref="System.Int32"/>
  69.     /// </param>
  70.     public void PlayWithCooldown(float cooldown, AudioClip clip, SoundEffectChannels channel, float volume)
  71.     {
  72.         PlayWithCooldown(cooldown, clip, channel, Vector3.zero, volume);
  73.     }
  74.    
  75.     /// <summary>
  76.     /// Plays a sound effect using a cooldown to avoid spam
  77.     /// </summary>
  78.     /// <param name="cooldown">
  79.     /// A <see cref="System.Single"/>
  80.     /// </param>
  81.     /// <param name="clip">
  82.     /// A <see cref="AudioClip"/>
  83.     /// </param>
  84.     /// <param name="channel">
  85.     /// A <see cref="System.Int32"/>
  86.     /// </param>
  87.     public void PlayWithCooldown(float cooldown, AudioClip clip, SoundEffectChannels channel)
  88.     {
  89.         PlayWithCooldown(cooldown, clip, channel, Vector3.zero, 1f);
  90.     }
  91.    
  92.     /// <summary>
  93.     ///
  94.     /// </summary>
  95.     /// <param name="cooldown">
  96.     /// A <see cref="System.Single"/>
  97.     /// </param>
  98.     /// <param name="clip">
  99.     /// A <see cref="AudioClip"/>
  100.     /// </param>
  101.     /// <param name="channel">
  102.     /// A <see cref="SoundEffectChannels"/>
  103.     /// </param>
  104.     /// <param name="position">
  105.     /// A <see cref="Vector3"/>
  106.     /// </param>
  107.     public void PlayWithCooldown(float cooldown, AudioClip clip, SoundEffectChannels channel, Vector3 position, float volume)
  108.     {
  109.         if(clip == null)
  110.         {
  111.             Debug.LogWarning("Trying to play null-sound in channel " + channel.ToString());
  112.             return;
  113.         }
  114.            
  115.         bool play = false;
  116.         if(!_channels.ContainsKey(channel))
  117.         {
  118.             _channels[channel] = new SoundEffectPlayer.PlayChannel();
  119.             play = true;
  120.         }
  121.         else if(_channels[channel].Timer >= _channels[channel].Cooldown)
  122.         {
  123.             play = true;
  124.         }
  125.    
  126.         // Reset play when playing
  127.         if(play)
  128.         {
  129.             AudioSource.PlayClipAtPoint(clip, position, volume);
  130.             _channels[channel].Cooldown = cooldown;
  131.             _channels[channel].Timer = 0f;     
  132.         }
  133.     }
  134.        
  135.     /// <summary>
  136.     /// Update timers in all channels
  137.     /// </summary>
  138.     void Update()
  139.     {
  140.         foreach(PlayChannel channel in _channels.Values)
  141.         {
  142.             channel.Timer += Time.smoothDeltaTime;
  143.         }
  144.     }
  145. }
Add Comment
Please, Sign In to add comment