using System; using UnityEngine; [AddComponentMenu("NGUI/Interaction/Play Sound")] public class UIPlaySound : MonoBehaviour { private bool canPlay { get { if (!base.enabled) { return false; } UIButton component = base.GetComponent(); return component == null || component.isEnabled; } } private void OnEnable() { if (this.trigger == UIPlaySound.Trigger.OnEnable) { NGUITools.PlaySound(this.audioClip, this.volume, this.pitch); } } private void OnDisable() { if (this.trigger == UIPlaySound.Trigger.OnDisable) { NGUITools.PlaySound(this.audioClip, this.volume, this.pitch); } } private void OnHover(bool isOver) { if (this.trigger == UIPlaySound.Trigger.OnMouseOver) { if (this.mIsOver == isOver) { return; } this.mIsOver = isOver; } if (this.canPlay && ((isOver && this.trigger == UIPlaySound.Trigger.OnMouseOver) || (!isOver && this.trigger == UIPlaySound.Trigger.OnMouseOut))) { NGUITools.PlaySound(this.audioClip, this.volume, this.pitch); } } private void OnPress(bool isPressed) { if (this.trigger == UIPlaySound.Trigger.OnPress) { if (this.mIsOver == isPressed) { return; } this.mIsOver = isPressed; } if (this.canPlay && ((isPressed && this.trigger == UIPlaySound.Trigger.OnPress) || (!isPressed && this.trigger == UIPlaySound.Trigger.OnRelease))) { NGUITools.PlaySound(this.audioClip, this.volume, this.pitch); } } private void OnClick() { if (this.canPlay && this.trigger == UIPlaySound.Trigger.OnClick) { NGUITools.PlaySound(this.audioClip, this.volume, this.pitch); } } private void OnSelect(bool isSelected) { if (this.canPlay && (!isSelected || UICamera.currentScheme == UICamera.ControlScheme.Controller)) { this.OnHover(isSelected); } } public void Play() { NGUITools.PlaySound(this.audioClip, this.volume, this.pitch); } public AudioClip audioClip; public UIPlaySound.Trigger trigger; [Range(0f, 1f)] public float volume = 1f; [Range(0f, 2f)] public float pitch = 1f; private bool mIsOver; public enum Trigger { OnClick, OnMouseOver, OnMouseOut, OnPress, OnRelease, Custom, OnEnable, OnDisable } }