但我们开发移动端的游戏时,发现使用Input.GetMouseButtonDown的方法不可用,怎么办?

虽然unity3d也有自带触屏的方法,但是使用起来代价太高,什么单击,双击这些功能都要自己封装。

下面我们来讲下EasyTouch这个插件,它将所有触屏的手势,都已经写好了。

而且Easytouch也支持NGUI,使用起来十分的方便。

接下来,我们详细地学习这个插件改如何运用到我们的项目中来。

首先,我们导入easytouch插件,这里我是用3.0版本的,可能有些老了,我都没更新,但是大致的功能实际上已经完全可以胜任了。

创建easytouch的步骤:

1.这里我使用的c#的脚本,js我不太熟悉。

2.完了之后你们会看到在scene中会出现EasyTouch的小图标,这样我们就能实现诸多触屏的功能。OK,马上来测试一下。

3.新建一个c#脚本,来实现简单的触屏逻辑,这里注意一下,easytouch是支持在editor下面进行触屏测试的,我们不用再把app发送到真机上测试。这也大大简化了开发者的工作。这也是我个人强烈推荐这个插件的原因。

好了,我们巴新建的脚本取名为TouchTest,

using UnityEngine;
using System.Collections; public class TouchTest : MonoBehaviour { // Subscribe to events
void OnEnable(){
EasyTouch.On_TouchStart += On_MyTouchStart;//启动On_TouchStart监听,也就是手指接触屏幕,就会触发On_MyTouchStart的方法执行
}
// Unsubscribe
void OnDisable(){
EasyTouch.On_TouchStart -= On_MyTouchStart;//去除监听
}
// Unsubscribe
void OnDestroy(){
EasyTouch.On_TouchStart -= On_MyTouchStart;//去除监听
}
// Touch start event
public void On_MyTouchStart(Gesture gesture){
Debug.Log( "Touch in " + gesture.position);//打印触摸到屏幕的坐标Vector2
}
}

 写好代码之后,我们新建一个GameObject,然后把脚本赋值给这个物体。

启动demo,观察效果,我随便在屏幕上点击三下,发现Console打印了这三个坐标。测试完毕。

大致步骤就是如此,这里只是仅仅测试一个On_TouchStart,在easytouch的API中封装了好多手势的方法:

On_Cancel( Gesture gesture)
Occurs when The system cancelled tracking for the touch, as when (for example) the user puts the device to her face.
On_Cancel2Fingers( Gesture gesture)
Occurs when the touch count is no longer egal to 2 and different to 0, after the begining of a two fingers gesture.
On_TouchStart( Gesture gesture)
Occurs when a finger touched the screen.
On_TouchDown( Gesture gesture)
Occurs as the touch is active.
On_TouchUp( Gesture gesture)
Occurs when a finger was lifted from the screen.
On_SimpleTap( Gesture gesture)
Occurs when a finger was lifted from the screen, and the time elapsed since the beginning of the touch is less than
the time required for the detection of a long tap.
On_DoubleTap( Gesture gesture)
Occurs when the number of taps is egal to 2 in a short time.
On_LongTapStart( Gesture gesture)
Occurs when a finger is touching the screen, but hasn’t moved since the time required for the detection of a long tap.
On_LongTap( Gesture gesture)
Occurs as the touch is active after a LongTapStart
On_LongTapEnd( Gesture gesture)
Occurs when a finger was lifted from the screen, and the time elapsed since the beginning of the touch is more than
the time required for the detection of a long tap.
On_DragStart( Gesture gesture)
Occurs when a drag start. A drag is a swipe on a pickable object
On_Drag( Gesture gesture)
Occurs as the drag is active.
On_DragEnd( Gesture gesture)
Occurs when a finger that raise the drag event , is lifted from the screen.
On_SwipeStart( Gesture gesture)
Occurs when swipe start.
On_Swipe( Gesture gesture)
Occurs as the swipe is active.
On_SwipeEnd( Gesture gesture)
Occurs when a finger that raise the swipe event , is lifted from the screen.
On_TouchStart2Fingers( Gesture gesture)
Like On_TouchStart but for a 2 fingers gesture.
On_TouchDown2Fingers( Gesture gesture)
Like On_TouchDown but for a 2 fingers gesture.
On_TouchUp2Fingers( Gesture gesture)
Like On_TouchUp but for a 2 fingers gesture.
On_SimpleTap2Fingers( Gesture gesture)
Like On_SimpleTap but for a 2 fingers gesture.
On_DoubleTap2Fingers( Gesture gesture)
Like On_DoubleTap but for a 2 fingers gesture.
On_LongTapStart2Fingers( Gesture gesture)
Like On_LongTapStart but for a 2 fingers gesture.
On_LongTap2Fingers( Gesture gesture)
Like On_LongTap but for a 2 fingers gesture.
On_LongTapEnd2Fingers( Gesture gesture)
Like On_LongTapEnd but for a 2 fingers gesture.
On_Twist( Gesture gesture)
Occurs when a twist gesture start
On_TwistEnd( Gesture gesture)
Occurs as the twist gesture is active.
On_PinchIn( Gesture gesture)
Occurs as the twist in gesture is active.
On_PinchOut( Gesture gesture)
Occurs as the pinch out gesture is active.
On_PinchEnd( Gesture gesture)
Occurs when the 2 fingers that raise the pinch event , are lifted from the screen.
On_DragStart2Fingers( Gesture gesture)
Like On_DragStart but for a 2 fingers gesture.
On_Drag2Fingers( Gesture gesture)
Like On_Drag but for a 2 fingers gesture.
On_DragEnd2Fingers( Gesture gesture)
Like On_DragEnd2Fingers but for a 2 fingers gesture.
On_SwipeStart2Fingers( Gesture gesture)
Like On_SwipeStart but for a 2 fingers gesture.
On_Swipe2Fingers( Gesture gesture)
Like On_Swipe but for a 2 fingers gesture.
On_SwipeEnd2Fingers( Gesture gesture)
Like On_SwipeEnd but for a 2 fingers gesture.

