using UnityEngine;
|
using UnityEngine.UI;
|
using UnityEngine.EventSystems;
|
|
public class UGUIEventListenerContainDrag : MonoBehaviour,
|
IPointerClickHandler,
|
IPointerDownHandler,
|
IPointerEnterHandler,
|
IPointerExitHandler,
|
IPointerUpHandler,
|
IBeginDragHandler,
|
IDragHandler,
|
IEndDragHandler
|
{
|
public delegate void VoidDelegate(GameObject go);
|
public delegate void VoidDelegateDrag(GameObject go, PointerEventData eventData);
|
public VoidDelegate OnClick { get; set; }
|
public VoidDelegate OnDown { get; set; }
|
public VoidDelegate OnEnter { get; set; }
|
public VoidDelegate OnLongPress { get; set; }
|
public VoidDelegate OnExit { get; set; }
|
public VoidDelegate OnUp { get; set; }
|
public VoidDelegateDrag OnDragStart { get; set; }
|
public VoidDelegateDrag OnDraging { get; set; }
|
public VoidDelegateDrag OnDragEnd { get; set; }
|
|
private string mAudioType;
|
//是否处于按下状态 与长按配合使用
|
bool isDown = false;
|
float time = 0;
|
|
public void OnPointerClick(PointerEventData eventData)
|
{
|
if (OnClick != null)
|
{
|
if (!GetComponent<Button>() || GetComponent<Button>().interactable)
|
{
|
OnClick(gameObject);
|
}
|
}
|
}
|
public void OnPointerDown(PointerEventData eventData)
|
{
|
OnDown?.Invoke(gameObject);
|
time = Time.realtimeSinceStartup;
|
isDown = true;
|
}
|
public void OnPointerEnter(PointerEventData eventData) { OnEnter?.Invoke(gameObject); }
|
public void OnPointerExit(PointerEventData eventData) { OnExit?.Invoke(gameObject); isDown = false; }
|
public void OnPointerUp(PointerEventData eventData) { OnUp?.Invoke(gameObject); isDown = false; time = Time.realtimeSinceStartup; }
|
public void OnBeginDrag(PointerEventData eventData) { OnDragStart?.Invoke(gameObject, eventData); }
|
public void OnDrag(PointerEventData eventData) { OnDraging?.Invoke(gameObject, eventData); }
|
public void OnEndDrag(PointerEventData eventData) { OnDragEnd?.Invoke(gameObject, eventData); }
|
|
public static UGUIEventListenerContainDrag Get(GameObject go, string clickAudio = null)
|
{
|
UGUIEventListenerContainDrag listener = go.GetComponent<UGUIEventListenerContainDrag>();
|
if (listener == null) listener = go.AddComponent<UGUIEventListenerContainDrag>();
|
listener.mAudioType = clickAudio;
|
return listener;
|
}
|
|
void Update()
|
{
|
//长按
|
if (OnLongPress != null && isDown && Time.realtimeSinceStartup - time > 0.8f)
|
{
|
OnLongPress(gameObject);
|
isDown = false;
|
}
|
}
|
}
|