github英文:https://github.com/TouchScript/TouchScript/wiki/Gestures


Gestures

In TouchScript all gesture recognition is done by components which inherit from Gesture class. Gestures are Monobehaviours, they are attached to GameObjects in scene and they receive touch input from TouchManager.

In other words if you want an object to react to taps attach TapGesture to it.

个人理解:在TouchScript中,所有的gesture 识别都是通过组件来实现的,而这些组件继承自Gesture类。Gestures是Monobehaviours,它们附载到Gameobjects上,它们从TouchManager获取touch input。

总而言之就是,你如果想要一个物体能对单击(Tap)进行响应,那么你需要吧TapGesture附载到它身上去。如下

What is a Gesture

A gesture can be either discrete or continuous.

Discrete gestures are recognized and immediately reset afterwards. An example of a discrete gesture is TapGesture. Which fires when user taps an object.

Continuous gestures continue to send events after they are recognized. An example of a continuous gesture is TransformGesture. It begins when movement starts and continues to dispatch delta changes afterwards.

A gesture is a state machine. It gets touch input and changes state when it recognizes the pattern it was made to recognize. A gesture can be in one of the following states:

  • Idle — hasn't started recognizing the pattern yet (either because there are not enough pointers on the target or the gesture is not interested in them),
  • Possible — gesture has started recognizing the pattern but hasn't succeeded yet,
  • Began — continuous gesture has begun,
  • Changed — continuous gesture updated,
  • Ended — continuous gesture ended,
  • Recognized — discrete gesture recognized its pattern,
  • Cancelled — gesture was cancelled by the system,
  • Failed — gesture failed to recognize its pattern or was forced to fail by another gesture.

If you want to know more about how gestures get pointer input, please read this article.

个人理解:一个动作可以是离散的也可以是连续的。

离散的动作可以被识别,然后在之后立刻重置。一个离散动作的例子就是tap。当用户tap到一个物体上时就会被触发。

连续的动作在被识别以后就会连续的发送事件。一个连续动作的例子就是TransformGesture。它从移动开始时就连续的分发增量。

一个动作是一个状态机。它获取到touch input 然后根据识别到的模式更改状态。一个动作可以是如下任何一种状态:

Idle--还没有开始识别模式,可能是因为目标身上还没有足够多的pointers,或者这个动作不在识别范围之内

possible--动作开始识别但是还没有成功被识别

began--连续动作开始

changed--连续动作更新

ended--连续动作结束

recognized--离散动作被识别

cancelled--动作被系统取消

failed--动作识别失败

那么动作是如何获取pointer input的呢?可以看这个篇博客:https://github.com/TouchScript/TouchScript/wiki/The-Journey-of-a-Touch-Point  (重要,对于理清TouchScript很有用)现在看不懂没关系,后面会讲到动作如何获取pointer input。

Working with gestures

After you add a Gesture to a GameObject, you should subscribe to its events. Most gestures have special events or send named messages when they are recognized. Also all of them dispatch StateChanged events.

  • To subscribe for a gesture event you just add an event handler like this:

当你在GameObject上添加了一个动作,你应该订阅到该事件中去。许多动作在被识别以后,都有对应的专门事件或者发送消息。当然他们都要分发StateChanged事件。

要订阅动作事件,你需要添加一个事件响应如下:

gesture.StateChanged += gestureStateChangedHandler;

Where the handler method must have the following signature:

事件响应方法需要的参数需要是这样的:

void gestureStateChangedHandler(object sender, GestureStateChangeEventArgs e)

GestureStateChangeEventArgs has State and PreviousState properties.

  • If you want to receive messages using Unity's SendMessage mechanism, first, you need to make sure that Use SendMessage check box is checked on the gesture. After that you need to declare a method in your script with signature:

GestureStateChangeEventArgs 有状态和先前状态的属性。

如果你想通过Unity的发送消息机制来获取消息,首先你需要确保该动作上Use SendMessage已经被勾选上了。之后你需要自己写一个函数:

void OnGestureStateChanged(Gesture sender)

Which will be called every time the gesture changes state. To find out the current state use sender.State property.

But if you are using a specific gesture, for example TapGesture, you can use its Tapped event which is essentially the same as Gesture.GestureState.Recognized but easier to remember.