这里我就不一一测试了,有兴趣的童鞋可以去官方的demo看看。

接着我们来看看easytouch的属性表:

Enable EasyTouch------->是否启用easytouch,否则所有的触屏效果消失。

Enable unity remote-------->是否启用Unity Remote,这个是啥东西呢,他是Unity开发移动游戏的辅助工具,就是在你的手机上安装这个app或apk,然后通过

数据线连接到你的电脑上,当你的unity要build 发布的时候,他就会自动在你的手机上测试,不用再build完之后把apk发到手机上测试。

就是这个小东东:

Broadcast Messages-------------------->是否启动Unity里面的SendMessage的机制,不熟悉这个的童鞋自己研究,其实也蛮好理解的。

就是向同级发送消息,在这个游戏物体上的所有MonoBehaviour上调用名称为methodName的方法,比如在Test1.cs脚本里面,我们有一个方法:

getMessage(string str)
{
print("receive message:"+str);//打印收到的消息
}

  然后在Test2.cs里面,我们调用

string s = "send message";
SendMessage("getMessage",s);

  运行就会收到消息。

好了我们回到easytouch,再看看参数

Ohter Receiver------------------>赋值一个object,允许你直接把消息发送到这个object上。

Joysticks & buttons--------------->是否启动Joysticks(虚拟遥控)(这个我们的以后再研究 )& buttons(按钮)

Enable NGUI compatibility---------------------->是否兼容NGUI插件,如果启动的话,我们选择NGUI的Camera和NGUI界面所在的Layer层级。注意了,一定要选择好NGUI的Camera和层级。

可能有读者不明白了,什么叫兼容NGUI?实际就是在NGUI搭建的界面点击,不会触发easytouch的触屏。举个例子,当我们点击button的时候,只触发ngui的button事件,不会触发easytouch的On_TouchStart事件,也就是说easytouch的事件被屏蔽了。

我们深入的研究一下,其他很简单,也就是在NGUI的Camera摄像机渲染到的且有Collider的GameObject,easytouch的触屏事件都被屏蔽。

Camera-------------------->用于easytouch触屏事件发射Ray碰撞的摄像机。

Enable auto-select------------->是否启用自动选择物体。

Pickable Layers------------------>就是设置可以选择物体的层级。

	// Touch start event
public void On_MyTouchStart(Gesture gesture){
Debug.Log( "Touch in " + gesture.position);//打印触摸到屏幕的坐标Vector2
if (gesture.pickObject == gameObject)//如果选择的物体是脚本上的这个物体的话,就打印他的名字
{
print(gameObject.name);
}
}

然后我们新建一个Cube,将脚本赋给它,然后设置Pickable Layers为Cube的Layer,运行点击Cube

Stationnary tolerance--------------------->静止的距离。这个是什么意思呢?当我们手指接触到屏幕,然后滑动一定的距离,那么这个距离 >= Stationnary tolerance的时候才会触发事件,注意啦,这里的事件就指的是滑动啦,拖动啦这些,单击什么的一律不管。

Long tap time-------------------->这个是长点击所需要的时间,也就是说超过这个时间,长点击的事件才会触发。

Swipe tolerance--------------->这个是滑动的精确度,介于0-1之间,0表示不精确,1表示非常精确。

接下来我来一一讲解各个手势的demo

