欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Unity實(shí)現(xiàn)簡(jiǎn)單手勢(shì)識(shí)別

 更新時(shí)間:2020年07月12日 15:38:43   作者:Maddie_Mo  
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)簡(jiǎn)單手勢(shì)識(shí)別,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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)文章

最新評(píng)論