Skip to content

Instantly share code, notes, and snippets.

@FullStackForger
Created April 15, 2015 19:42
Show Gist options
  • Save FullStackForger/03653ed74dedd4b5cc33 to your computer and use it in GitHub Desktop.
Save FullStackForger/03653ed74dedd4b5cc33 to your computer and use it in GitHub Desktop.
Registers custom handles for one custom or all UI events ( Unity3d )
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using System.Collections;
// inspired by: https://github.com/AyARL/UnityGUIExamples/blob/master/EventTrigger/Assets/TriggerSetup.cs
// gist: https://gist.github.com/rusticode/05b306f11aa4a33bb113
[RequireComponent(typeof(RectTransform))]
[RequireComponent(typeof(EventTrigger))]
[AddComponentMenu("BGM/State Switch")]
public class EventLogger : MonoBehaviour {
public bool captureAllEvents = true;
public EventTriggerType triggerType = EventTriggerType.PointerClick;
private EventTrigger eventTrigger = null;
// Use this for initialization
void Start()
{
eventTrigger = gameObject.GetComponent<EventTrigger>();
RegisterHandler(EventTriggerType.PointerClick, OnPointerClick);
RegisterHandler(EventTriggerType.PointerEnter, OnPointerEnter);
RegisterHandler(EventTriggerType.PointerExit, OnPointerExit);
RegisterHandler(EventTriggerType.PointerDown, OnPointerDown);
RegisterHandler(EventTriggerType.PointerUp, OnPointerUp);
RegisterHandler(EventTriggerType.Drag, OnDrag);
RegisterHandler(EventTriggerType.Drop, OnDrop);
RegisterHandler(EventTriggerType.Scroll, OnScroll);
RegisterHandler(EventTriggerType.UpdateSelected, OnUpdateSelected);
RegisterHandler(EventTriggerType.Select, OnSelect);
RegisterHandler(EventTriggerType.Deselect, OnDeselect);
RegisterHandler(EventTriggerType.Move, OnMove);
}
#region Helpers
// Use listener that uses the BaseEventData passed to the Trigger
private void RegisterHandler(EventTriggerType triggerType, UnityAction<BaseEventData> action)
{
// Quit if trigger type is not to be handled
if (!shouldHandle(triggerType)) {
return;
}
// Create a nee TriggerEvent and add a listener
EventTrigger.TriggerEvent trigger = new EventTrigger.TriggerEvent();
// capture and pass the event data to the listener
trigger.AddListener((eventData) => action(eventData));
// Create and initialise EventTrigger.Entry using the created TriggerEvent
EventTrigger.Entry entry = new EventTrigger.Entry() { callback = trigger, eventID = triggerType };
// Add the EventTrigger.Entry to delegates list on the EventTrigger
eventTrigger.delegates.Add(entry);
}
private bool shouldHandle(EventTriggerType eventTriggerType) {
return captureAllEvents || triggerType == eventTriggerType;
}
#endregion
#region Callbacks
private void OnPointerEnter(BaseEventData data)
{
if (shouldHandle(EventTriggerType.PointerEnter)) {
Debug.Log ("OnPointerEnter" + data.selectedObject);
}
}
private void OnPointerExit(BaseEventData data)
{
if (shouldHandle(EventTriggerType.PointerExit)) {
Debug.Log ("OnPointerExit" + data.selectedObject);
}
}
private void OnPointerDown(BaseEventData data)
{
if (shouldHandle(EventTriggerType.PointerDown)) {
Debug.Log ("OnPointerDown" + data.selectedObject);
}
}
private void OnPointerUp(BaseEventData data)
{
if (shouldHandle(EventTriggerType.PointerUp)) {
Debug.Log ("OnPointerUp" + data.selectedObject);
}
}
private void OnPointerClick(BaseEventData data)
{
if (shouldHandle(EventTriggerType.PointerClick)) {
Debug.Log ("OnPointerClick " + data.selectedObject);
}
}
private void OnDrag(BaseEventData data)
{
if (shouldHandle(EventTriggerType.Drag)) {
Debug.Log ("OnDrag" + data.selectedObject);
}
}
private void OnDrop(BaseEventData data)
{
if (shouldHandle(EventTriggerType.Drop)) {
Debug.Log ("OnDrop" + data.selectedObject);
}
}
private void OnScroll(BaseEventData data)
{
if (shouldHandle(EventTriggerType.Scroll)) {
Debug.Log ("OnScroll" + data.selectedObject);
}
}
private void OnUpdateSelected(BaseEventData data)
{
if (shouldHandle(EventTriggerType.UpdateSelected)) {
Debug.Log ("OnUpdateSelected" + data.selectedObject.GetInstanceID());
}
}
private void OnSelect(BaseEventData data)
{
if (shouldHandle(EventTriggerType.Select)) {
Debug.Log ("OnSelect" + data.selectedObject);
}
}
private void OnDeselect(BaseEventData data)
{
if (shouldHandle(EventTriggerType.Deselect)) {
Debug.Log ("OnDeselect" + data.selectedObject);
}
}
private void OnMove(BaseEventData data)
{
if (shouldHandle(EventTriggerType.Move)) {
Debug.Log ("Move" + data.selectedObject);
}
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment