Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MonoBehaviour that Connects WWise AKEvents to Unity IEventSystemHandler
using UnityEngine;
using UnityEngine.EventSystems;
namespace RPG.Audio
{
[AddComponentMenu("UI/Audio/Play Audio WWise")]
public class UIPlayAudioWWise : MonoBehaviour, IEventSystemHandler, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler
{
public enum Event
{
None,
PointerEnter,
PointerExit,
PointerDown,
PointerUp,
Click,
DoubleClick
}
[SerializeField] private Event m_PlayOnEvent = Event.None;
public AK.Wwise.Event m_AkSoundEvent = new AK.Wwise.Event();
/// <summary>
/// Gets or sets the event on which the audio clip should be played.
/// </summary>
public Event playOnEvent { get { return this.m_PlayOnEvent; } set { this.m_PlayOnEvent = value; } }
/// <summary>
/// Gets or sets the audio clip.
/// </summary>
public AK.Wwise.Event audioClip { get { return this.m_AkSoundEvent; } set { this.m_AkSoundEvent = value; } }
private bool m_Pressed = false;
public void OnPointerEnter(PointerEventData eventData)
{
if (!this.m_Pressed)
this.TriggerEvent(Event.PointerEnter);
}
public void OnPointerExit(PointerEventData eventData)
{
if (!this.m_Pressed)
this.TriggerEvent(Event.PointerExit);
}
public void OnPointerDown(PointerEventData eventData)
{
if (eventData.button != PointerEventData.InputButton.Left)
return;
this.TriggerEvent(Event.PointerDown);
this.m_Pressed = true;
}
public void OnPointerUp(PointerEventData eventData)
{
if (eventData.button != PointerEventData.InputButton.Left)
return;
this.TriggerEvent(Event.PointerUp);
if (this.m_Pressed)
{
if (eventData.clickCount > 1)
{
this.TriggerEvent(Event.DoubleClick);
eventData.clickCount = 0;
}
else
{
this.TriggerEvent(Event.Click);
}
}
this.m_Pressed = false;
}
private void TriggerEvent(Event e)
{
if (e == this.m_PlayOnEvent)
{
this.PlayAudio();
}
}
public void PlayAudio()
{
if (!this.enabled || !this.gameObject.activeInHierarchy)
{
return;
}
if (this.m_AkSoundEvent == null)
{
return;
}
m_AkSoundEvent.Post(gameObject);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.