62 lines
1.8 KiB
C#
62 lines
1.8 KiB
C#
using System;
|
||
using UnityEngine;
|
||
using UnityEngine.EventSystems;
|
||
|
||
// 为避免潜在的冲突和更清晰的逻辑,建议将类名稍作更改,以反映其新功能
|
||
// 例如:UIMouseBlocker,或者就保持 UIBlockCameraDrag 也可以,只要功能上能理解
|
||
public class UIBlockCameraDrag : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler
|
||
{
|
||
public static bool IsPointerOnUI = false;
|
||
public static bool ShouldBlockDrag = false;
|
||
public static bool MoveEvent = false;
|
||
public static bool DownUpEvent = false;
|
||
public static Vector3 DragOrigin;
|
||
public static Vector3 MoveVector;
|
||
|
||
//是否ban点击
|
||
public bool BanClick = false;
|
||
|
||
public void OnPointerEnter(PointerEventData eventData)
|
||
{
|
||
IsPointerOnUI = true;
|
||
}
|
||
|
||
public void OnPointerExit(PointerEventData eventData)
|
||
{
|
||
IsPointerOnUI = false;
|
||
ShouldBlockDrag = false;
|
||
MoveEvent = false;
|
||
}
|
||
|
||
public void OnPointerDown(PointerEventData eventData)
|
||
{
|
||
if (IsPointerOnUI && eventData.button == PointerEventData.InputButton.Left)
|
||
{
|
||
ShouldBlockDrag = true;
|
||
DragOrigin = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||
}
|
||
}
|
||
|
||
public void OnPointerUp(PointerEventData eventData)
|
||
{
|
||
if (IsPointerOnUI && eventData.button == PointerEventData.InputButton.Left)
|
||
{
|
||
if (!MoveEvent && !BanClick) DownUpEvent = true;
|
||
ShouldBlockDrag = false;
|
||
MoveEvent = false;
|
||
}
|
||
}
|
||
|
||
public void Update()
|
||
{
|
||
if (ShouldBlockDrag)
|
||
{
|
||
MoveVector = DragOrigin - Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||
if (MoveVector.magnitude > 0.01f)
|
||
{
|
||
MoveEvent = true;
|
||
}
|
||
}
|
||
}
|
||
}
|