Unity-EasyTouch插件之Two Finger

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

 

挤压(缩放):

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

 

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

 

 

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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 @   草帽领  阅读(3103)  评论(9编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示