Example code:

这个函数会在动作状态改变的时候被调用。要想找到目前状态,用sender.State的属性。

但是如果你用一个特殊的动作,比如说TapGesture,你需要用它的Tapped事件,该事件本质上就是Gesture.GestureState.Recognized,但是更容易记忆。来个例子:

private void OnEnable()
{GetComponent<TapGesture>().Tapped += tappedHandler;
}private void OnDisable()
{GetComponent<TapGesture>().Tapped -= tappedHandler;
}private void tappedHandler(object sender, EventArgs e)
{
}

要看更多的动作事件,这里:https://github.com/TouchScript/TouchScript/wiki/Tutorials

Gestures bundled with TouchScript

TouchScript includes the following gestures.

DISCRETE:

  1. Tap Gesture — recognizes a single/double/triple tap.
    Events: Tapped, Messages: OnTap.
  2. Press Gesture — recognizes when a user presses an object, i.e. when the first touch lands on it.
    Is automatically friendly with any other gesture.
    Events: Pressed, Messages: OnPress.
  3. Release Gesture — recognizes when a user releases an object, i.e. when the last touch is lifted off.
    Is automatically friendly with any other gesture.
    Events: Released, Messages: OnRelease.
  4. LongPress Gesture — recognizes when a user holds touches on an object for a specific time interval.
    Events: LongPressed, Messages: OnLongPress.
  5. Flick Gesture — recognizes a flick gesture in any direction.
    Events: Flicked, Messages: OnFlick.

CONTINUOUS:

  1. Transform Gesture — recognizes a transform gesture i.e. translation, rotation, scaling and their combinations.
    Events: TransformStarted, Transformed, TransformCompleted; Messages: OnTransformStart, OnTransform, OnTransformComplete.
  2. Screen Transform Gesture — same as Transform Gesture but in screen coordinates.
    Events: TransformStarted, Transformed, TransformCompleted; Messages: OnTransformStart, OnTransform, OnTransformComplete.
  3. Pinned Transform Gesture — similar to Transform Gesture but if an object was pinned to its center and couldn't move only rotate around pin point and scale.
    Events: TransformStarted, Transformed, TransformCompleted; Messages: OnTransformStart, OnTransform, OnTransformComplete.
  4. Meta Gesture — redispatches all touch events for an object it is attached to as separate events. If you are considering to use MetaGesture it's usually better to create a custom gesture instead.
    Events: TouchBegan, TouchMoved, TouchEnded, TouchCancelled; Messages: OnTouchBegan, OnTouchMoved, OnTouchEnded, OnTouchCancelled.

TransformGesture, _PinnedTransformGesture and ScreenTransformGesture use only the first one or two touch points which is fine for a small mobile device but might look odd on a big touch screen, this is why they all have Clustered* counterparts which combine touch points to clusters and work with the centers of these clusters instead of individual touches.

个人理解:意思就是TouchScript都有这些动作。

离散动作有:

1.Tap Gesture--识别单击、双击、三连击。事件:Tapped。消息:OnTap。

2.Press Gesture--识别到用户按下某个物体。比如,第一次touch。Events:Pressed。消息:OnPress。

3.Release Gesture--识别到用户释放某个物体。比如,最后一个touch离开。Events:Released。消息:OnRelease。

4.LongPress Gesture---识别到用户长按。Events:LongPressed。消息:OnLongPress。

5.Flick Gesture--识别到任何方向的滑动。Events:Flicked。消息:OnFlick。

连续动作:

1.Transform Gesture--识别到transform gesture,比如说,移动,旋转,缩放,叠加等。Events:TransformStarted, Transformed, TransformCompleted; 消息: OnTransformStart, OnTransform, OnTransformComplete.

2.Screen Transform Gesture--和transform gesture一样,但是实在屏幕坐标系下。Events:TransformStarted, Transformed, TransformCompleted; 消息: OnTransformStart, OnTransform, OnTransformComplete.

3.Pinned Transform Gesture--和transform gesture一样,但是一个物体被定在了中心,那么就不能被移动,只能围绕定住的中心旋转和缩放。Events:TransformStarted, Transformed, TransformCompleted; 消息: OnTransformStart, OnTransform, OnTransformComplete.

