[[tutorials/net_unity|{{ :unity_3d_logo.png }}]]
====== Unity Mini Tutorial: Switching Scenes ======
===== Introduction =====
Switching scenes is a common method to load a new level in a game or go to a different activity area. In order to do this properly with Unity and GestureWorks together, gestures need to be unloaded and reset to avoid problems. This tutorial discusses the proper way to switch scenes using GestureWorks Unity.
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_ChangingScenes is also required.
----
===== Process Overview =====
- [[tutorials:net_unity:unity_mini_tutorial_switching_scenes#example_of_changing_scenes|Example of changing scenes]]
- [[tutorials:net_unity:unity_mini_tutorial_switching_scenes#discussion_of_changing_scenes|Discussion of changing scenes]]
----
===== Process Detail =====
==== 1. Example of changing scenes ====
Copy your GestureWorks dlls to the Assets/GestureWorks/Core directory of MiniTutorial_ChangingScenes (more discussion of the process is in a previous tutorial.)
The project contains two scene files, MyScene.unity and MySceneB.unity. Open MyScene.unity and built and run. There will be two sprites with a button displayed:
{{ :tutorials:net_unity:777px-unity_mini_scenes.1_a.png?direct |}}
Tapping the button will switch scenes. This is shown by the bitmap objects being tinted red in the other scene:
{{ :tutorials:net_unity:777px-unity_mini_scenes.1_b.png?direct |}}
The button code that switches the scene is in Scripts/SwitchScenes.cs and is as follows:
using UnityEngine;
using System.Collections;
using GestureWorksCoreNET;
using GestureWorksCoreNET.Unity;
public class SwitchScenes : TouchObject {
public GestureWorksScript gestureWorks;
private bool switchedScenes = false;
public void Tap(GestureEvent gEvent)
{
Switch();
}
private void Switch()
{
if(switchedScenes || gestureWorks == null)
{
return;
}
switchedScenes = true;
gestureWorks.SwitchScenes("MySceneB");
}
}
The object responds to touch or drag objects by calling the Switch method. The button object contains a reference to the scene’s GestureWorks GameObject and calls the SwitchScenes method on it to change scenes. This is different than the standard Unity way of switching scene files by calling:
Application.loadedLevel(“sceneName”);
==== 2. Discussion of changing scenes ====
Changing scenes with GestureWorks should always be done through the SwitchScene methods in the GestureWorks GameObject. The process for switching scenes involves many steps of waiting for existing gestures to process and clearing out gestures and touch events. Calling SwitchScene handles all of these steps, not calling it and using Unity’s Application.LoadLevel directly may cause a crash (and maybe not on every game run but occasionally.) If more fine control of switching scenes is required the Coroutine UnloadScene, which SwitchScenes calls, in the GestureWorksScript can be used directly.
----
===== 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_movement_related_to_camera|Unity Mini Tutorial: Movement Related to Camera]]
* [[tutorials:net_unity:unity_mini_tutorial_registering_touch_objects|Unity Mini Tutorial: Registering Touch Objects]]
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]]