【初识 EasyTouch】 (二)单指手势识别

【初识 EasyTouch】 (二)单指手势识别


添加EasyTouch

【初识 EasyTouch】 (二)单指手势识别_第1张图片

Enable auto-select属性


Enable auto-select,勾选后可以通过gesture.pickObject  来从Pickable Layers中选择的Layer中得到选中的游戏体(可以选择Everything),如果点选的物体不在Pickable Layers中选择的Layer中,则gestrue.PickObject为null


为物体注册点击事件回调函数

点击事件

using UnityEngine;
using System.Collections;

public class TouchStart : MonoBehaviour {
	
	private TextMesh textMesh;
	
	// Subscribe to events
	void OnEnable(){
		EasyTouch.On_TouchStart += On_TouchStart;
		EasyTouch.On_TouchDown += On_TouchDown;
		EasyTouch.On_TouchUp += On_TouchUp;
	}

	void OnDisable(){
		UnsubscribeEvent();
	}
	
	void OnDestroy(){
		UnsubscribeEvent();
	}
	
	void UnsubscribeEvent(){
		EasyTouch.On_TouchStart -= On_TouchStart;
		EasyTouch.On_TouchDown -= On_TouchDown;
		EasyTouch.On_TouchUp -= On_TouchUp;
	}
	
	void Start () {
		textMesh =(TextMesh) transform.Find("TexttouchStart").transform.gameObject.GetComponent("TextMesh");
	}
	
	// At the touch beginning 
	public void On_TouchStart(Gesture gesture){
		
		// Verification that the action on the object
		if (gesture.pickObject == gameObject)
			gameObject.renderer.material.color = new Color( Random.Range(0.0f,1.0f),  Random.Range(0.0f,1.0f), Random.Range(0.0f,1.0f));
	}
	
	// During the touch is down
	public void On_TouchDown(Gesture gesture){
		
		// Verification that the action on the object
		if (gesture.pickObject == gameObject)
			textMesh.text = "Down since :" + gesture.actionTime.ToString("f2");
	}
	
	// At the touch end
	public void On_TouchUp(Gesture gesture){
		
		// Verification that the action on the object
		if (gesture.pickObject == gameObject){
			gameObject.renderer.material.color = new Color( 1f,1f,1f);
			textMesh.text ="Touch Start/Up";
		}
	}
}


轻敲事件

using UnityEngine;
using System.Collections;

public class Tap : MonoBehaviour {
	
	// Subscribe to events
	void OnEnable(){
		EasyTouch.On_SimpleTap += On_SimpleTap;
	}

	void OnDisable(){
		UnsubscribeEvent();
	}
	
	void OnDestroy(){
		UnsubscribeEvent();
	}
	
	void UnsubscribeEvent(){
		EasyTouch.On_SimpleTap -= On_SimpleTap;	
	}
	
	// Simple tap
	private void On_SimpleTap( Gesture gesture){
		
		// Verification that the action on the object
		if (gesture.pickObject == gameObject){
			gameObject.renderer.material.color = new Color( Random.Range(0.0f,1.0f),  Random.Range(0.0f,1.0f), Random.Range(0.0f,1.0f));
		}
	}
}


双击事件

using UnityEngine;
using System.Collections;

public class DoubleTap : MonoBehaviour {
	
	// Subscribe to events
	void OnEnable(){
		EasyTouch.On_DoubleTap += On_DoubleTap;	
	}

	void OnDisable(){
		UnsubscribeEvent();
	}
	
	void OnDestroy(){
		UnsubscribeEvent();
	}
	
	void UnsubscribeEvent(){
		EasyTouch.On_DoubleTap -= On_DoubleTap;	
	}	
	
	// Double tap  
	private void On_DoubleTap( Gesture gesture){
		
		// Verification that the action on the object
		if (gesture.pickObject == gameObject){
			
	 		gameObject.renderer.material.color = new Color( Random.Range(0.0f,1.0f),  Random.Range(0.0f,1.0f), Random.Range(0.0f,1.0f));
		}
	}
}


长按事件

using UnityEngine;
using System.Collections;

public class LongTap : MonoBehaviour {

	private TextMesh textMesh;
	
