123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- 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<UIButton>();
- 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
- }
- }
|