[Unity插件]EasyTouch的使用

参考链接:http://www.cnblogs.com/dingxiaowei/p/3581456.html?utm_source=tuicool


1.创建空物体,添加EasyTouch脚本,确保General properties中的Broadcast messages是取消勾选的。

2.创建一个新的C#脚本,命名为MyFirstTouch,加到上面的空物体上。

3.运行并点击屏幕。

using UnityEngine;
using System.Collections;

public class MyFirstTouch : MonoBehaviour {

    void OnEnable()
    {
        //鼠标在屏幕上点击时,On_TouchStart响应一次,On_TouchDown至少响应一次;松开时On_TouchUp响应一次
        EasyTouch.On_TouchStart += EasyTouch_On_TouchStart;
        EasyTouch.On_TouchDown += EasyTouch_On_TouchDown;
        EasyTouch.On_TouchUp += EasyTouch_On_TouchUp;

        //鼠标在屏幕上拖拽时,On_SwipeStart响应一次,On_Swipe至少响应一次;松开时On_SwipeEnd响应一次
        EasyTouch.On_SwipeStart += EasyTouch_On_SwipeStart;
        EasyTouch.On_Swipe += EasyTouch_On_Swipe;
        EasyTouch.On_SwipeEnd += EasyTouch_On_SwipeEnd;
    }

    public void EasyTouch_On_TouchStart(Gesture gesture)
    {
        //print("Position:" + gesture.position);
        print("EasyTouch_On_TouchStart");
    }
    public void EasyTouch_On_TouchDown(Gesture gesture)
    {
        print("EasyTouch_On_TouchDown");
    }
    public void EasyTouch_On_TouchUp(Gesture gesture)
    {
        print("EasyTouch_On_TouchUp");
    }

    public void EasyTouch_On_SwipeStart(Gesture gesture)
    {
        print("EasyTouch_On_SwipeStart");
    }
    public void EasyTouch_On_Swipe(Gesture gesture)
    {
        //print("X:" + gesture.deltaPosition.x);
        //print("Y:" + gesture.deltaPosition.y);
        print("EasyTouch_On_Swipe");
    }
    public void EasyTouch_On_SwipeEnd(Gesture gesture)
    {
        print("EasyTouch_On_SwipeEnd");
    }

    void OnDisable()
    {
        EasyTouch.On_TouchStart -= EasyTouch_On_TouchStart;//省略n行
    }

    void OnDestroy()
    {
        EasyTouch.On_TouchStart -= EasyTouch_On_TouchStart;//省略n行
    }
}

摇杆的使用:

1..创建空物体,添加EasyTouch脚本和EasyJoystick脚本。

2.设置好参数。


3.创建脚本MoveController.cs用来接收摇杆事件控制角色的移动。

using UnityEngine;  
using System.Collections;  
  
public class MoveController : MonoBehaviour {  
  
    void OnEnable()  
    {  
        EasyJoystick.On_JoystickMove += OnJoystickMove;  
        EasyJoystick.On_JoystickMoveEnd += OnJoystickMoveEnd;  
    }  
  
  
    //移动摇杆结束  
    void OnJoystickMoveEnd(MovingJoystick move)  
    {  
        //停止时,角色恢复idle  
        if (move.joystickName == "MoveJoystick")  
        {  
            animation.CrossFade("idle");  
        }  
    }  
  
  
    //移动摇杆中  
    void OnJoystickMove(MovingJoystick move)  
    {  
        if (move.joystickName != "MoveJoystick")  
        {  
            return;  
        }  
          
        //获取摇杆中心偏移的坐标  
        float joyPositionX = move.joystickAxis.x;  
        float joyPositionY = move.joystickAxis.y;  
  
  
        if (joyPositionY != 0 || joyPositionX != 0)  
        {  
            //设置角色的朝向(朝向当前坐标+摇杆偏移量)  
           transform.LookAt(new Vector3(transform.position.x + joyPositionX, transform.position.y, transform.position.z + joyPositionY));  
            //移动玩家的位置(按朝向位置移动)  
            transform.Translate(Vector3.forward * Time.deltaTime * 5);  
            //播放奔跑动画  
            animation.CrossFade("run");  
        }  
    }  
}

按钮的使用:

1.在上面的空物体上添加EasyButton脚本。

2.设置好参数。


3.为按钮绑定的对象添加jump方法。

你可能感兴趣的:(EasyTouch)