【Unity插件】FingerGestures导入使用教程

发表于2017-08-24
评论0 2.5k浏览
FingerGestures是一款强大的手势识别插件,支持鼠标和触控。跨平台,最新的3.0版本支持自定义手势识别。下面和大家介绍的是Unity插件FingerGestures导入使用教程,一起来看看吧。

其中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.
  1. 5)打输出debug信息的代码  
  2. 最终脚本  
  3. using UnityEngine;  
  4. using System.Collections;  
  5. public class TapTutorial : MonoBehaviour {  
  6. void OnTap(TapGesture gesture)  
  7. {    
  8.   Debug.Log( "Tap gesture detected at "   gesture.Position     
  9.             ". It was sent by "   gesture.Recognizer.name );  
  10. }  
  11. }  
识别单击物体
(1)创建球,确保有collider或者trigger,位置(0,1,0),缩放(4,4,4)
(2)Gesture物体上加  ScreenRaycaster 脚本
当Ray Thickness为0时  可以是collider或者trigger,
当Ray Thickness不为0时  必须是collider, 可以模拟厚手指的操作。

  1. 2)  脚本变为  
  2. using UnityEngine;  
  3. using System.Collections;  
  4. public class TapTutorial : MonoBehaviour {  
  5. void OnTap( TapGesture gesture )   
  6. {  
  7.     if( gesture.Selection )  
  8.         Debug.Log( "Tapped object: "   gesture.Selection.name );  
  9.     else  
  10.         Debug.Log( "No object was tapped at "   gesture.Position );  
  11. }  
  12. }  


之后就能检测到点击物体了,并且被点击的圆球同样的收到同名事件

识别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



[csharp] view plain copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class FingerEventTutor : MonoBehaviour {  
  5.   
  6. void OnFingerDown(FingerDownEvent e)   
  7. {  
  8. Debug.Log( e.Finger   " Down at "   e.Position   " on object:"   e.Selection );  
  9. }  
  10. void OnFingerUp( FingerUpEvent e )   
  11. {  
  12.     // time the finger has been held down before being released  
  13.     float elapsedTime = e.TimeHeldDown;  
  14. Debug.Log( e.Finger   " Up at "   e.Position   " on object:"   e.Selection );  
  15. }  
  16. }  

五.自定义手势识别
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. }  
ointCloudGesture. RecognizedTemplate 对应画出的图形模板
PointCloudGesture. MatchScore 匹配百分比,1代表完美匹配
PointCloudGesture. MatchDistance 与图形有多接近

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

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引