Files
2026-05-26 16:15:54 +08:00

80 lines
1.9 KiB
C#

using UnityEngine;
using UnityEngine.EventSystems;
public class UIDrag : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerDownHandler, IPointerUpHandler
{
//按下回调
public System.Action OnPointerDownCall;
public System.Action OnPointerUpCall;
//开始拖动回调
public System.Action OnBeginDragCall;
//拖动中回调
public System.Action<PointerEventData> OnDragCall;
//拖动结束回调
public System.Action OnEndDragCall;
bool isdrag = false;
public bool isTouchTwo = false;
public void OnPointerDown(PointerEventData eventData)
{
OnPointerDownCall?.Invoke();
}
public void OnPointerUp(PointerEventData eventData)
{
OnPointerUpCall?.Invoke();
}
public void OnBeginDrag(PointerEventData eventData)
{
#if UNITY_EDITOR
if (Input.GetMouseButton(0))
#else
if (Input.touchCount == 1 || Input.touchCount == 2)
#endif
{
OnBeginDragCall?.Invoke();
isdrag = true;
}
}
public void OnDrag(PointerEventData eventData)
{
OnDragCall?.Invoke(eventData);
if (isTouchTwo)
{
#if !UNITY_EDITOR
//if (Input.touchCount == 1)
//{
// if (!isdrag)
// {
// OnBeginDragCall?.Invoke();
// isdrag = true;
// }
//}
//else
if (Input.touchCount == 2)
{
//双指操作
if (isdrag)
{
OnEndDragCall?.Invoke();
isdrag = false;
}
}
#endif
}
}
public void OnEndDrag(PointerEventData eventData)
{
if (isdrag)
{
OnEndDragCall?.Invoke();
isdrag = false;
}
}
//双指操作
}