我是从这两位转载的 但是网上一堆一样的代码,我也不确定他们是不是原创
https://blog.csdn.net/m0_38066968/article/details/102804059
https://blog.csdn.net/fanglrui/article/details/28009347
private Vector2 first = Vector2.zero;
private Vector2 second = Vector2.zero;
// 鼠标左右滑动事件
void OnGUI()
{
if (Event.current.type == EventType.MouseDown)
{
//记录鼠标按下的位置
first = Event.current.mousePosition;
}
if (Event.current.type == EventType.MouseDrag)
{
//记录鼠标拖动的位置
second = Event.current.mousePosition;
if (second.x < first.x)
{
//拖动的位置的x坐标比按下的位置的x坐标小时,响应向左事件
print("left");
}
if (second.x > first.x)
{
//拖动的位置的x坐标比按下的位置的x坐标大时,响应向右事件
print("right");
}
first = second;
}
}
enum slideVector { nullVector, left, right };
private Vector2 lastPos;//上一个位置
private Vector2 currentPos;//下一个位置
private slideVector currentVector = slideVector.nullVector;//当前滑动方向
private float timer;//时间计数器
public float offsetTime = 0.01f;//判断的时间间隔
private void Update()
{
//触摸左右滑动事件
if (Input.touchCount == 1)
{
if (Input.touches[0].phase == TouchPhase.Began)
{
lastPos = Input.touches[0].position;
currentPos = Input.touches[0].position;
timer = 0;
Debug.Log("Click begin && Drag begin");
}
if (Input.touches[0].phase == TouchPhase.Moved)
{
currentPos = Input.touches[0].position;
timer += Time.deltaTime;
if (timer > offsetTime)
{
if (currentPos.x < lastPos.x)
{
if (currentVector == slideVector.left)
{
return;
}
//TODO trun Left event
currentVector = slideVector.left;
Debug.Log("Touch Turn left");
}
if (currentPos.x > lastPos.x)
{
if (currentVector == slideVector.right)
{
return;
}
//TODO trun right event
currentVector = slideVector.right;
Debug.Log("Touch Turn right");
}
lastPos = currentPos;
timer = 0;
}
}
if (Input.touches[0].phase == TouchPhase.Ended)
{//滑动结束
currentVector = slideVector.nullVector;
Debug.Log("Click over && Drag over");
}
}
}