2015-02-03 03:25:41 +08:00
|
|
|
/// Credit AriathTheWise
|
|
|
|
/// Sourced from - http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/page-2#post-1796783
|
2015-09-11 01:50:23 +08:00
|
|
|
/// Extended to include a HELD state that continually fires while the button is held down.
|
2015-02-03 03:25:41 +08:00
|
|
|
|
|
|
|
using UnityEngine.Events;
|
|
|
|
using UnityEngine.EventSystems;
|
2015-02-03 07:07:31 +08:00
|
|
|
|
|
|
|
namespace UnityEngine.UI.Extensions
|
2015-02-03 03:25:41 +08:00
|
|
|
{
|
2015-02-03 07:07:31 +08:00
|
|
|
/// <summary>
|
|
|
|
/// UIButton
|
|
|
|
/// </summary>
|
|
|
|
[AddComponentMenu("UI/Extensions/UI Button")]
|
|
|
|
public class UIButton : Button, IPointerDownHandler, IPointerUpHandler
|
2015-02-03 03:25:41 +08:00
|
|
|
{
|
2015-02-03 07:07:31 +08:00
|
|
|
#region Sub-Classes
|
|
|
|
[System.Serializable]
|
|
|
|
public class UIButtonEvent : UnityEvent<PointerEventData.InputButton> { }
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Events
|
2015-09-11 01:50:23 +08:00
|
|
|
[Tooltip("Event that fires when a button is clicked")]
|
2015-02-03 07:07:31 +08:00
|
|
|
public UIButtonEvent OnButtonClick;
|
2015-09-11 01:50:23 +08:00
|
|
|
[Tooltip("Event that fires when a button is initially pressed down")]
|
2015-02-03 07:07:31 +08:00
|
|
|
public UIButtonEvent OnButtonPress;
|
2015-09-11 01:50:23 +08:00
|
|
|
[Tooltip("Event that fires when a button is released")]
|
2015-02-03 07:07:31 +08:00
|
|
|
public UIButtonEvent OnButtonRelease;
|
2015-09-11 01:50:23 +08:00
|
|
|
[Tooltip("Event that continually fires while a button is held down")]
|
|
|
|
public UIButtonEvent OnButtonHeld;
|
2015-02-03 07:07:31 +08:00
|
|
|
#endregion
|
2015-09-11 01:50:23 +08:00
|
|
|
|
|
|
|
private bool _pressed;
|
|
|
|
private PointerEventData _heldEventData;
|
2015-02-03 07:07:31 +08:00
|
|
|
|
|
|
|
public override void OnPointerClick(PointerEventData eventData)
|
2015-02-03 03:25:41 +08:00
|
|
|
{
|
2015-02-03 07:07:31 +08:00
|
|
|
base.OnSubmit(eventData);
|
|
|
|
|
|
|
|
if (OnButtonClick != null)
|
|
|
|
{
|
|
|
|
OnButtonClick.Invoke(eventData.button);
|
|
|
|
}
|
2015-02-03 03:25:41 +08:00
|
|
|
}
|
2015-02-03 07:07:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
|
2015-02-03 03:25:41 +08:00
|
|
|
{
|
2015-02-03 07:07:31 +08:00
|
|
|
DoStateTransition(SelectionState.Pressed, false);
|
|
|
|
|
|
|
|
if (OnButtonPress != null)
|
|
|
|
{
|
|
|
|
OnButtonPress.Invoke(eventData.button);
|
|
|
|
}
|
2015-09-11 01:50:23 +08:00
|
|
|
_pressed = true;
|
|
|
|
_heldEventData = eventData;
|
2015-02-03 03:25:41 +08:00
|
|
|
}
|
2015-02-03 07:07:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
|
2015-02-03 03:25:41 +08:00
|
|
|
{
|
2015-02-03 07:07:31 +08:00
|
|
|
DoStateTransition(SelectionState.Normal, false);
|
|
|
|
|
|
|
|
if (OnButtonRelease != null)
|
|
|
|
{
|
|
|
|
OnButtonRelease.Invoke(eventData.button);
|
|
|
|
}
|
2015-09-11 01:50:23 +08:00
|
|
|
_pressed = false;
|
|
|
|
_heldEventData = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
if (!_pressed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (OnButtonHeld != null)
|
|
|
|
{
|
|
|
|
OnButtonHeld.Invoke(_heldEventData.button);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-02-03 07:07:31 +08:00
|
|
|
}
|
2015-02-03 03:25:41 +08:00
|
|
|
}
|