So earlier this month I could finally push the Unity Hacks example code and slide show project live – after one of the videos of the presentation was pushed live.
The project is a pretty big treasure trove of handy scripts though and I didn’t like just tossing it out there and let people pick it apart on their own(feel free to go excavating if you learn better that way though). Therefore I plan for the next couple of posts here to cover various bits and bops from that project.
Note that a bunch of these scripts – for instance the one I cover today – was at the time I was still at Unity Technologies slated for the new Standard Assets shipped with Unity. Since I left, I obviously can’t say if or when these are shipped and in which form. What’s in the Unity Hacks project is pretty much the last iteration I did on them before leaving.
TouchGestures.cs
The purpose of this component is to facilitate easy handling of common gestures on touch enabled devices. With one instance of the component placed in a scene, you can specify which gestures to track, some key measuring caps with sensible defaults and a list of MonoBehaviours interested in receiving notification when a gesture has been detected.
An example of how to handle gesture tracking event can be found in this particular snippet from the presentation Control.cs script from Unity Hacks:
This handler responds to single swipes, checking for strictly left or right swipes, handling them by progressing to the next slide/build step or jumping to the previous slide.
Great, so what do the different configuration values mean? First, let’s take a look at the tracking clamps:
- maxDuration: If a tracked touch remains active for longer than this time, it is no longer considered as part of a gesture. This avoids misinterpreting drags or virtual joysticks.
- min[Pinch/Swipe]ScreenTravel: How much of the normalized screen size should a pinch or a swipe travel before it is considered valid? This avoids misinterpreting taps as mini pinches or swipes.
- maxTapScreenTravel: What is the maximum normalized screen movement a tap gesture can have before it is no longer considered a tap?
- maxTapDuration: For how long can a tracked touch be live before it is no longer considered a tap?
The last elements are somewhat less finicky and tweak happy, though the tracking clamps should have sensible default values:
- receivers: The list of MonoBehaviours interested in receiving a callback when a gesture has been identified.
- trackingMask: This is a bitmask identifying which gestures you are interested in tracking. If you only need to deal with swipes, there is no need to spend time tracking any other gestures. Note that in order for this field to be set correctly, it is important that the custom inspector for the TouchGestures component is used.
- trackMultiple: Should the gesture tracker allow multiple gestures to occur at the same time? Like a pinch and a swipe in different ends of the screen.
And that is about it! You should have what you need to start using the TouchGestures component. Hopefully you will find it useful.