daily pastebin goal
42%
SHARE
TWEET

Untitled

a guest Jul 11th, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class SFXManager : MonoBehaviour {
  2.  
  3.     //ui sfx
  4.     [SerializeField]
  5.     private AudioSource uiSFX;
  6.  
  7.     //ui clips
  8.     [SerializeField]
  9.     private AudioClip sliderClick;
  10.     [SerializeField]
  11.     private AudioClip weaponRecharged;
  12.  
  13.     [SerializeField]
  14.     private SFX buttonClicks;
  15.     [SerializeField]
  16.     private SFX slideOpen;
  17.     [SerializeField]
  18.     private SFX slideClose;
  19.  
  20.     private void OnEnable()
  21.     {
  22.         SliderButton_UI.playClick += PlayerSliderClick;
  23.         WeaponPanel_UI.weaponCharged += WeaponCharged;
  24.         ButtonClick.playButtonSFX += PlayUIButtonClick;
  25.         SlideOutFromRight.slideOpen += PlaySlideOpen;
  26.         SlideOutFromRight.slideClose += PlaySlideClosed;
  27.     }
  28.  
  29.     private void OnDisable()
  30.     {
  31.         SliderButton_UI.playClick -= PlayerSliderClick;
  32.         WeaponPanel_UI.weaponCharged -= WeaponCharged;
  33.         ButtonClick.playButtonSFX -= PlayUIButtonClick;
  34.         SlideOutFromRight.slideOpen -= PlaySlideOpen;
  35.         SlideOutFromRight.slideClose -= PlaySlideClosed;
  36.     }
  37.  
  38.     private void PlayerSliderClick()
  39.     {
  40.         if(sliderClick != null)
  41.         {
  42.             uiSFX.volume = .5f + Random.Range(-0.05f,0.05f);
  43.             uiSFX.pitch = .9f + Random.Range(-0.05f, 0.05f);
  44.             uiSFX.clip = sliderClick;
  45.             uiSFX.Play();
  46.         }
  47.     }
  48.  
  49.     private void WeaponCharged(WeaponPanel_UI weapon)
  50.     {
  51.         //make fancier to select different sounds based on weapon?
  52.         PlayWeaponRecharge();
  53.     }
  54.  
  55.     private void PlayWeaponRecharge()
  56.     {
  57.         if (weaponRecharged != null)
  58.         {
  59.             uiSFX.volume = .9f + Random.Range(-0.05f, 0.05f);
  60.             uiSFX.pitch = .9f + Random.Range(-0.05f, 0.05f);
  61.             uiSFX.clip = weaponRecharged;
  62.             uiSFX.Play();
  63.         }
  64.     }
  65.  
  66.     private void PlayUIButtonClick()
  67.     {
  68.         buttonClicks.PlaySFX(uiSFX, false);
  69.     }
  70.  
  71.     private void PlaySlideOpen()
  72.     {
  73.         slideOpen.PlaySFX(uiSFX, false);
  74.     }
  75.  
  76.     private void PlaySlideClosed()
  77.     {
  78.         slideClose.PlaySFX(uiSFX, false);
  79.     }
  80. }
  81.  
  82. [System.Serializable]
  83. public class SFX
  84. {
  85.     public List<AudioClip> clips;
  86.     [Range(0f,1f)]
  87.     public float volume = 1f;
  88.     [Range(0f, 0.2f)]
  89.     public float volumeVariation = 0.05f;
  90.     [Range(0f, 2f)]
  91.     public float pitch = 1f;
  92.     [Range(0f, 0.2f)]
  93.     public float pitchVariation = 0.05f;
  94.  
  95.     public void PlaySFX(AudioSource source, bool waitToFinish = true)
  96.     {
  97.         if (!source.isPlaying || !waitToFinish)
  98.         {
  99.             source.clip = GetRandomClip(this);
  100.             source.volume = this.volume + Random.Range(-volumeVariation, volumeVariation);
  101.             source.pitch = this.pitch + Random.Range(-pitchVariation, pitchVariation);
  102.             source.Play();
  103.         }
  104.     }
  105.  
  106.     private AudioClip GetRandomClip(SFX sfx)
  107.     {
  108.         if (sfx.clips.Count > 0)
  109.             return sfx.clips[Random.Range(0, sfx.clips.Count - 1)];
  110.         else
  111.             return null;
  112.     }
  113.  
  114. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top