4.Meta Gesture--向该物体分发所有的touch events,并且是单独的分发。如果你考虑用这个动作的话,最好是自己创建一个动作。Events: TouchBegan, TouchMoved, TouchEnded, TouchCancelled; Messages: OnTouchBegan, OnTouchMoved, OnTouchEnded, OnTouchCancelled.

TransformGesture, _PinnedTransformGesture ScreenTransformGesture只使用第一个或两个接触点为小型移动设备很好但看起来奇怪的大触摸屏,这就是为什么他们都有集群*同行的接触点结合,集群和处理这些星系团的中心,而不是单独的触摸。

Making gestures work together

Usually when a gesture recognizes it stops all other gestures on the same GameObject and its parents from recognizing. The gesture which starts first gets exclusive rights. It's a good way to make sure that, for example, Transform Gesture stops Tap Gesture from recognizing when the object is being dragged.

But sometimes you want two or more gestures work together. Such gestures are called Friendlywithin the library.
There are two ways to make gestures friendly:

  1. Add one to another one's Friendly Gestures list in inspector by dragging and dropping.
  2. Call gesture1.AddFriendlyGesture(gesture2);.

It doesn't matter what gesture you added to another one's Friendly Gestures list. If one gesture is friendly with another, the second one will automatically be friendly with the first one.

个人理解:通常,当一个手势识别时,它会阻止同一游戏对象及其父对象上的所有其他手势识别。首先启动的手势获得独占权。这是一个很好的方法来确保,例如,Transform手势阻止Tap手势识别对象何时被拖动。

但有时你需要两个或更多的手势一起工作。这种手势在库中称为友好手势。

两种方法可以让手势变得友好:

1、添加一个到另一个的友好手势列表在检查器中通过拖放。

2、

gesture1.AddFriendlyGesture (gesture2);

你在别人的友好手势列表中添加了什么手势并不重要。如果一个手势对另一个手势友好,第二个手势就会自动对第一个手势友好。

Requiring a gesture to fail

If you want gesture A to recognize only when gesture B fails you can set B to A's RequireGestureToFail in inspector or via script. In this case gesture A won't recognize until gesture B fails.

It's a way to implement single and double taps on the same object.

But this behavior adds some delay to taps because double tap must fail first.

如果你想让手势A只在手势B失败时识别,你可以在检查器中或通过脚本将B设置为A的RequireGestureToFail。在这种情况下,直到手势B失败,手势A才会识别。

这是在同一对象上实现单点和双点的一种方法。

但是这种行为会给点击增加一些延迟,因为双击必须先失败。

Precise control for gesture recognition

Every gesture has IGestureDelegate Delegate property. IGestureDelegate is an interface which allows an external object to control a gesture.

If Delegate is set it gets asked every time a gesture wants to do something:

  • bool ShouldReceiveTouch(Gesture gesture, ITouch touch); is called when a new touch is going to be added to the gesture. You can discard this touch if you want.
  • bool ShouldBegin(Gesture gesture); is called when the gesture is about to begin. You can prevent it from beginning.
  • bool ShouldRecognizeSimultaneously(Gesture first, Gesture second); is called when two gestures want to work simultaneously. Usually this is ruled by Friendly Gestures property, but you can override this behavior using a Delegate.

个人理解:每个手势都有IGestureDelegate委托属性。IGestureDelegate是一个允许外部对象控制手势的接口。

如果设置了委托,每当手势想要做某事时,它都会被询问:

bool ShouldReceiveTouch(Gesture gesture, ITouch touch);当要向手势添加新触摸时调用。如果你愿意,你可以丢弃这个触摸。
bool ShouldBegin(Gesture gesture); 在手势即将开始时调用。你可以从一开始就阻止它。
bool ShouldRecognizeSimultaneously(Gesture first, Gesture second);当两个手势想要同时工作时调用。通常这是由Friendly gesture属性控制的,但您可以使用委托覆盖此行为。

这里涉及到对委托的理解,不懂委托的小伙伴可以看该博客:Unity委托简单例子

