using UnityEngine;
using System.Collections;
using Deduction;
public class MouseInput : MonoBehaviour {
public static MouseInput instance;
void Awake()
{
instance = this;
}
//鼠标左键事件
public event mousePosHandler leftDownEvent;
public event mousePosHandler leftUpEvent;
public event mousePosHandler leftDragEvent;
public event mousePosHandler leftDragDeltaEvent;
public event mousePosHandler leftRelaxedEvent;
public event mousePosHandler leftClickEvent;
Vector2 posLeftDown;//记录鼠标左键按下的位置
Vector2 posLeftPre;
bool isLeftPressed = false;
bool clickEnable = true;
//鼠标滚轮事件
public event mouseScrollHandler mouseScrollEvent;
//鼠标中键事件
public event mousePosHandler middleDownEvent;
public event mousePosHandler middleUpEvent;
public event mousePosHandler middleDragEvent;
public event mousePosHandler middleDragDeltaEvent;
public event mousePosHandler middleRelaxedEvent;
Vector2 posMiddlePre;
bool isMiddlePressed = false;
void Update()
{
//鼠标左键
if (Input.GetMouseButtonDown(0))
{
isLeftPressed=true;
clickEnable = true;
posLeftDown = Input.mousePosition;
posLeftPre = Input.mousePosition;
if(leftDownEvent!=null)
leftDownEvent(Input.mousePosition);
}
if (isLeftPressed)
{
if(((Vector2)Input.mousePosition-posLeftDown).sqrMagnitude>9)
{
clickEnable = false;
}
if (!clickEnable)
{
if (leftDragEvent != null)
leftDragEvent(Input.mousePosition);
Vector2 leftDragDelta = (Vector2)Input.mousePosition -posLeftPre;
posLeftPre = Input.mousePosition;
if (leftDragDeltaEvent != null)
leftDragDeltaEvent(leftDragDelta);
}
}
else
{
if (leftRelaxedEvent!=null)
{
leftRelaxedEvent(Input.mousePosition);
}
}
if (Input.GetMouseButtonUp(0))
{
isLeftPressed = false;
if (leftUpEvent != null)
leftUpEvent(Input.mousePosition);
if(clickEnable)
{
if (leftClickEvent != null)
leftClickEvent(Input.mousePosition);
}
}
//鼠标中键
if (Input.GetMouseButtonDown(2))
{
isMiddlePressed = true;
posMiddlePre = Input.mousePosition;
if (middleDownEvent != null)
middleDownEvent(Input.mousePosition);
}
if (isMiddlePressed)
{
if (middleDragEvent != null)
middleDragEvent(Input.mousePosition);
Vector2 middleDragDelta = (Vector2)Input.mousePosition - posMiddlePre;
posMiddlePre = Input.mousePosition;
if (middleDragDeltaEvent != null)
middleDragDeltaEvent(middleDragDelta);
}
else
{
if (middleRelaxedEvent != null)
middleRelaxedEvent(Input.mousePosition);
}
if (Input.GetMouseButtonUp(2))
{
isMiddlePressed = false;
if (middleUpEvent != null)
middleUpEvent(Input.mousePosition);
}
//鼠标滚轮
float scroll = Input.GetAxis("Mouse ScrollWheel");
if (!Mathf.Approximately(scroll,0))
{
if (mouseScrollEvent != null)
mouseScrollEvent(scroll);
}
}
}
判断是鼠标拖拽还是点击
最新推荐文章于 2024-03-14 16:53:56 发布
1886

被折叠的 条评论
为什么被折叠?



