1 将此包导入入 unity 场景
导入包 1
导入包 2
2 取消勾选不必要的文件
取消勾选
3 在你的核心控制移动脚本中,先加上导入语句如下
using HedgehogTeam.EasyTouch;
4 然后在上下左右的控制语句加入如下代码
Gesture currentGesture = EasyTouch.current;
if(currentGesture!=null&¤tGesture.swipe==EasyTouch.SwipeDirection.Up)
如果你原来的逻辑如下
void FixedUpdate () {
Vector2 p=Vector2.MoveTowards (transform.position,dest,speed);
gameObject.GetComponent<Rigidbody2D> ().MovePosition (p);
Gesture currentGesture = EasyTouch.current;
if ((Vector2)transform.position == dest) {
if ((Input.GetKey (KeyCode.UpArrow) || Input.GetKey (KeyCode.W))&&Valid(Vector2.up)) {
dest = (Vector2)transform.position + Vector2.up;
}
if ((Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.D))&&Valid(Vector2.right)) {
dest = (Vector2)transform.position + Vector2.right;
}
if ((Input.GetKey (KeyCode.DownArrow) || Input.GetKey (KeyCode.S))&&Valid(Vector2.down)) {
dest = (Vector2)transform.position + Vector2.down;
}
if ((Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.A))&&Valid(Vector2.left)) {
dest = (Vector2)transform.position + Vector2.left;
}
}
Vector2 dir = dest - (Vector2)transform.position;
gameObject.GetComponent<Animator> ().SetFloat ("DirX",dir.x);
gameObject.GetComponent<Animator> ().SetFloat ("DirY",dir.y);
}
那么现在就要改成这个样子
void FixedUpdate () {
Vector2 p=Vector2.MoveTowards (transform.position,dest,speed);
gameObject.GetComponent<Rigidbody2D> ().MovePosition (p);
Gesture currentGesture = EasyTouch.current;
if ((Vector2)transform.position == dest) {
if (((currentGesture!=null&¤tGesture.swipe==EasyTouch.SwipeDirection.Up)||(Input.GetKey (KeyCode.UpArrow) || Input.GetKey (KeyCode.W)))&&Valid(Vector2.up)) {
dest = (Vector2)transform.position + Vector2.up;
}
if (((currentGesture!=null&¤tGesture.swipe==EasyTouch.SwipeDirection.Right)||(Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.D)))&&Valid(Vector2.right)) {
dest = (Vector2)transform.position + Vector2.right;
}
if (((currentGesture!=null&¤tGesture.swipe==EasyTouch.SwipeDirection.Down)||(Input.GetKey (KeyCode.DownArrow) || Input.GetKey (KeyCode.S)))&&Valid(Vector2.down)) {
dest = (Vector2)transform.position + Vector2.down;
}
if (((currentGesture!=null&¤tGesture.swipe==EasyTouch.SwipeDirection.Left)||(Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.A)))&&Valid(Vector2.left)) {
dest = (Vector2)transform.position + Vector2.left;
}
}
Vector2 dir = dest - (Vector2)transform.position;
gameObject.GetComponent<Animator> ().SetFloat ("DirX",dir.x);
gameObject.GetComponent<Animator> ().SetFloat ("DirY",dir.y);
}
就是添加滑动事件的条件,是不是明白了呢?
来源: http://www.jianshu.com/p/7eaf311f3bfe