您的位置:首页 > 其它

判断是鼠标拖拽还是点击

2017-01-17 21:12 267 查看
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;

4000
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);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: