unity 2018 下 Easy Touch5.X 使用+代码

首先下载一个easy touch 5的版本 https://download.csdn.net/download/fengjiebkb/10582284,这个测试过可以使用。

 导入包

 该插件包含了EasyTouch Lite(触摸检测)和Easy Touch Controls(虚拟摇杆)。所以导入项目后能看到下图EasyTouch(触屏检测)和EasyTouchControls(虚拟摇杆)两个文件夹,根据需要查看Demo和文档。

创建虚拟摇杆

在右侧对摇杆重命名,并且选择Dynamic,area选择屏幕左侧。

 创建代码:

 1 public class Ctrl_HeroMovingCtrlByET : MonoBehaviour
 2 {
 3     //摇杆的名称
 4     private const string JOYSTICK_NAME = "HeroJoystick";
 5     public AnimationClip Ani_Idle;              //动画剪辑_休闲
 6     public AnimationClip Ani_Run;              //动画剪辑_移动
 7     private void Update()
 8     {
 9         // 当前帧的当前手势
10         Gesture currentGesture = EasyTouch.current;
11       
12         if (currentGesture != null) // 不操作时,当前帧的手势返回空,要做为空判断
13         {
14             if (EasyTouch.EvtType.On_UIElementTouchUp == currentGesture.type)
15             {
16                 On_UIElementTouchUp(currentGesture);
17             }
18             if (EasyTouch.EvtType.On_OverUIElement == currentGesture.type)
19             {
20                 On_OverUIElement(currentGesture);
21             }
22         }
23     }   
24     /// <summary>
25     /// 摇杆结束
26     /// </summary>
27     /// <param name="gesture"></param>
28     private void On_UIElementTouchUp(Gesture gesture)
29     {
30        GetComponent<Animation>().CrossFade(Ani_Idle.name);
31     }
32     /// <summary>
33     /// 摇杆
34     /// </summary>
35     /// <param name="gesture"></param>
36     private void On_OverUIElement(Gesture gesture)
37     {
38 
39         //设置角色的朝向(朝向当前坐标+摇杆偏移量)  
40         transform.LookAt(new Vector3(transform.position.x - gesture.deltaPosition.x, transform.position.y, transform.position.z - gesture.deltaPosition.y));
41 
42         //获取摇杆中心偏移的坐标  
43         transform.Translate(Vector3.forward * Time.deltaTime * 5);
44 
45         //播放奔跑动画  
46         GetComponent<Animation>().CrossFade(Ani_Run.name);
47     }
48 }

在角色下面放上我们的脚本,然后添加角色的动作:在摇杆的时候播放run动作,停止的时候播放idle动作。

 OK,测试成功。

猜你喜欢

转载自www.cnblogs.com/lixiao24/p/9415549.html