标签:
但我们开发移动端的游戏时,发现使用Input.GetMouseButtonDown的方法不可用,怎么办?
虽然unity3d也有自带触屏的方法,但是使用起来代价太高,什么单击,双击这些功能都要自己封装。
下面我们来讲下EasyTouch这个插件,它将所有触屏的手势,都已经写好了。
而且Easytouch也支持NGUI,使用起来十分的方便。
接下来,我们详细地学习这个插件改如何运用到我们的项目中来。
首先,我们导入easytouch插件,这里我是用3.0版本的,可能有些老了,我都没更新,但是大致的功能实际上是完全可以胜任了。
创建easytouch的步骤:
1.这里我使用的c#的脚本,js我不太熟悉。
2.完了之后你们会看到在scene中会出现EasyTouch。
3.新建一个c#脚本,来实现简单的触屏逻辑,这里注意一下,easytouch支持在editor下面进行测试,不用再发送到真机上。这也大大简化了开发者的工作。所以个人强烈推荐这个插件。
我们取名为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,观察效果。
大致步骤就是如此,我们发现我们只是测试一个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完之后发到手机上,再打开测试。
就是这个小东东:
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(按钮)
标签:
原文地址:http://www.cnblogs.com/CaomaoUnity3d/p/4617293.html