Unity-EasyTouch插件之Two Finger

今天,我们来学习下多手指触摸屏幕的事件,分别有挤压(缩放),挤压(旋转)

 

挤压(缩放):

在easytouch中,双手指挤压缩放的英文为Pinch

 

好了今天我们的测试是双手指挤压对物体进行缩放。我们测试的是Cube。

 

 

在easytouch中,分别有挤入(PinchIn)挤出(PinchOut)这这两个对立的触屏手势。

 

using UnityEngine;
using System.Collections;

public class TouchTest : MonoBehaviour {
	void OnEnable(){
		EasyTouch.On_PinchIn += On_PinchIn;//挤入
		EasyTouch.On_PinchOut += On_PinchOut;//挤出
		EasyTouch.On_PinchEnd += On_PinchEnd;//挤压结束
	}
	
	void OnDisable(){
		UnsubscribeEvent();
	}
	
	void OnDestroy(){
		UnsubscribeEvent();
	}
	
	// Unsubscribe to events
	void UnsubscribeEvent(){
		EasyTouch.On_PinchIn -= On_PinchIn;
		EasyTouch.On_PinchOut -= On_PinchOut;
		EasyTouch.On_PinchEnd -= On_PinchEnd;
	}
	
	void Start(){
	}
	private void On_PinchIn(Gesture gesture){

		if (gesture.pickObject == gameObject){
			
			float zoom = Time.deltaTime * gesture.deltaPinch;
			
			Vector3 scale = transform.localScale ;
			transform.localScale = new Vector3( scale.x - zoom, scale.y -zoom, scale.z-zoom);
		}	
	}

	private void On_PinchOut(Gesture gesture){
		if (gesture.pickObject == gameObject){
			float zoom = Time.deltaTime * gesture.deltaPinch;
			
			Vector3  scale = transform.localScale ;
			transform.localScale = new Vector3( scale.x + zoom, scale.y +zoom,scale.z+zoom);

		}
	}
	private void On_PinchEnd(Gesture gesture){
		
		if (gesture.pickObject == gameObject){
			print ("End");
		}
		
	}	
}

  这里注意一下,挤压的时候两个手指必须都接触到GameObject,不能一只有一只没有。

 

可能有些小伙伴还不知道怎么在电脑上测试,在easytoucjh的属性面板里面,我们发现按下Left Alt是默认的Pinch和Twist的按键。

 

 

挤压(旋转):

在easytouch中双手挤压旋转对应的英文为Twist

 

using UnityEngine;
using System.Collections;

public class TouchTest : MonoBehaviour {
	void OnEnable(){
		EasyTouch.On_Twist += On_Twist;
		EasyTouch.On_TwistEnd += On_TwistEnd;
	}
	
	void OnDisable(){
		UnsubscribeEvent();
	}
	
	void OnDestroy(){
		UnsubscribeEvent();
	}
	
	// Unsubscribe to events
	void UnsubscribeEvent(){
		EasyTouch.On_Twist -= On_Twist;
		EasyTouch.On_TwistEnd -= On_TwistEnd;
	}
	
	void Start(){

	}
	private void On_Twist(Gesture gesture)
	{
		if (gesture.pickObject == gameObject)
		{
			print(gesture.twistAngle);
			transform.Rotate(new Vector3(0,0,gesture.twistAngle));//旋转挤压的角度,
		}
	}
	private void On_TwistEnd(Gesture gesture)
	{
		if (gesture.pickObject == gameObject)
		{
			print("End");
		}
	}
}

  

 

posted @ 2015-07-04 17:46  草帽领  阅读(3078)  评论(9编辑  收藏  举报