Unity3d插件研究之Easytouch的更多相关文章

  1. Unity3D插件分享

    网上看到一个讲unity3D插件的,看着不错,转载过来. 本文汇总了近百个Unity3D插件,供大家参考下载. 2D_Toolkit_1.51 动画开发插件包 FingerGestures 触摸插件 ...

  2. 安装安卓模拟器和unity3d插件EZGUI

    一.安装安卓模拟器 1.下载安卓模拟器http://www.pc6.com/softview/SoftView_64923.html: 2.安装安卓模拟器. 3.下载安卓apk,然后右键用BlueSt ...

  3. Unity3D插件-自制小插件、简化代码便于使用(新手至高手进阶必经之路)

    Unity3D插件-简化代码.封装功能 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 1 FindT() ...

  4. Unity3d插件]EasyTouch简单使用方法

    EasyTouch使用 EasyTouch 文件夹[-] 一.效果图 二.操作步骤 1.官方文档上的步骤 2.翻译一下以上的步骤 3.依据官方的这些提示.自己来做一个属于自己的人物遥感控制 对于移动平 ...

  5. Unity插件研究-EasyTouch V5

    抽空研究了下Easy Touch 5插件,发现确实很好用,下面是相应的用法: 1. Easy Touch Controls:实现虚拟摇杆的组件 在项目的"Hierarchy"窗口下 ...

  6. [Unity3d插件]EasyTouch的初步使用

    对于移动平台上的RPG类的游戏,我们常用虚拟摇杆来控制人物角色的行走和一些行为,相信我们对它并不陌生,之前尝试了EasyTouch2.5,发现并没有最新版的3.1好用,2.5版本的对于自适应没有做的很 ...

  7. Unity3D插件之Easy Touch 3.1(1): Easy Joystick

    先看官方介绍:https://www.assetstore.unity3d.com/#/content/3322 (Allows you to quickly and easily develop a ...

  8. [Unity3D插件]2dToolKit系列三 碰撞检测功能的实现以及障碍物的随机摆放

    貌似有一段时间没更新2dtoolkit系列了,这段时间一直在忙着其他事情,今天开始继续这个插件系列的教程,网上搜索,貌似关于这个插件的教程无非还是跟官方的教程很类似,有的甚至都没有自己照着亲手实践一遍 ...

  9. [unity3d插件]2dtoolkit系列一 创建精灵

    从今天开始要做一个2d游戏,由于之前都是做cocos2dx的,然后接触了一段时间的unity3d,都是做3D方面的东西,得知要做2d游戏还是有点开心的,或许因为不想丢失之前的2d游戏的一些思想,然后接 ...

随机推荐

  1. vue知识点(1)

    处理用户输入 v-on指令添加一个事件监听器 div id="app-5"> <p>{{ message }}</p> <button v-on ...

  2. Centos7 配置网络

    /* Centos7 的网络 不可以用ifconfig获取,需要安装包 所以 .*/ //查看ip [root@master ~]# ip a /* Centos7 的网卡名字与 Centos6有区别 ...

  3. python math模块

    import math math. ceil:取大于等于x的最小的整数值,如果x是一个整数,则返回x copysign:把y的正负号加到x前面,可以使用0 cos:求x的余弦,x必须是弧度 degre ...

  4. AC日记——「SCOI2015」国旗计划 LiBreOJ 2007

    #2007. 「SCOI2015」国旗计划 思路: 跪烂Claris 代码: #include <cstdio> #include <algorithm> #define ma ...

  5. javascript高程笔记:逻辑与和逻辑或

    逻辑与和或 逻辑与 当 && 前后两个操作数都是布尔值,无可厚非,同时为true才为true.与其他强类型语言不同的是,javascript逻辑与前后的操作数可以应用于任何类型. 而且 ...

  6. 微软企业库5.0 学习之路——第七步、Cryptographer加密模块简单分析、自定义加密接口及使用—下篇

    在上一篇文章中, 我介绍了企业库Cryptographer模块的一些重要类,同时介绍了企业库Cryptographer模块为我们提供的扩展接口,今天我就要根据这些 接口来进行扩展开发,实现2个加密解密 ...

  7. 四十三 常用内建模块 base64

    Base64是一种用64个字符来表示任意二进制数据的方法. 用记事本打开exe.jpg.pdf这些文件时,我们都会看到一大堆乱码,因为二进制文件包含很多无法显示和打印的字符,所以,如果要让记事本这样的 ...

  8. 【剑指offer】面试题 15. 二进制中 1 的个数

    面试题 15. 二进制中 1 的个数 题目描述 题目:输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. Java 实现 方法一 public class Solution { // y ...

  9. 2017CCPC 杭州 J. Master of GCD【差分标记/线段树/GCD】

    给你一个n个初始元素都为1的序列和m个询问q. 询问格式为:l r x(x为2or3) 最后求1~n所有数的GCD GCD:把每个数分别分解质因数,再把各数中的全部公有质因数提取出来连乘,所得的积就是 ...

  10. 编译 Windows 版本的 Unity Mono(2017-03-12 20:59)

    上一篇说了如何编译 Android 下的 mono,这里简要说下编译 windows 版本的 mono,就是 mono.dll,Unity 版本只有一个 mono.dll,官方的 mono,好几个可执 ...