判断滑动屏幕的方向,但是只能判断一次比较遗憾
参考官方API:http://docs.Unity3D.com/ScriptReference/EventType.html
代码:
private Vector2 lastPos;//上一个位置
private Vector2 currentPos;//现在的位置
private float timer;//时间计数器
private float offsetTime = 0.001f;//判断的时间间隔
voidOnGUI(){
if(Event.current.type == EventType.MouseDown) {//滑动开始
lastPos = Event.current.mousePosition;
currentPos = Event.current.mousePosition;
timer = 0;
}
if(Event.current.type == EventType.MouseDrag) {//滑动过程
currentPos = Event.current.mousePosition;
timer += Time.deltaTime;
if(timer > offsetTime) {
if(currentPos.x < lastPos.x) {
if(currentVector == slideVector.left) {
return;
}
currentVector = slideVector.left;
}
if(currentPos.x > lastPos.x) {
if(currentVector == slideVector.right) {
return;
}
currentVector = slideVector.right;
}
lastPos = currentPos;
timer = 0;
}
}
if(Event.current.type == EventType.MouseUp) {//滑动结束
currentVector = slideVector.nullVector;
}
}