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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM