User Tools

Site Tools


tutorials:net_unity:unity_mini_tutorial_registering_touch_objects

Unity Mini Tutorial: Registering Touch Objects

Introduction

Many GestureWorks Unity application register all of their scene’s touch objects at startup, and this is handled by GestureWorks Unity at initialization. Creating or destroying touch objects in the middle of a game is not as straightforward and this tutorial discusses the proper way to do so.

For this tutorial you will need the http://gestureworks.com/coreGestureworks Core multitouch framework; a free trial is available.

Download the code for all of the C# & Unity multitouch tutorials here: gestureworks-unity on GitHub.


Requirements

Estimated time to complete: 10 minutes

This tutorial assumes a basic understanding of GestureWorks setup in Unity as described in the HelloMultitouch tutorials and the Interactive Bitmap tutorial. The project MiniTutorial_RegisteringTouchObjects is also required.


Process Overview

  1. Example of creating and destroying touch objects
  2. Discussion of creating and destroying touch objects

Process Detail

1. Example of changing scenes

Copy your GestureWorks dlls to the Assets/GestureWorks/Core directory of MiniTutorial_RegisteringTouchObjects (more discussion of the process is in a previous tutorial.) Build and run MyScene.unity. Two boxes appear:

Tapping the green box creates an instance of new TouchObject called TapSphere, and tapping the red destroys a TapSphere.

Tapping a sphere illustrates it is a touch object, and changes color:

The tap sphere script is simple, it just changes to a non white color on tap:

linenums:1 |TapSphere // //
public class TapSphere : TouchObject {
 
    // Use this for initialization
    void Start () {
 
    }
 
    // Update is called once per frame
    void Update () {
 
    }
 
    void Tap(GestureEvent gEvent) {
 
        renderer.material.color = new Color(Random.Range(0.0f, 0.9f),
        Random.Range(0.0f, 0.9f), Random.Range(0.0f, 0.9f));   
    }
}

The green cube’s script is CreateCube, which has a reference to the scene’s GestureWorksScript object and uses that to register a new object by calling RegisterTouchObject:

linenums:1 |CreateCube // //
public class CreateCube : TouchObject {
 
    public GameObject testPrefab;
 
    public GestureWorksScript gestureWorks;
 
    // Use this for initialization
    void Start () {
 
    }
 
    // Update is called once per frame
    void Update () {
 
    }
 
    void Tap(GestureEvent gEvent) {
 
        Object[] spheres = FindObjectsOfType(typeof(TapSphere));
 
        Object obj = Instantiate(testPrefab, transform.position + new Vector3((spheres.Length + 1) * 1.5f, 0.0f, 0.0f),  
 Quaternion.identity);
 
        TouchObject touchObj = ((GameObject)obj).GetComponent<TouchObject>();
 
        if(touchObj && gestureWorks) {
 
            gestureWorks.RegisterTouchObject(touchObj);     
        }
    }
}

The red cube’s script is RemoveCube, and it also has a reference to the scene’s GestureWorksScript and uses that to destroy an object by calling DeregisterAndDestroyTouchObject:

linenums:1 |RemoveCube // //
public class RemoveCube : TouchObject {
 
    public GestureWorksScript gestureWorks;
 
    // Use this for initialization
    void Start () {
 
    }
 
    // Update is called once per frame
    void Update () {
 
    }
 
    void Tap(GestureEvent gEvent) {
 
        TapSphere[] spheres = FindObjectsOfType(typeof(TapSphere)) as TapSphere[];
 
        if(spheres.Length == 0)
        {
            return;
        }
 
        GameObject obj = spheres[0].gameObject;
 
        if(!obj)
        {
             return;
        }
 
        TouchObject touchObj = obj.GetComponent<TouchObject>();
 
        if(touchObj && gestureWorks) {
 
            gestureWorks.DeregisterAndDestroyTouchObject(touchObj); 
        }
    }
}

2. Discussion of creating and destroying touch objects

For touch object’s that are in a scene at startup no additional work is needed to register them as this happens defaultly during start with a GestureWorks GameObject. To use a touch object created while the game is running requires it to be manually registered with GestureWorks, and this needs to be done carefully to not conflict with current gesture processing. This is done by using the Coroutine RegisterTouchObjectRoutine in GestureWorksScript.cs, which is used by the method RegisterTouchObject. Calling RegisterTouchObject safely registers the object with GestureWorks and allows it to respond to gestures.

Removing an object’s touch gestures response requires deregistering the object with GestureWorks, which is done safely by using GestureWorksScript’s DeregisterTouchObject which uses the Coroutine DeregisterTouchObjectRoutine. To destroy an object use GestureWorksScript’s DeregisterAndDestroyTouchObject, which safely destroys a touch object after deregistering. It is recommended to destroy touch object’s with this method, as destroying a touch object that is not fully deregistered can cause a crash. This is for touch object’s that need to be destroyed mid game, at scene or game end all touch objects are deregistered by default.


Continuing Education

It is the intention of this and the other GestureWorks Core tutorials to get the programmer started using the GestureWorks core framework, and is by no means an exhaustive explanation of all of the features of Gestureworks Core; in fact, we’ve only barely scratched the surface!

Additional tutorials for learning more about GestureWorks Unity:

There is also FAQ here:

For more information on GestureWorks Core, GestureML, and CreativeML, please visit the following sites:

tutorials/net_unity/unity_mini_tutorial_registering_touch_objects.txt · Last modified: 2019/01/21 19:24 (external edit)