	// Subscribe to events
	void OnEnable(){
		EasyTouch.On_LongTapStart += On_LongTapStart;
		EasyTouch.On_LongTap += On_LongTap;
		EasyTouch.On_LongTapEnd += On_LongTapEnd;
	}

	void OnDisable(){
		UnsubscribeEvent();
	}
	
	void OnDestroy(){
		UnsubscribeEvent();
	}
	
	void UnsubscribeEvent(){
		EasyTouch.On_LongTapStart -= On_LongTapStart;
		EasyTouch.On_LongTap -= On_LongTap;
		EasyTouch.On_LongTapEnd -= On_LongTapEnd;
	}
	
	void Start(){
		
		textMesh = transform.Find("TextLongTap").transform.gameObject.GetComponent("TextMesh") as TextMesh;
	}
	
	// At the long tap beginning 
	private void On_LongTapStart( Gesture gesture){
		
		// Verification that the action on the object
		if (gesture.pickObject==gameObject){
			gameObject.renderer.material.color = new Color( Random.Range(0.0f,1.0f),  Random.Range(0.0f,1.0f), Random.Range(0.0f,1.0f));
		}
	}
	
	// During the long tap 
	private void On_LongTap( Gesture gesture){
		
		// Verification that the action on the object
		if (gesture.pickObject==gameObject){
			textMesh.text = gesture.actionTime.ToString("f2");
		}
	
	}
	
	// At the long tap end
	private void On_LongTapEnd( Gesture gesture){
		
		// Verification that the action on the object
		if (gesture.pickObject==gameObject){
			gameObject.renderer.material.color = Color.white;
			textMesh.text="Long tap";
		}
	
	}

}


拖拽事件

using UnityEngine;
using System.Collections;

public class Drag : MonoBehaviour {

	private TextMesh textMesh;
	private Vector3 deltaPosition;

	
	// Subscribe to events
	void OnEnable(){
		EasyTouch.On_Drag += On_Drag;
		EasyTouch.On_DragStart += On_DragStart;
		EasyTouch.On_DragEnd += On_DragEnd;
	}

	void OnDisable(){
		UnsubscribeEvent();
	}
	
	void OnDestroy(){
		UnsubscribeEvent();
	}
	
	void UnsubscribeEvent(){
		EasyTouch.On_Drag -= On_Drag;
		EasyTouch.On_DragStart -= On_DragStart;
		EasyTouch.On_DragEnd -= On_DragEnd;
	}	
	
	
	void Start(){
		textMesh = transform.Find("TextDrag").transform.gameObject.GetComponent("TextMesh") as TextMesh;
	}
	
	// At the drag beginning 
	void On_DragStart( Gesture gesture){
		
		// Verification that the action on the object
		if (gesture.pickObject == gameObject){
			gameObject.renderer.material.color = new Color( Random.Range(0.0f,1.0f),  Random.Range(0.0f,1.0f), Random.Range(0.0f,1.0f));
		
			// the world coordinate from touch for z=5
			Vector3 position = gesture.GetTouchToWordlPoint(5);
			
			
			deltaPosition = position - transform.position;
		}	
	}
	
	// During the drag
	void On_Drag(Gesture gesture){
	
		// Verification that the action on the object
		if (gesture.pickObject == gameObject){
			
			// the world coordinate from touch for z=5
			Vector3 position = gesture.GetTouchToWordlPoint(5);
			
			transform.position = position - deltaPosition;
			
			// Get the drag angle
			float angle = gesture.GetSwipeOrDragAngle();
			
			textMesh.text = gesture.swipe.ToString() + " / angle :" + angle.ToString("f2");
		}
	}
	
	// At the drag end
	void On_DragEnd(Gesture gesture){
	
		// Verification that the action on the object
		if (gesture.pickObject == gameObject){
			transform.position= new Vector3(3f,1.8f,-5f);
			gameObject.renderer.material.color = Color.white;
			textMesh.text="Drag me";
		}
	}
}



几种事件监听的方式大同小异
OnEnable()函数中注册事件,并告知每个事件的回调函数,这样就可以在回调函数中做具体的事情了
OnDisable()函数中取消事件的监听

可供监听的事件在EasyTouch.cs中有详细说明,感兴趣的童鞋可以看下






你可能感兴趣的:(【初识 EasyTouch】 (二)单指手势识别)