乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

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

       有趣的永 2015-12-09

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

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

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

       

      FingerGestures研究院之初探Unity手勢操作(一) - 雨松MOMO程序研究院 - 1

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

      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()
          {
           //啟動時調(diào)用,這里開始注冊手勢操作的事件。
           //按下事件: OnFingerDown就是按下事件監(jiān)聽的方法,這個名子可以由你來自定義。方法只能在本類中監(jiān)聽。下面所有的事件都一樣?。?!
              FingerGestures.OnFingerDown += OnFingerDown;
              //抬起事件
      FingerGestures.OnFingerUp += OnFingerUp;
          //開始拖動事件
          FingerGestures.OnFingerDragBegin += OnFingerDragBegin;
              //拖動中事件...
              FingerGestures.OnFingerDragMove += OnFingerDragMove;
              //拖動結(jié)束事件
              FingerGestures.OnFingerDragEnd += OnFingerDragEnd;
      //上、下、左、右、四個方向的手勢滑動
      FingerGestures.OnFingerSwipe += OnFingerSwipe;
      //連擊事件 連續(xù)點擊事件
      FingerGestures.OnFingerTap += OnFingerTap;
      //手指觸摸屏幕中事件調(diào)用一下三個方法
      FingerGestures.OnFingerStationaryBegin += OnFingerStationaryBegin;
      FingerGestures.OnFingerStationary += OnFingerStationary;
      FingerGestures.OnFingerStationaryEnd += OnFingerStationaryEnd;
      //長按事件
      FingerGestures.OnFingerLongPress += OnFingerLongPress;
          }
          void OnDisable()
          {
           //關(guān)閉時調(diào)用,這里銷毀手勢操作的事件
           //和上面一樣
              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;
          }
          //按下時調(diào)用
          void OnFingerDown( int fingerIndex, Vector2 fingerPos )
          {
      //int fingerIndex 是手指的ID 第一按下的手指就是 0 第二個按下的手指就是1。。。一次類推。
      //Vector2 fingerPos 手指按下屏幕中的2D坐標
      //將2D坐標轉(zhuǎn)換成3D坐標
              transform.position = GetWorldPos( fingerPos );
      Debug.Log(" OnFingerDown ="  +fingerPos);
          }
      //抬起時調(diào)用
      void OnFingerUp( int fingerIndex, Vector2 fingerPos, float timeHeldDown )
      {
      Debug.Log(" OnFingerUp ="  +fingerPos);
      }
      //開始滑動
      void OnFingerDragBegin( int fingerIndex, Vector2 fingerPos, Vector2 startPos )
          {
         Debug.Log("OnFingerDragBegin fingerIndex =" + fingerIndex  + " fingerPos ="+fingerPos +"startPos =" +startPos);
          }
      //滑動結(jié)束
      void OnFingerDragEnd( int fingerIndex, Vector2 fingerPos )
      {
      Debug.Log("OnFingerDragEnd fingerIndex =" + fingerIndex  + " fingerPos ="+fingerPos);
      }
      //滑動中
          void OnFingerDragMove( int fingerIndex, Vector2 fingerPos, Vector2 delta )
          {
                 transform.position = GetWorldPos( fingerPos );
      Debug.Log(" OnFingerDragMove ="  +fingerPos);
          }
      //上下左右四方方向滑動手勢操作
      void OnFingerSwipe( int fingerIndex, Vector2 startPos, FingerGestures.SwipeDirection direction, float velocity )
          {
      //結(jié)果是 Up Down Left Right 四個方向
      Debug.Log("OnFingerSwipe " + direction + " with finger " + fingerIndex);
          }
      //連續(xù)按下事件, tapCount就是當前連續(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 有一個區(qū)別。
               //OnFingerStationary 是手指觸摸在屏幕中的事件,而OnFingerDragMove是先觸摸一下然后滑動的事件。
               //如果你需要時時捕獲手指觸摸屏幕中的事件時 用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屏幕坐標換算成3D坐標
          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 ) ) );
          }
      }

       

      如下圖所示,用鼠標還是IOS Android觸摸事件都能很好的在這個Cube上響應,大家把我的代碼手動的打一遍就什么都明白了。

       

      FingerGestures研究院之初探Unity手勢操作(一) - 雨松MOMO程序研究院 - 2

       

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

       

      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;
          }

       

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

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

      雨松MOMO祝大家學習愉快,啦啦啦。

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

      3

      最后編輯:
      作者:雨松MOMO
      專注移動互聯(lián)網(wǎng),Unity3D游戲開發(fā)
      捐 贈如果您愿意花10塊錢請我喝一杯咖啡的話,請用手機掃描二維碼即可通過支付寶直接向我捐款哦。

        本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多