TH1/Unity/Assets/Scripts/TH1_UI/UIBlockCameraDrag.cs
2025-08-24 01:22:34 +08:00

62 lines
1.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}
}
}