鼠标相关的枚举参考:
public enum MouseButtonType { Left, Right, Middle }
public enum MouseOperationType { BtnDown, BtnUp, Click, DoubleClick, Drag, DragDelta, DragVector, Release, ReleaseOutUI }
产生事件的代码参考:
using UnityEngine;
using UnityEngine.Events;
public class SceneMouseOperation : MonoBehaviour
{
#region Class MouseButtonStatus
class MouseButtonStatus
{
public bool clickEnable = false;
public float clickTime = 0;
public bool isDrag = false;
public Vector2 posBtnDown = Vector2.zero;
public Vector2 posDragStart = Vector2.zero;
public Vector2 posDragPre = Vector2.zero;
}
#endregion
#region SingleTon
static public SceneMouseOperation instance;
//
void Awake()
{
instance = this;
}
#endregion
#region Update Message
void Update()
{
ButtonOperation(pointerOnUI);
ScrollOperation(pointerOnUI);
}
#endregion
#region Function ButtonOperation
bool pointerOnUI = false;
public void SetPointOnUI(bool pointerOnUI) { this.pointerOnUI = pointerOnUI; }
float doubleClickInterval = 0.2f;
public void SetDoubleClickInterval(float interval) { doubleClickInterval = interval; }
MouseButtonStatus[] mouseButtonStatuses = { new MouseButtonStatus(), new MouseButtonStatus(), new MouseButtonStatus() };
Vector2 mousePosPre = Vector2.zero;
UnityAction<int, MouseOperationType, Vector2> onMouseOperation;
public void AddActMouseOperation(UnityAction<int, MouseOperationType, Vector2> act) { onMouseOperation -= act; onMouseOperation += act; }
public void RemoveActMouseOperation(UnityAction<int, MouseOperationType, Vector2> act) { onMouseOperation -= act; }
public void ButtonOperation(bool pointerOnUI)
{
bool isMouseMove = false;
Vector2 mousePos = Input.mousePosition;
if (mousePosPre != mousePos)
{
isMouseMove = true;
mousePosPre = mousePos;
}
//
for (int btnIndex = 0; btnIndex < 3; btnIndex++)
{
MouseButtonStatus status = mouseButtonStatuses[btnIndex];
//
if (Input.GetMouseButtonDown(btnIndex))
{
if (!pointerOnUI)
{
status.isDrag = true;
status.clickEnable = true;
status.posBtnDown = mousePos;
}
onMouseOperation?.Invoke(btnIndex, MouseOperationType.BtnDown, mousePos);
}
if (status.isDrag)
{
if (isMouseMove)
{
if (status.clickEnable)
{
if ((status.posBtnDown - mousePos).sqrMagnitude > 9)
{
status.clickEnable = false;
status.posDragStart = mousePos;
status.posDragPre = mousePos;
}
}
else
{
Vector2 dragDelta = mousePos - status.posDragPre;
status.posDragPre = mousePos;
onMouseOperation?.Invoke(btnIndex, MouseOperationType.DragDelta, dragDelta);
//
Vector2 dragVector = mousePos - status.posDragStart;
onMouseOperation?.Invoke(btnIndex, MouseOperationType.DragVector, dragVector);
}
onMouseOperation?.Invoke(btnIndex, MouseOperationType.Drag, mousePos);
}
}
if (Input.GetMouseButtonUp(btnIndex))
{
status.isDrag = false;
if (status.clickEnable)
{
status.clickEnable = false;
//
onMouseOperation?.Invoke(btnIndex, MouseOperationType.Click, mousePos);
if ((Time.time - status.clickTime) < doubleClickInterval)
{
status.clickTime = 0;
onMouseOperation?.Invoke(btnIndex, MouseOperationType.DoubleClick, mousePos);
}
else
{
status.clickTime = Time.time;
}
}
onMouseOperation?.Invoke(btnIndex, MouseOperationType.BtnUp, mousePos);
}
}
//
if (isMouseMove)
{
bool isBtnDrag = false;
foreach (MouseButtonStatus status in mouseButtonStatuses)
{
if (status.isDrag)
{
isBtnDrag = true;
break;
}
}
if (!isBtnDrag)
{
if (!pointerOnUI)
{
onMouseOperation?.Invoke(0, MouseOperationType.ReleaseOutUI, mousePos);
}
onMouseOperation?.Invoke(0, MouseOperationType.Release, mousePos);
}
}
}
#endregion
#region Function ScrollOperation
UnityAction<float> onScroll;
public void AddActScroll(UnityAction<float> act) { onScroll -= act; onScroll += act; }
public void RemoveActScroll(UnityAction<float> act) { onScroll -= act; }
public void ScrollOperation(bool pointerOnUI)
{
if (!pointerOnUI)
{
Rect rect = new Rect(0, 0, Screen.width, Screen.height);
if (rect.Contains(Input.mousePosition))
{
float scroll = Input.GetAxis("Mouse ScrollWheel");
if (!Mathf.Approximately(scroll, 0))
{
onScroll?.Invoke(scroll);
}
}
}
}
#endregion
}