【风宇冲】Unity3D教程宝典之FingerGestures

       FingerGestures

FingerGestures是一款强大的手势识别插件,支持鼠标和触控。跨平台,最新的3.0版本支持自定义手势识别。
其中CustomGestures的识别还是挺准的。测试画个一笔五角星,能被容易的识别。

一.安装
直接解压包,里面还有samples和对playerMaker拓展 2个子包。

二.导入Sample包
在搜索栏里输入 t:scene,   然后全选场景并拖入Build Setting里的Scenes in Build
把'Sample Browser'场景调整到第一个场景
也可在player setting里把屏幕设置为600x400
【风宇冲】Unity3D教程宝典之FingerGestures

三.基本识别  GestureRecognizers(检测用户输入并发送事件)
识别单击
场景里必须只有一个FingerGestures组件示例。它相当于Manager。
(1)直接新建GameObject起名Manager,并加FingerGestures脚本
(2)直接新建GameObject起名Gestures,并加TapRecognizer脚本
(3)直接新建TapTutorial.cs 并放到Gestures上
(4)在Gestures里TapRecognizer面板下,点"Copy Event To Clipboard" 把对应代码拷贝到粘贴板上,并在TapTutorial脚本里粘贴代码。
recognizer检测对应输入到后, 会在obj上发对应的 SendMessage消息。但是 SendMessage开销大,可以自己实现开销更小的delegate-based events.
【风宇冲】Unity3D教程宝典之FingerGestures
(5)打输出debug信息的代码
最终脚本
  1. using UnityEngine;
  2. using System.Collections;
  3. public class TapTutorial : MonoBehaviour {
  4. void OnTap(TapGesture gesture)
  5.  
  6.   Debug.Log( "Tap gesture detected at " + gesture.Position + 
  7.             ". It was sent by " + gesture.Recognizer.name );
  8. }
  9. }

识别单击物体
(1)创建球,确保有collider或者trigger,位置(0,1,0),缩放(4,4,4)
(2)Gesture物体上加    ScreenRaycaster  脚本
当Ray Thickness为0时  可以是collider或者trigger,
当Ray Thickness不为0时  必须是 collider, 可以模拟厚手指的操作。
【风宇冲】Unity3D教程宝典之FingerGestures
(2)  脚本变为
  1. using UnityEngine;
  2. using System.Collections;
  3. public class TapTutorial : MonoBehaviour {
  4. void OnTap( TapGesture gesture ) 
  5. {
  6.     if( gesture.Selection )
  7.         Debug.Log( "Tapped object: " + gesture.Selection.name );
  8.     else
  9.         Debug.Log( "No object was tapped at " + gesture.Position );
  10. }
  11. }
之后就能检测到点击物体了,并且被点击的圆球同样的收到同名事件

识别2手指Tap3下
设置 Required Finger Count为2
设置 Required Taps为3
如果是电脑运行,用鼠标左右键同时按模拟

四.FingerEventDetector
检测单手指的按下 松开 经过 移动 静止,与GesturesRecognizers类似,发消息,用ScreenRaycaster与物体互动。
FingerEventDetector是抽象类,各种   finger event detectors继承自它。
也可以提供finger index跟踪某指定手指。
(1)新场景,加FingerGestures管理
(2)新物体起名FingerEvent,并加FingerDownDetector
(3)新建脚本FingerEventTutor.cs, 并从FingerDownDetector拷贝粘贴脚本
  1. using UnityEngine;
  2. using System.Collections;
  3. public class FingerEventTutor : MonoBehaviour {
  4. void OnFingerDown(FingerDownEvent e) 
  5. {
  6. Debug.Log( e.Finger + " Down at " + e.Position + " on object:" + e.Selection );
  7. }
  8. }

点击运行,即可检测任何手指按下的事件。

每个手指事件都要添加对应的detector

