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

分类: Unity3d之插件篇 |
原创文章如需转载请注明:转载自风宇冲Unity3D教程学院
FingerGestures是一款强大的手势识别插件,支持鼠标和触控。跨平台,最新的3.0版本支持自定义手势识别。
其中CustomGestures的识别还是挺准的。测试画个一笔五角星,能被容易的识别。
其中CustomGestures的识别还是挺准的。测试画个一笔五角星,能被容易的识别。
一.安装
直接解压包,里面还有samples和对playerMaker拓展 2个子包。
二.导入Sample包
在搜索栏里输入 t:scene,
然后全选场景并拖入Build Setting里的Scenes in Build
把'Sample Browser'场景调整到第一个场景
也可在player setting里把屏幕设置为600x400
三.基本识别 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.
(5)打输出debug信息的代码
最终脚本
- using
UnityEngine; - using
System.Collections; - public
class TapTutorial : MonoBehaviour { - void
OnTap(TapGesture gesture) - {
-
Debug.Log( "Tap gesture detected at " + gesture.Position + -
". It was sent by " + gesture.Recognizer.name ); - }
- }
识别单击物体
(1)创建球,确保有collider或者trigger,位置(0,1,0),缩放(4,4,4)
(2)Gesture物体上加
ScreenRaycaster
脚本
当Ray Thickness为0时 可以是collider或者trigger,
当Ray Thickness不为0时 必须是
collider, 可以模拟厚手指的操作。
(2) 脚本变为
- using UnityEngine;
- using System.Collections;
- public class TapTutorial : MonoBehaviour {
- void OnTap( TapGesture gesture )
- {
-
if( gesture.Selection ) -
Debug.Log( "Tapped object: " + gesture.Selection.name ); -
else -
Debug.Log( "No object was tapped at " + gesture.Position ); - }
- }
之后就能检测到点击物体了,并且被点击的圆球同样的收到同名事件
识别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拷贝粘贴脚本
- using UnityEngine;
- using System.Collections;
- public class FingerEventTutor : MonoBehaviour {
- void OnFingerDown(FingerDownEvent e)
- {
- Debug.Log( e.Finger + " Down at " + e.Position + " on object:" + e.Selection );
- }
- }
点击运行,即可检测任何手指按下的事件。
每个手指事件都要添加对应的detector
- using UnityEngine;
- using System.Collections;
-
- public class FingerEventTutor : MonoBehaviour {
-
- void OnFingerDown(FingerDownEvent e)
- {
- Debug.Log( e.Finger + " Down at " + e.Position + " on object:" + e.Selection );
- }
- void OnFingerUp( FingerUpEvent e )
- {
-
// time the finger has been held down before being released -
float elapsedTime = e.TimeHeldDown; - Debug.Log( e.Finger + " Up at " + e.Position + " on object:" + e.Selection );
- }
- }
五.自定义手势识别
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物体下
- using UnityEngine;
- using System.Collections;
- public class PointCloudTutorial : MonoBehaviour
- {
-
void OnCustomGesture( PointCloudGesture gesture ) -
{ -
Debug.Log( "Recognized custom gesture: " + gesture.RecognizedTemplate.name + -
", match score: " + gesture.MatchScore + -
", match distance: " + gesture.MatchDistance ); -
} - }
PointCloudGesture.
RecognizedTemplate 对应画出的图形模板
PointCloudGesture.
MatchScore 匹配百分比,1代表完美匹配
PointCloudGesture.
MatchDistance 与图形有多接近
当然也可以从
代码绘制PointCloudGestureTemplat e
- void Awake()
- {
-
PointCloudGestureTemplat e triangle =ScriptableObject.CreateInstance<PointCloudGestureTemplat e>(); -
triangle.name = "Triangle Gesture Template"; -
triangle.BeginPoints(); -
triangle.AddPoint( 0, 1, 1 ); -
triangle.AddPoint( 0, 2, 2 ); -
triangle.AddPoint( 0, 3, 1 ); -
triangle.AddPoint( 0, 1, 1 ); -
triangle.EndPoints(); -
-
PointCloudGestureTemplat e square =ScriptableObject.CreateInstance<PointCloudGestureTemplat e>(); -
square.name = "Square Gesture Template"; -
square.BeginPoints(); -
square.AddPoint( 0, 2, 1 ); -
square.AddPoint( 0, 2, 3 ); -
square.AddPoint( 0, 4, 3 ); -
square.AddPoint( 0, 4, 1 ); -
square.AddPoint( 0, 2, 1 ); -
square.EndPoints(); -
-
PointCloudRegognizer recognizer = gameObject.AddComponent<PointCloudRegognizer>(); -
recognizer.AddTemplate( triangle ); -
recognizer.AddTemplate( square ); - }
AddPoint的
第一个参数代表第几画,但是现在只支持一笔画出来的图形,所以该值只填0。当
EndPoints()被调用的时候,所有点都会被单位化至(0,1)的范围。
可学习
delegate-based .Net events
PlayMaker Actions