Last active
August 29, 2015 14:05
-
-
Save empika/da862c4dd59910800c65 to your computer and use it in GitHub Desktop.
Add Confirm and cancel handling to a Unity UI element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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