Unity實(shí)現(xiàn)簡(jiǎn)單手勢(shì)識(shí)別
本文實(shí)例為大家分享了Unity實(shí)現(xiàn)手勢(shì)識(shí)別的具體代碼,供大家參考,具體內(nèi)容如下
代碼很簡(jiǎn)單沒有難度,都有注解,隨便 看一看 就會(huì)了。
CallEvent () 方法需要自己搭載使用。
Unity代碼
using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> /// 手勢(shì)識(shí)別 /// </summary> public class PlayerAnimator_ZH : MonoBehaviour { //鼠標(biāo)第一次點(diǎn)擊位置 public Vector2 _MousePos; //位置枚舉 public GestureState _GestureStateBe; //最小動(dòng)作距離 private float _MinGestureDistance = 20.0f; //手勢(shì)開啟布爾 private bool _IsInvaild; void Update() { //手勢(shì)方法 GestureOnClick(); } //手勢(shì)方法 public void GestureOnClick() { //手勢(shì)為空 _GestureStateBe = GestureState.Null; if (Input.GetMouseButtonDown(0)) { //第一次鼠標(biāo)點(diǎn)擊位置記錄 _MousePos = Input.mousePosition; //開啟手勢(shì)識(shí)別 _IsInvaild = true; } if (Input.GetMouseButton(0)) { //鼠標(biāo)軌跡向量 Vector2 _Dis = (Vector2)Input.mousePosition - _MousePos; //畫線 Debug.DrawLine(_MousePos, (Vector2)Input.mousePosition, Color.cyan); //判斷當(dāng)前 向量的長(zhǎng)度 是否大于 最小動(dòng)作距離 if (_Dis.magnitude>_MinGestureDistance) { //判斷在 空間 X軸 還是在 Y軸 if (Mathf.Abs(_Dis.x) > Mathf.Abs(_Dis.y) && _IsInvaild) { if (_Dis.x > 0) { //如果當(dāng)前向量值 X 大于 0 就是 Right 狀態(tài) _GestureStateBe = GestureState.Right; } else if (_Dis.x < 0) { //如果當(dāng)前向量值 X 小于 0 就是 Lift 狀態(tài) _GestureStateBe = GestureState.Lift; } } //判斷在 空間 X軸 還是在 Y軸 else if (Mathf.Abs(_Dis.x) < Mathf.Abs(_Dis.y) && _IsInvaild) { if (_Dis.y > 0) { //如果當(dāng)前向量值 Y 大于 0 就是 Up 狀態(tài) _GestureStateBe = GestureState.Up; } else if (_Dis.y < 0) { //如果當(dāng)前向量值 Y 小于 0 就是 Down 狀態(tài) _GestureStateBe = GestureState.Down; } } //關(guān)閉手勢(shì)識(shí)別 _IsInvaild = false; } } } //呼叫事件 public void CallEvent() { switch (_GestureStateBe) { case GestureState.Null: // Null 方法調(diào)用(自己寫) break; case GestureState.Up: // Up 方法調(diào)用(自己寫) break; case GestureState.Down: // Down 方法調(diào)用(自己寫) break; case GestureState.Lift: // Lift 方法調(diào)用(自己寫) break; case GestureState.Right: // Right 方法調(diào)用(自己寫) break; default: break; } } //狀態(tài)枚舉 public enum GestureState { Null, Up, Down, Lift, Right } }
其實(shí)代碼還可進(jìn)行補(bǔ)充,比如左上、左下、右上、右下、疊加等等吧,如有問題就 Call 我吧。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)基于任務(wù)的異步編程模式
本文詳細(xì)講解了C#實(shí)現(xiàn)基于任務(wù)的異步編程模式,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04c#獲得目標(biāo)服務(wù)器中所有數(shù)據(jù)庫名、表名、列名的實(shí)現(xiàn)代碼
這篇文章主要介紹了c#獲得目標(biāo)服務(wù)器中所有數(shù)據(jù)庫名、表名、列名的方法,需要的朋友可以參考下2014-05-05C#通過PInvoke調(diào)用c++函數(shù)的備忘錄的實(shí)例詳解
這篇文章主要介紹了C#通過PInvoke調(diào)用c++函數(shù)的備忘錄的實(shí)例以及相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們學(xué)習(xí)下。2019-08-08利用thrift實(shí)現(xiàn)js與C#通訊的實(shí)例代碼
利用thrift實(shí)現(xiàn)js與C#通訊的實(shí)例代碼,需要的朋友可以參考一下2013-04-04C#讀取XML的CDATA節(jié)點(diǎn)內(nèi)容實(shí)例詳解
在本篇文章里小編給大家整理了關(guān)于C# 讀取XML的CDATA節(jié)點(diǎn)內(nèi)容的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們參考學(xué)習(xí)下。2019-09-09