免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
FingerGestures研究院之初探Unity手勢(shì)操作(一) | 雨松MOMO程序研究院

          前幾天有個(gè)朋友問我Unity手勢(shì)操作,后來我還幫他做了一個(gè)例子。我覺得在Unity中用這個(gè)手勢(shì)操作的插件會(huì)很方便。以前我只是知道FingerGestures,但是沒有深入的用過,這兩天學(xué)習(xí)了一下。真的很好用。

          最近研究了一下Unity中的一個(gè)手勢(shì)操作的插件FingerGestures。它能很方便監(jiān)聽到Unity中的各種手勢(shì)事件:上下左右四方向的滑動(dòng)事件、按下事件、抬起事件、移動(dòng)事件、連擊事件、長按事件等等。它同時(shí)支持觸摸屏操作與鼠標(biāo)操作,總起來說使用起來還是比較方便的,今天寫下教程記錄這個(gè)插件的詳細(xì)使用步驟。首先下載這個(gè)插件,大家可以在圣典上找這個(gè)插件的下載地址,當(dāng)然也可以在本文最后下載該插件。

 我看了一下這個(gè)插件底層的實(shí)現(xiàn)步驟,他是通過C#代理的形式來實(shí)現(xiàn)手勢(shì)操作的。如下圖紅圈內(nèi)所示,這五個(gè)重要的預(yù)設(shè)用來監(jiān)聽觸摸與鼠標(biāo)的手勢(shì)事件。包括:?jiǎn)问钟|摸事件、雙手觸摸事件、鼠標(biāo)事件、觸摸事件。這里我們使用一個(gè)單手的事件,如圖中所示將Finger Gertures Initializer拖拽入左側(cè)層次視圖中。

 

OK,上面我們說了該插件是通過C#代理形式來接收事件消息的,所以我們需要用腳本來注冊(cè)這些事件從而開始接收消息。接著創(chuàng)建一個(gè)立方體對(duì)象用以處理手勢(shì)操作,當(dāng)然你也可以處理游戲中的任何對(duì)象。編寫腳本FingerEvent.cs ,把這個(gè)腳本掛在這個(gè)立方體對(duì)象之上。

FingerEvent.cs腳本 

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using UnityEngine;
using System.Collections;
public class FingerEvent :  MonoBehaviour {
    void OnEnable()
    {
     //啟動(dòng)時(shí)調(diào)用,這里開始注冊(cè)手勢(shì)操作的事件。
     //按下事件: OnFingerDown就是按下事件監(jiān)聽的方法,這個(gè)名子可以由你來自定義。方法只能在本類中監(jiān)聽。下面所有的事件都一樣?。。?/div>
        FingerGestures.OnFingerDown += OnFingerDown;
        //抬起事件
FingerGestures.OnFingerUp += OnFingerUp;
    //開始拖動(dòng)事件
    FingerGestures.OnFingerDragBegin += OnFingerDragBegin;
        //拖動(dòng)中事件...
        FingerGestures.OnFingerDragMove += OnFingerDragMove;
        //拖動(dòng)結(jié)束事件
        FingerGestures.OnFingerDragEnd += OnFingerDragEnd;
//上、下、左、右、四個(gè)方向的手勢(shì)滑動(dòng)
FingerGestures.OnFingerSwipe += OnFingerSwipe;
//連擊事件 連續(xù)點(diǎn)擊事件
FingerGestures.OnFingerTap += OnFingerTap;
//手指觸摸屏幕中事件調(diào)用一下三個(gè)方法
FingerGestures.OnFingerStationaryBegin += OnFingerStationaryBegin;
FingerGestures.OnFingerStationary += OnFingerStationary;
FingerGestures.OnFingerStationaryEnd += OnFingerStationaryEnd;
//長按事件
FingerGestures.OnFingerLongPress += OnFingerLongPress;
    }
    void OnDisable()
    {
     //關(guān)閉時(shí)調(diào)用,這里銷毀手勢(shì)操作的事件
     //和上面一樣
        FingerGestures.OnFingerDown -= OnFingerDown;
FingerGestures.OnFingerUp -= OnFingerUp;
FingerGestures.OnFingerDragBegin -= OnFingerDragBegin;
        FingerGestures.OnFingerDragMove -= OnFingerDragMove;
        FingerGestures.OnFingerDragEnd -= OnFingerDragEnd;
FingerGestures.OnFingerSwipe -= OnFingerSwipe;
FingerGestures.OnFingerTap -= OnFingerTap;
FingerGestures.OnFingerStationaryBegin -= OnFingerStationaryBegin;
FingerGestures.OnFingerStationary -= OnFingerStationary;
FingerGestures.OnFingerStationaryEnd -= OnFingerStationaryEnd;
FingerGestures.OnFingerLongPress -= OnFingerLongPress;
    }
    //按下時(shí)調(diào)用
    void OnFingerDown( int fingerIndex, Vector2 fingerPos )
    {
//int fingerIndex 是手指的ID 第一按下的手指就是 0 第二個(gè)按下的手指就是1。。。一次類推。
//Vector2 fingerPos 手指按下屏幕中的2D坐標(biāo)
//將2D坐標(biāo)轉(zhuǎn)換成3D坐標(biāo)
        transform.position = GetWorldPos( fingerPos );
Debug.Log(" OnFingerDown ="  +fingerPos);
    }
//抬起時(shí)調(diào)用
void OnFingerUp( int fingerIndex, Vector2 fingerPos, float timeHeldDown )
{
Debug.Log(" OnFingerUp ="  +fingerPos);
}
//開始滑動(dòng)
void OnFingerDragBegin( int fingerIndex, Vector2 fingerPos, Vector2 startPos )
    {
   Debug.Log("OnFingerDragBegin fingerIndex =" + fingerIndex  + " fingerPos ="+fingerPos +"startPos =" +startPos);
    }
//滑動(dòng)結(jié)束
void OnFingerDragEnd( int fingerIndex, Vector2 fingerPos )
{
Debug.Log("OnFingerDragEnd fingerIndex =" + fingerIndex  + " fingerPos ="+fingerPos);
}
//滑動(dòng)中
    void OnFingerDragMove( int fingerIndex, Vector2 fingerPos, Vector2 delta )
    {
           transform.position = GetWorldPos( fingerPos );
Debug.Log(" OnFingerDragMove ="  +fingerPos);
    }
//上下左右四方方向滑動(dòng)手勢(shì)操作
void OnFingerSwipe( int fingerIndex, Vector2 startPos, FingerGestures.SwipeDirection direction, float velocity )
    {
//結(jié)果是 Up Down Left Right 四個(gè)方向
Debug.Log("OnFingerSwipe " + direction + " with finger " + fingerIndex);
    }
//連續(xù)按下事件, tapCount就是當(dāng)前連續(xù)按下幾次
void OnFingerTap( int fingerIndex, Vector2 fingerPos, int tapCount )
    {
        Debug.Log("OnFingerTap " + tapCount + " times with finger " + fingerIndex);
    }
//按下事件開始后調(diào)用,包括 開始 結(jié)束 持續(xù)中狀態(tài)只到下次事件開始!
         //OnFingerStationary 事件和  OnFingerDragMove 有一個(gè)區(qū)別。
         //OnFingerStationary 是手指觸摸在屏幕中的事件,而OnFingerDragMove是先觸摸一下然后滑動(dòng)的事件。
         //如果你需要時(shí)時(shí)捕獲手指觸摸屏幕中的事件時(shí) 用OnFingerStationary 即可
void OnFingerStationaryBegin( int fingerIndex, Vector2 fingerPos )
{
Debug.Log("OnFingerStationaryBegin " + fingerPos + " times with finger " + fingerIndex);
}
void OnFingerStationary( int fingerIndex, Vector2 fingerPos, float elapsedTime )
{
Debug.Log("OnFingerStationary " + fingerPos + " times with finger " + fingerIndex);
}
void OnFingerStationaryEnd( int fingerIndex, Vector2 fingerPos, float elapsedTime )
{
Debug.Log("OnFingerStationaryEnd " + fingerPos + " times with finger " + fingerIndex);
}
//長按事件
void OnFingerLongPress( int fingerIndex, Vector2 fingerPos )
{
Debug.Log("OnFingerLongPress " + fingerPos );
}
//把Unity屏幕坐標(biāo)換算成3D坐標(biāo)
    Vector3 GetWorldPos( Vector2 screenPos )
    {
        Camera mainCamera = Camera.main;
        return mainCamera.ScreenToWorldPoint( new Vector3( screenPos.x, screenPos.y, Mathf.Abs( transform.position.z - mainCamera.transform.position.z ) ) );
    }
}

 

