Skip to content

Instantly share code, notes, and snippets.

@empika
Last active August 29, 2015 14:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save empika/da862c4dd59910800c65 to your computer and use it in GitHub Desktop.
Save empika/da862c4dd59910800c65 to your computer and use it in GitHub Desktop.
Add Confirm and cancel handling to a Unity UI element
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
[Serializable]
public class ConfirmCancelEntry
{
public ConfirmCancelTriggerType eventID = ConfirmCancelTriggerType.Submit;
public EventTrigger.TriggerEvent callback = new EventTrigger.TriggerEvent ();
}
public enum ConfirmCancelTriggerType
{
Submit,
Cancel
}
public class OnConfirmCancel : MonoBehaviour, ISubmitHandler, ICancelHandler
{
public EventSystem System;
public List<ConfirmCancelEntry> delegates;
public void OnSubmit(BaseEventData data)
{
this.Execute (ConfirmCancelTriggerType.Submit, data);
}
public void OnCancel (BaseEventData data)
{
this.Execute (ConfirmCancelTriggerType.Cancel, data);
}
private void Execute (ConfirmCancelTriggerType id, BaseEventData eventData)
{
if (this.delegates != null)
{
int i = 0;
int count = this.delegates.Count;
while (i < count)
{
ConfirmCancelEntry entry = this.delegates [i];
if (entry.eventID == id && entry.callback != null)
{
entry.callback.Invoke (eventData);
}
i++;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment