Unity实现简单手势识别
作者:Maddie_Mo 时间:2022-10-14 07:52:05
本文实例为大家分享了Unity实现手势识别的具体代码,供大家参考,具体内容如下
代码很简单没有难度,都有注解,随便 看一看 就会了。
CallEvent () 方法需要自己搭载使用。
Unity代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 手势识别
/// </summary>
public class PlayerAnimator_ZH : MonoBehaviour
{
//鼠标第一次点击位置
public Vector2 _MousePos;
//位置枚举
public GestureState _GestureStateBe;
//最小动作距离
private float _MinGestureDistance = 20.0f;
//手势开启布尔
private bool _IsInvaild;
void Update()
{
//手势方法
GestureOnClick();
}
//手势方法
public void GestureOnClick()
{
//手势为空
_GestureStateBe = GestureState.Null;
if (Input.GetMouseButtonDown(0))
{
//第一次鼠标点击位置记录
_MousePos = Input.mousePosition;
//开启手势识别
_IsInvaild = true;
}
if (Input.GetMouseButton(0))
{
//鼠标轨迹向量
Vector2 _Dis = (Vector2)Input.mousePosition - _MousePos;
//画线
Debug.DrawLine(_MousePos, (Vector2)Input.mousePosition, Color.cyan);
//判断当前 向量的长度 是否大于 最小动作距离
if (_Dis.magnitude>_MinGestureDistance)
{
//判断在 空间 X轴 还是在 Y轴
if (Mathf.Abs(_Dis.x) > Mathf.Abs(_Dis.y) && _IsInvaild)
{
if (_Dis.x > 0)
{
//如果当前向量值 X 大于 0 就是 Right 状态
_GestureStateBe = GestureState.Right;
}
else if (_Dis.x < 0)
{
//如果当前向量值 X 小于 0 就是 Lift 状态
_GestureStateBe = GestureState.Lift;
}
}
//判断在 空间 X轴 还是在 Y轴
else if (Mathf.Abs(_Dis.x) < Mathf.Abs(_Dis.y) && _IsInvaild)
{
if (_Dis.y > 0)
{
//如果当前向量值 Y 大于 0 就是 Up 状态
_GestureStateBe = GestureState.Up;
}
else if (_Dis.y < 0)
{
//如果当前向量值 Y 小于 0 就是 Down 状态
_GestureStateBe = GestureState.Down;
}
}
//关闭手势识别
_IsInvaild = false;
}
}
}
//呼叫事件
public void CallEvent()
{
switch (_GestureStateBe)
{
case GestureState.Null:
// Null 方法调用(自己写)
break;
case GestureState.Up:
// Up 方法调用(自己写)
break;
case GestureState.Down:
// Down 方法调用(自己写)
break;
case GestureState.Lift:
// Lift 方法调用(自己写)
break;
case GestureState.Right:
// Right 方法调用(自己写)
break;
default:
break;
}
}
//状态枚举
public enum GestureState
{
Null,
Up,
Down,
Lift,
Right
}
}
其实代码还可进行补充,比如左上、左下、右上、右下、叠加等等吧,如有问题就 Call 我吧。
来源:https://blog.csdn.net/weixin_43925843/article/details/105582675
标签:unity,手势识别
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#实现只运行单个实例应用程序的方法(使用VB.Net的IsSingleInstance)
2023-06-16 07:54:29
Java基于TCP方式的二进制文件传输
2023-06-04 18:23:52
![](https://img.aspxhome.com/file/2023/9/68219_0s.png)
详解Android Flutter中SliverAppBar的使用教程
2023-06-23 12:11:27
![](https://img.aspxhome.com/file/2023/7/97867_0s.jpg)
Java使用DualPivotQuicksort排序
2022-05-22 20:58:12
Java 实战项目之小说在线阅读系统的实现流程
2022-01-31 07:41:49
![](https://img.aspxhome.com/file/2023/4/86824_0s.png)
JAVA OutputStreamWriter流的实现
2021-08-12 19:45:47
WPF使用DrawingContext实现二维绘图
2021-06-08 15:04:27
C#获取网页源代码的方法
2023-06-19 05:43:44
Android解决所有双击优化的问题
2023-07-27 09:07:03
![](https://img.aspxhome.com/file/2023/0/86010_0s.png)
Springboot 异步任务和定时任务的异步处理
2022-08-20 07:21:12
![](https://img.aspxhome.com/file/2023/8/64928_0s.webp)
深入c# Func委托的详解
2022-02-15 05:44:51
SpringBoot2 Jpa 批量删除功能的实现
2023-06-18 04:54:35
![](https://img.aspxhome.com/file/2023/7/80907_0s.jpg)
Spring的连接数据库以及JDBC模板(实例讲解)
2023-03-21 05:56:36
![](https://img.aspxhome.com/file/2023/1/63101_0s.png)
浅谈java的接口和C++虚类的相同和不同之处
2023-08-05 12:01:51
SpringBoot中@ConditionalOnBean实现原理解读
2023-04-25 14:24:54
![](https://img.aspxhome.com/file/2023/2/82432_0s.png)
在Spring Boot2中使用CompletableFuture的方法教程
2022-09-27 14:18:51
Java中静态代码块、构造代码块、构造函数和普通代码块的区别
2023-11-25 10:09:06
![](https://img.aspxhome.com/file/2023/8/60158_0s.png)
深入理解spring boot 监控
2023-11-04 02:25:15
![](https://img.aspxhome.com/file/2023/0/67490_0s.png)
解决Weblogic部署war找不到spring配置文件的问题
2022-12-29 07:03:08
![](https://img.aspxhome.com/file/2023/2/83492_0s.png)
SpringBoot定制三种错误页面及错误数据方法示例
2022-03-10 01:15:55
![](https://img.aspxhome.com/file/2023/7/61387_0s.png)