Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SFXManager : MonoBehaviour {
- //ui sfx
- [SerializeField]
- private AudioSource uiSFX;
- //ui clips
- [SerializeField]
- private AudioClip sliderClick;
- [SerializeField]
- private AudioClip weaponRecharged;
- [SerializeField]
- private SFX buttonClicks;
- [SerializeField]
- private SFX slideOpen;
- [SerializeField]
- private SFX slideClose;
- private void OnEnable()
- {
- SliderButton_UI.playClick += PlayerSliderClick;
- WeaponPanel_UI.weaponCharged += WeaponCharged;
- ButtonClick.playButtonSFX += PlayUIButtonClick;
- SlideOutFromRight.slideOpen += PlaySlideOpen;
- SlideOutFromRight.slideClose += PlaySlideClosed;
- }
- private void OnDisable()
- {
- SliderButton_UI.playClick -= PlayerSliderClick;
- WeaponPanel_UI.weaponCharged -= WeaponCharged;
- ButtonClick.playButtonSFX -= PlayUIButtonClick;
- SlideOutFromRight.slideOpen -= PlaySlideOpen;
- SlideOutFromRight.slideClose -= PlaySlideClosed;
- }
- private void PlayerSliderClick()
- {
- if(sliderClick != null)
- {
- uiSFX.volume = .5f + Random.Range(-0.05f,0.05f);
- uiSFX.pitch = .9f + Random.Range(-0.05f, 0.05f);
- uiSFX.clip = sliderClick;
- uiSFX.Play();
- }
- }
- private void WeaponCharged(WeaponPanel_UI weapon)
- {
- //make fancier to select different sounds based on weapon?
- PlayWeaponRecharge();
- }
- private void PlayWeaponRecharge()
- {
- if (weaponRecharged != null)
- {
- uiSFX.volume = .9f + Random.Range(-0.05f, 0.05f);
- uiSFX.pitch = .9f + Random.Range(-0.05f, 0.05f);
- uiSFX.clip = weaponRecharged;
- uiSFX.Play();
- }
- }
- private void PlayUIButtonClick()
- {
- buttonClicks.PlaySFX(uiSFX, false);
- }
- private void PlaySlideOpen()
- {
- slideOpen.PlaySFX(uiSFX, false);
- }
- private void PlaySlideClosed()
- {
- slideClose.PlaySFX(uiSFX, false);
- }
- }
- [System.Serializable]
- public class SFX
- {
- public List<AudioClip> clips;
- [Range(0f,1f)]
- public float volume = 1f;
- [Range(0f, 0.2f)]
- public float volumeVariation = 0.05f;
- [Range(0f, 2f)]
- public float pitch = 1f;
- [Range(0f, 0.2f)]
- public float pitchVariation = 0.05f;
- public void PlaySFX(AudioSource source, bool waitToFinish = true)
- {
- if (!source.isPlaying || !waitToFinish)
- {
- source.clip = GetRandomClip(this);
- source.volume = this.volume + Random.Range(-volumeVariation, volumeVariation);
- source.pitch = this.pitch + Random.Range(-pitchVariation, pitchVariation);
- source.Play();
- }
- }
- private AudioClip GetRandomClip(SFX sfx)
- {
- if (sfx.clips.Count > 0)
- return sfx.clips[Random.Range(0, sfx.clips.Count - 1)];
- else
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement