I’m using NGUI and want to play a audio clip after hitting a button. But I want it to disable that button during the playing otherwise if you keep pressing the button over and over again you get the clip playing over itself. Also if I hit any other button I’d like the audio to stop playing … here is the script I’m working with
using UnityEngine;
///
/// Plays the specified sound.
///
[AddComponentMenu(“NGUI/Interaction/Button Sound”)]
public class UIButtonSound : MonoBehaviour
{
public enum Trigger
{
OnClick,
OnMouseOver,
OnMouseOut,
OnPress,
OnRelease,
}
public AudioClip audioClip;
public Trigger trigger = Trigger.OnClick;
public float volume = 1f;
void OnHover (bool isOver)
{
if (enabled && ((isOver && trigger == Trigger.OnMouseOver) || (!isOver && trigger == Trigger.OnMouseOut)))
{
NGUITools.PlaySound(audioClip, volume);
}
}
void OnPress (bool isPressed)
{
if (enabled && ((isPressed && trigger == Trigger.OnPress) || (!isPressed && trigger == Trigger.OnRelease)))
{
NGUITools.PlaySound(audioClip, volume);
}
}
void OnClick ()
{
if (enabled && trigger == Trigger.OnClick)
{
NGUITools.PlaySound(audioClip, volume);
}
}
}