User Tools

Site Tools


tutorials:net_unity:unity_mini_tutorial_registering_touch_objects

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
tutorials:net_unity:unity_mini_tutorial_registering_touch_objects [2015/09/10 13:28]
glass [Continuing Education]
tutorials:net_unity:unity_mini_tutorial_registering_touch_objects [2019/01/21 19:24] (current)
Line 1: Line 1:
 +[[tutorials/​net_unity|{{ :​unity_3d_logo.png }}]]
  
 +====== 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 [[http://​files.gestureworks.com/​downloads/​Core/​Trial/​GestureworksCoreTrialSetup.exe|free trial]] is available.
 +
 +Download the code for all of the C# & Unity multitouch tutorials here: [[https://​github.com/​ideum/​gestureworks-unity/​|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 =====
 +
 +  - Example of creating and destroying touch objects
 +  - Discussion of creating and destroying touch objects
 +
 +----
 +
 +
 +===== Process Detail =====
 +
 +==== 1. Example of changing scenes ====
 +
 +
 +Copy your GestureWorks dlls to the <​html><​font face="​Courier New">​Assets/​GestureWorks/​Core</​font></​html>​ directory of MiniTutorial_RegisteringTouchObjects (more discussion of the process is in a previous tutorial.) Build and run <​html><​font face="​Courier New">​MyScene.unity</​font></​html>​. Two boxes appear:
 +
 +{{ :​tutorials:​net_unity:​777px-unity_mini_register.1_a.png?​direct |}}
 +
 +Tapping the green box creates an instance of new TouchObject called TapSphere, and tapping the red destroys a TapSphere.
 +
 +{{ :​tutorials:​net_unity:​unity_mini_register.1_b.png?​direct |}}
 +
 +Tapping a sphere illustrates it is a touch object, and changes color:
 +
 +{{ :​tutorials:​net_unity:​unity_mini_register.1_c.png?​direct |}}
 +
 +The tap sphere script is simple, it just changes to a non white color on tap:
 +
 +<​code:​xml 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)); ​  
 +    }
 +}
 +</​code>​
 +
 +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:​
 +
 +<​code:​xml 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); ​    
 +        }
 +    }
 +}
 +</​code>​
 +
 +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:​
 +
 +<​code:​xml 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); ​
 +        }
 +    }
 +}
 +</​code>​
 +
 +==== 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:
 +
 +  * [[tutorials:​net_unity:​unity_mini_tutorial_switching_scenes|Unity Mini Tutorial: Switching Scenes]]
 +  * [[tutorials:​net_unity:​unity_mini_tutorial_movement_related_to_camera|Unity Mini Tutorial: Movement Related to Camera]]
 +
 +There is also FAQ here:
 +
 +  * [[tutorials:​net_unity:​gestureworks_faq|Unity GestureWorks FAQ]]
 +
 +For more information on GestureWorks Core, GestureML, and CreativeML, please visit the following sites:
 +
 +  * [[http://​wiki.gestureworks.com]]
 +  * [[http://​gestureml.org]]
 +  * [[http://​creativeml.org]]