判断是鼠标拖拽还是点击
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); } } }
相关文章推荐
- jquery判断是用户鼠标点击事件还是jquery的trigger事件
- js 如何判断鼠标点击事件还是js代码调用
- Winform,C#,listView判断鼠标点击是行还是listView的空白区
- Winform,C#,listView判断鼠标点击是行还是listView的空白区
- Winform,C#,listView判断鼠标点击是行还是listView的空白区
- 百度地图 判断点击的是 map 还是 marker
- 判断IE是否是点击关闭按钮关闭的,还是在刷新
- 还是一个鼠标点击td变成input,失去焦点更新数据库
- js如何用判断鼠标的右键还是左键,屏蔽右键菜单
- UGUI研究院之判断鼠标或者手指是否点击在UI上
- js判断鼠标左、中、右键哪个被点击的方法
- 判断请求是通过点击链接还是直接输入网址
- 【Unity】UGUI如何判断鼠标或者手指是否点击到UI上
- 【百度地图API】如何判断点击的是地图还是覆盖物?
- 《鼠标点击物体,物体跟随光标移动》与《鼠标按住左键不放拖拽物体》
- chrome下判断点击input上标签还是其余标签的实现方法
- js判断鼠标滚动放下,向上滚还是向下滚?
- 原生js及H5模拟鼠标点击拖拽
- UGUI 之 判断鼠标或者手指是否点击在UI上
- 判断MonthCalander中鼠标点中了日期还是翻页按钮!