TouchScript中文---Gestures相关推荐

  1. TouchScript中文---The Journey of a Touch Point

    github英文:https://github.com/TouchScript/TouchScript/wiki/The-Journey-of-a-Touch-Point The Journey of ...

  2. TouchScript中文---Pointer Input

    本博主要是看到github上有TouchScript的基本的文档,想要翻译成中文,加深一下理解,同时写一下自己的想法. 原文博客:https://github.com/TouchScript/Touc ...

  3. Unity 触摸插件 TouchScript遇到的坑

    说在前面,我现在的项目使用TouchScript 是因为突然改需求要用触摸屏.原本TouchScript的功能根本不能满足项目用. 网上找到大部分教学都没有说怎么用Script写其他功能,所以只能一步 ...

  4. Android 4.0新特性(中文)

    Android 4.0新特性(中文) 转自http://www.eoeandroid.com/thread-103300-1-1.html android4.0 SDK发布有一段时间了,在eoe上找到 ...

  5. 中文最常用600句短语地道英文表达

    中文最常用600句短语地道英文表达 感觉很有用,需要时可以来参考参考. 2 字篇 1. 活该! serves you(him,her) right!   = you deserve (he/she d ...

  6. Unity 之大屏幕多点触控插件 TouchScript 9.0 入门指南

    在端游上大家应该多数都使用过的EasyTouch插件,在EasyTouch 5.0.17 最新版本扩展方法中我有过简单的介绍,今天说一下另一款触摸插件,它非常适合做展示类大屏幕的多点触控TouchSc ...

  7. Unity 之大屏幕多点触控插件 TouchScript 9.0 入门指南(上)

    在端游上大家应该多数都使用过的EasyTouch插件,在EasyTouch 5.0.17 最新版本扩展方法中我有过简单的介绍,今天说一下另一款触摸插件,它非常适合做展示类大屏幕的多点触控TouchSc ...

  8. Unity插件TouchScript初识

    一.下载并试玩 首先去Asset Store下载该插件,过程不多赘述,下载完并导入. 可以先来体验一下,你就明白这个插件好不好玩了,点击那个Examples.unity,然后就可以试玩了.. 二.尝试 ...

  9. 中文最常用600句短语地道英文表达(1)

    1. 活该! serves you(him,her) right! = you deserve (he/she deserves it.) e.g you failed the test? serve ...

最新文章

  1. 英特尔nuc能代替主机吗_制砂机生产的沙子可靠吗?能代替天然沙子吗?
  2. GDI对象存储和查看lib文件导出函数
  3. 做一个幸福的“生活家”:谈《心欢喜,灵快乐》
  4. Peaks加强版 黑暗爆炸 - 3551 Kruskal重构树 + 主席树
  5. linux 普通用户touch权限不够_Linux 开启指令
  6. 大数据学习——免密登录配置
  7. linux根据进程名称,查看后台任务的运行目录
  8. 马斯克自曝曾寻求苹果600亿美元收购特斯拉 但库克拒绝会面
  9. ExtJs之ExtJs.Model验证
  10. Android ListView上拉获取下一页
  11. nohup启动jar_nohup命令详解
  12. python语法基础汇总
  13. 布尔逻辑析取范式思考
  14. PG创建临时表时添加on commit drop参数
  15. jenkins-github上提交代码后构建job(十二)
  16. 微信小程序开发和APP开发有哪些区别
  17. 【第五章 | 存储器管理】《操作系统 慕课版》课后答案 + 复习
  18. 循环神经网络(RNN)简单介绍及实现(基于表面肌电信号)
  19. 三菱plc pwm指令_三菱PLC常用指令汇总,速存!
  20. 说说漏洞检测的那些事儿

热门文章

  1. 学校微机室计算机清单模板,附、教学设施设备清单.doc
  2. 宇枫资本个人如何理财投资
  3. 多功能数字钟软件C语言,多功能数字时钟
  4. 极限环matlab程序,用Matlab仿真噪声激励的FitzHugh-Nagumo系统模型.pdf
  5. Windows10要成为“史上最安全的操作系统”,还需要你对它做了这几件事
  6. Flowable全局监听器-待办消息提醒
  7. 【干货】从头捋一遍AGV的关键技术与细节
  8. calibre-web 豆瓣API失效解决
  9. 有源码的java安卓游戏_android游戏妄撮java源码
  10. 秒懂设计模式之命令模式(Command Pattern)