UIPlaySound.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using UnityEngine;
  3. [AddComponentMenu("NGUI/Interaction/Play Sound")]
  4. public class UIPlaySound : MonoBehaviour
  5. {
  6. private bool canPlay
  7. {
  8. get
  9. {
  10. if (!base.enabled)
  11. {
  12. return false;
  13. }
  14. UIButton component = base.GetComponent<UIButton>();
  15. return component == null || component.isEnabled;
  16. }
  17. }
  18. private void OnEnable()
  19. {
  20. if (this.trigger == UIPlaySound.Trigger.OnEnable)
  21. {
  22. NGUITools.PlaySound(this.audioClip, this.volume, this.pitch);
  23. }
  24. }
  25. private void OnDisable()
  26. {
  27. if (this.trigger == UIPlaySound.Trigger.OnDisable)
  28. {
  29. NGUITools.PlaySound(this.audioClip, this.volume, this.pitch);
  30. }
  31. }
  32. private void OnHover(bool isOver)
  33. {
  34. if (this.trigger == UIPlaySound.Trigger.OnMouseOver)
  35. {
  36. if (this.mIsOver == isOver)
  37. {
  38. return;
  39. }
  40. this.mIsOver = isOver;
  41. }
  42. if (this.canPlay && ((isOver && this.trigger == UIPlaySound.Trigger.OnMouseOver) || (!isOver && this.trigger == UIPlaySound.Trigger.OnMouseOut)))
  43. {
  44. NGUITools.PlaySound(this.audioClip, this.volume, this.pitch);
  45. }
  46. }
  47. private void OnPress(bool isPressed)
  48. {
  49. if (this.trigger == UIPlaySound.Trigger.OnPress)
  50. {
  51. if (this.mIsOver == isPressed)
  52. {
  53. return;
  54. }
  55. this.mIsOver = isPressed;
  56. }
  57. if (this.canPlay && ((isPressed && this.trigger == UIPlaySound.Trigger.OnPress) || (!isPressed && this.trigger == UIPlaySound.Trigger.OnRelease)))
  58. {
  59. NGUITools.PlaySound(this.audioClip, this.volume, this.pitch);
  60. }
  61. }
  62. private void OnClick()
  63. {
  64. if (this.canPlay && this.trigger == UIPlaySound.Trigger.OnClick)
  65. {
  66. NGUITools.PlaySound(this.audioClip, this.volume, this.pitch);
  67. }
  68. }
  69. private void OnSelect(bool isSelected)
  70. {
  71. if (this.canPlay && (!isSelected || UICamera.currentScheme == UICamera.ControlScheme.Controller))
  72. {
  73. this.OnHover(isSelected);
  74. }
  75. }
  76. public void Play()
  77. {
  78. NGUITools.PlaySound(this.audioClip, this.volume, this.pitch);
  79. }
  80. public AudioClip audioClip;
  81. public UIPlaySound.Trigger trigger;
  82. [Range(0f, 1f)]
  83. public float volume = 1f;
  84. [Range(0f, 2f)]
  85. public float pitch = 1f;
  86. private bool mIsOver;
  87. public enum Trigger
  88. {
  89. OnClick,
  90. OnMouseOver,
  91. OnMouseOut,
  92. OnPress,
  93. OnRelease,
  94. Custom,
  95. OnEnable,
  96. OnDisable
  97. }
  98. }