zoukankan      html  css  js  c++  java
  • Unity3d插件研究之Easytouch

    但我们开发移动端的游戏时,发现使用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

  • 相关阅读:
    Crazypony四轴飞行器代码框架
    Python中 sys.argv[]的用法简明解释
    详解STM32的PWM输出及频率和脉宽(占空比)的计算——寄存器配置六步曲!(转)
    jquery——所有版本下载
    jsop跨域请求方式--及中文乱码解决方案
    js ---任何浏览器关闭当前网页的代码
    jquery ajax跨域请求 IE9及以下不支持---调试
    poi---Excel导入数据-ClassNotFoundException
    ajax获取json数据为undefined--原因解析
    可变参数 / 枚举--详解
  • 原文地址:https://www.cnblogs.com/CaomaoUnity3d/p/4617293.html
Copyright © 2011-2022 走看看