使用EasyTouch中的触摸控件

1 将此包导入入unity场景


使用EasyTouch中的触摸控件_第1张图片
导入包1

导入包2

2 取消勾选不必要的文件


使用EasyTouch中的触摸控件_第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 ().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 ().SetFloat ("DirX",dir.x);
        gameObject.GetComponent ().SetFloat ("DirY",dir.y);
    }

那么现在就要改成这个样子

void FixedUpdate () {
        Vector2 p=Vector2.MoveTowards (transform.position,dest,speed);
        gameObject.GetComponent ().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 ().SetFloat ("DirX",dir.x);
        gameObject.GetComponent ().SetFloat ("DirY",dir.y);
    }

就是添加滑动事件的条件,是不是明白了呢?

你可能感兴趣的:(使用EasyTouch中的触摸控件)