【风宇冲】Unity3D教程宝典之FingerGestures

  1. using UnityEngine;
  2. using System.Collections;

  3. public class FingerEventTutor : MonoBehaviour {

  4. void OnFingerDown(FingerDownEvent e) 
  5. {
  6. Debug.Log( e.Finger + " Down at " + e.Position + " on object:" + e.Selection );
  7. }
  8. void OnFingerUp( FingerUpEvent e ) 
  9. {
  10.     // time the finger has been held down before being released
  11.     float elapsedTime = e.TimeHeldDown;
  12. Debug.Log( e.Finger + " Up at " + e.Position + " on object:" + e.Selection );
  13. }
  14. }


五.自定义手势识别
3.0后可以用PointCloudRecognizer来识别自定义手势。算法使用的是$P recognizer. 现在只支持single-stroke(单手指单次画),将会支持multi-strokes. 
PointCloudRecognizer将会对比一些gesture模板并返回最接近的 匹配图形,并返回分数和距离值。
PointCloud gestures手势,和缩放以及画的方向无关。但是图形的旋转必须是固定的,例如一个正着摆放的三角形你必须画正的,你画一个倒着摆放的三角形它是不能被识别的。

绘制PointCloud
(1)在Assets栏下,创建 PointCloud Gesture Template,起名 MyPCGesture
(2)点击Edit,绘制图形,然后Apply

使用PointCloud
(1)新场景,加FingerGestures管理
(2)创建新的物体起名Gestures
(3)   Gestures加  PointCloudRecognizer 组件。
属性
Max Match Distance 用户画的图形distance必须在该值之下, 设定得越小,画得就必须越精确。
Sampling Distance  两个连续手指位置的最小间距。越小表示越精确,但更多的采样
Gesture Template List  要去匹配的图形库
(4) 将 MyPCGesture加至  Gesture Template List
(5) 创建 PointCloudTutorial.cs脚本并添加至 Gestures物体下
  1. using UnityEngine;
  2. using System.Collections;
  3. public class PointCloudTutorial : MonoBehaviour
  4. {
  5.     void OnCustomGesture( PointCloudGesture gesture ) 
  6.     {
  7.         Debug.Log( "Recognized custom gesture: " + gesture.RecognizedTemplate.name + 
  8.             ", match score: " + gesture.MatchScore + 
  9.             ", match distance: " + gesture.MatchDistance );
  10.     }
  11. }
PointCloudGesture.  RecognizedTemplate 对应画出的图形模板
PointCloudGesture.  MatchScore 匹配百分比,1代表完美匹配
PointCloudGesture.  MatchDistance 与图形有多接近

当然也可以从 代码绘制PointCloudGestureTemplate 
  1. void Awake()
  2. {
  3.     PointCloudGestureTemplate triangle =ScriptableObject.CreateInstance<PointCloudGestureTemplate>();
  4.     triangle.name = "Triangle Gesture Template";
  5.     triangle.BeginPoints();
  6.     triangle.AddPoint( 011 );
  7.     triangle.AddPoint( 022 );
  8.     triangle.AddPoint( 031 );
  9.     triangle.AddPoint( 011 );
  10.     triangle.EndPoints();
  11.  
  12.     PointCloudGestureTemplate square =ScriptableObject.CreateInstance<PointCloudGestureTemplate>();
  13.     square.name = "Square Gesture Template";
  14.     square.BeginPoints();
  15.     square.AddPoint( 021 );
  16.     square.AddPoint( 023 );
  17.     square.AddPoint( 043 );
  18.     square.AddPoint( 041 );
  19.     square.AddPoint( 021 );
  20.     square.EndPoints();
  21.  
  22.     PointCloudRegognizer recognizer = gameObject.AddComponent<PointCloudRegognizer>();
  23.     recognizer.AddTemplate( triangle );
  24.     recognizer.AddTemplate( square );
  25. }
AddPoint的 第一个参数代表第几画,但是现在只支持一笔画出来的图形,所以该值只填0。当 EndPoints()被调用的时候,所有点都会被单位化至(0,1)的范围。


可学习
delegate-based .Net events
PlayMaker Actions

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值