如下圖所示,用鼠標(biāo)還是IOS Android觸摸事件都能很好的在這個(gè)Cube上響應(yīng),大家把我的代碼手動(dòng)的打一遍就什么都明白了。

 

 

        上面的腳本,我們是直接綁定在立方體對(duì)象上來監(jiān)聽它,如果你想在別的腳本監(jiān)聽這個(gè)立方體對(duì)象的手勢(shì)操作。只需調(diào)用如下方法即可。這個(gè)方法官方封裝在了SampleBase中。因?yàn)楣俜降睦映绦蚰_本是繼承它的,所以子類就可以直接使用父類的方法??墒?strong>SampleBase會(huì)自動(dòng)初始化一個(gè)SampleUI的腳本,不想初始化這個(gè)腳本的話直接用下面方法就行,原理就是通過射線我就不過多的解釋了。傳遞鼠標(biāo)或觸摸的2D坐標(biāo)即可得到觸摸的3D模型對(duì)象。

 

1
2
3
4
5
6
7
8
9
10
11
    // Return the GameObject at the given screen position, or null if no valid object was found
    public static GameObject PickObject( Vector2 screenPos )
    {
        Ray ray = Camera.main.ScreenPointToRay( screenPos );
        RaycastHit hit;
        if( Physics.Raycast( ray, out hit ) )
            return hit.collider.gameObject;
        return null;
    }

 

最后大家仔細(xì)看一下官方的FingerGestures.cs腳本,所有的手勢(shì)操作的事件都在這里,包括單手操作事件、雙手操作事件、鼠標(biāo)操作事件。

插件以及源碼下載地址:http://vdisk.weibo.com/s/ifRgG

雨松MOMO祝大家學(xué)習(xí)愉快,啦啦啦。

MOMO與MO嫂提醒您:親,如果您覺得本文不錯(cuò),快快將這篇文章分享出去吧 。另外請(qǐng)點(diǎn)擊網(wǎng)站頂部彩色廣告或者捐贈(zèng)支持本站發(fā)展,謝謝!

3

最后編輯:
作者:雨松MOMO
專注移動(dòng)互聯(lián)網(wǎng),Unity3D游戲開發(fā)
捐 贈(zèng)如果您愿意花10塊錢請(qǐng)我喝一杯咖啡的話,請(qǐng)用手機(jī)掃描二維碼即可通過支付寶直接向我捐款哦。
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Unity3D的FingerGesture插件
Unity3d 簡(jiǎn)單實(shí)現(xiàn)物體旋轉(zhuǎn)縮放移動(dòng)
(搬運(yùn)工)unity3D的FingerGestures插件
Kinect在Unity平臺(tái)上的開發(fā)實(shí)例
unity3d 平滑看向的腳本
unity3d腳本控制骨骼旋轉(zhuǎn)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服