UIButton.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.EventSystems;
  6. namespace SC
  7. {
  8. [RequireComponent(typeof(BoxCollider))]
  9. [AddComponentMenu("ShadowCreator/UIButton")]
  10. public class UIButton : Button{
  11. public static AudioClip clickAudio;
  12. public static AudioClip enterAudio;
  13. private AudioSource mAudioSource;
  14. enum AudioType {
  15. Enter,
  16. Click,
  17. }
  18. protected override void Awake() {
  19. base.Awake();
  20. clickAudio = Resources.Load<AudioClip>("Sound/btnSound");
  21. enterAudio = Resources.Load<AudioClip>("Sound/mouseOver");
  22. mAudioSource = GetComponent<AudioSource>();
  23. if(!mAudioSource) {
  24. mAudioSource = gameObject.AddComponent<AudioSource>();
  25. mAudioSource.playOnAwake = false;
  26. }
  27. }
  28. void PlayAudio( AudioType type ) {
  29. if(type == AudioType.Click && clickAudio) {
  30. mAudioSource.clip = clickAudio;
  31. mAudioSource.Play();
  32. } else if(type == AudioType.Enter && enterAudio) {
  33. mAudioSource.clip = enterAudio;
  34. mAudioSource.Play();
  35. }
  36. }
  37. public override void OnPointerClick( PointerEventData eventData ) {
  38. base.OnPointerClick(eventData);
  39. PlayAudio(AudioType.Click);
  40. }
  41. }
  42. }