微信登录

Easy Touch 5 - 触摸控制器 - 代码方式

作用:触摸控制器

仅供学习版 5.0.17版

链接:https://pan.baidu.com/s/18LLWN7E6rysoGZqwmqtnxA 密码:aiyl

Easy Touch 5介绍

功能包内容:

EasyTouchBundle
| — EasyTouch 触摸检测
| — | — Documentation 文档
| — | — Examples 例子
| — | — Plugins 插件
| — EasyTouchControls 控制
| — | — Component 组成部分
| — | — Documentation 文档
| — | — Examples 例子
| — | — Plugins 插件
| — | — Prefab 预制体
| — Install安装
| — | — PlayMakerAddOn

文件拖入Assets

导入文件后的cs0618出错,去掉错误的方法:

双击错误,打开错误的文件
加上:

  1. #pragma warning disable 0618

导入文件后的cs0619出错,去掉错误的方法:

  1. EventType.mouseDown修改为:
  2. EventType.MouseDown

成功导入

这是需要导入的类

  1. using HedgehogTeam.EasyTouch;

使用 - 代码方式:

右键创建一个GameObject物体 — EasyTouch — EasyTouch
右键创建一个GameObject空物体 — 命名为EasyTouchManager
在 EasyTouchManager 上加C#代码 — EasyTouchManager5_Demo.cs

EasyTouchManager5_Demo.cs 代码:

2个方法,方法一比较随便,方法二更专业,哈哈哈
方法一:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using HedgehogTeam.EasyTouch;//1、导入类
  5. /// <summary>
  6. /// 五代EasyTouch特性 使用的是 单例 来 监听事件
  7. /// </summary>
  8. public class EasyTouchManager5_Demo : MonoBehaviour
  9. {
  10. void Update()//2、update这个方法
  11. {
  12. Gesture currentGesture = EasyTouch.current;//3、获取当前手势
  13. if (currentGesture == null) return;//4、注意 当手机屏幕上没有任何操作的时候currentGesture是null的,要添加一个判断,防止空指针异常
  14. //5、以下是事件监听
  15. //按下
  16. if (EasyTouch.EvtType.On_TouchStart == currentGesture.type)
  17. {
  18. //可以把方法写在里面
  19. print("OnTouchStart");
  20. print("StartPosition" + currentGesture.startPosition);//触摸位置
  21. }
  22. //抬起
  23. if (EasyTouch.EvtType.On_TouchUp == currentGesture.type)
  24. {
  25. //可以把方法写在里面
  26. print("OnTouchEnd");
  27. print("OnTouchEnd" + gesture.actionTime);//触摸时间
  28. }
  29. //滑动
  30. if (EasyTouch.EvtType.On_Swipe == currentGesture.type)
  31. {
  32. //可以把方法写在里面
  33. print("OnSwipe");
  34. print("Type" + gesture.type);//触摸类型
  35. }
  36. }
  37. }

方法二:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using HedgehogTeam.EasyTouch;//1、导入类
  5. /// <summary>
  6. /// 五代EasyTouch特性 使用的是 单例 来 监听事件
  7. /// </summary>
  8. public class EasyTouchManager5_Demo : MonoBehaviour
  9. {
  10. void Update()//2、update这个方法
  11. {
  12. Gesture currentGesture = EasyTouch.current;//3、获取当前手势
  13. if (currentGesture == null) return;//4、注意 当手机屏幕上没有任何操作的时候currentGesture是null的,要添加一个判断,防止空指针异常
  14. //5、以下是事件监听
  15. //按下
  16. if (EasyTouch.EvtType.On_TouchStart == currentGesture.type)
  17. {
  18. OnTouchStart(currentGesture);
  19. }
  20. //抬起
  21. if (EasyTouch.EvtType.On_TouchUp == currentGesture.type)
  22. {
  23. OnTouchEnd(currentGesture);
  24. }
  25. //滑动
  26. if (EasyTouch.EvtType.On_Swipe == currentGesture.type)
  27. {
  28. OnSwipe(currentGesture);
  29. }
  30. }
  31. void OnTouchStart(Gesture gesture)
  32. {
  33. //可以把方法写在里面
  34. print("OnTouchStart");
  35. print("StartPosition" + gesture.startPosition);//触摸位置
  36. }
  37. void OnTouchEnd(Gesture gesture)
  38. {
  39. //可以把方法写在里面
  40. print("OnTouchEnd");
  41. print("OnTouchEnd" + gesture.actionTime);//触摸时间
  42. }
  43. void OnSwipe(Gesture gesture)
  44. {
  45. //可以把方法写在里面
  46. print("OnSwipe");
  47. print("Type" + gesture.type);//触摸类型
  48. }
  49. }
Easy Touch 5 - 触摸控制器 - 代码方式