Unity - Scene swap and DataSet persistence

October 14, 2013 - 9:27am #1

This article describes a basic example of implementing an app with multiple scenes, where:

  • one scene contains an Image Target
  • another scene contains an Image Target + a Virtual Button
  • the sample makes use of the Keep Alive Behaviour, allowing the ARCamera prefab (and the associated datasets) to persist when switching across different scenes

The sample logic will be as follows:

A main scene with the ARCamera is started; these scenes will show a UI with 2 buttons in overlay; each button will allow to load a scene, i.e. either the scene with the Image Target, or the scene with the ImageTarget+VirtualButton.

Scene setup

For this sample, create a New Project and import the Vuforia Unity Extension package

Create 3 scenes:

  • The first scene only includes an ARCamera object; the DataSetLoadBehaviour attached to the camera is configured so to Load and Activate a dataset of your choice (e.g. ChipsAndStones from the ImageTargets sample); for ease of reference, we call this scene as “InitScene”; also, under the Keep Alive Behaviour component, enable (tick) the “Keep AR Camera Alive” checkbox.
  • The second scene (call it “ImageTargetScene” for instance) only contains an ImageTarget, configured to use a target from the Dataset of the ARCamera (see previous scene), for instance the “chips” target from the ChipsAndStones dataset (or any other target of your choice, as long as the dataset has been loaded and activated in the ARCamera in the “InitScene”)
  • The third scene (call it “VirtualButtonScene” for instance), contains the same ImageTarget as the second scene, plus a Virtual Button as a child of the target

In your second and third scene, you’ll also want to add some augmentation object to the ImageTarget (e.g. a teapot), and to have some lighting (e.g. a directional light).


Important note:

the second and third scenes (“ImageTargetScene” and “VirtualButtonScene”) must not include any ARCamera, as this is already included in the first scene (“InitScene”) and “kept alive” from such scene; if you have any ARCamera in the second or third scene, make sure to remove them.

Open the “InitScene” and attach a script with the following code to the ARCamera object (this code allows showing the Buttons to selectively load one of the other 2 scenes):

public class SceneLoader : MonoBehaviour {

    void OnGUI() {
      if (GUI.Button (new Rect(50,50,300,50), "Load Image Target Scene")) {       
       if (GUI.Button (new Rect(50,100,300,50), "Load Virtual Buttons Scene")) {
       GUI.Box (new Rect(50,200,300,30), "Current scene: " + Application.loadedLevelName);

Finally, in the Virtual Button scene, add a custom Virtual Button event handler to the Image Target, for instance something like the following:

public class VBEventHandler : MonoBehaviour, IVirtualButtonEventHandler {

	// Use this for initialization
	void Start () {
		VirtualButtonBehaviour vbb = GetComponentInChildren<VirtualButtonBehaviour>();
		if (vbb != null) {
			Debug.Log ("Registering VB event handler");
	public void OnButtonPressed(VirtualButtonBehaviour vb) {
		Debug.Log ("Virtual Button Pressed");

    public void OnButtonReleased(VirtualButtonBehaviour vb) {
		Debug.Log ("Virtual Button Released");

Make sure to add the 3 scenes to your Build Settings in this order:

  • InitScene
  • ImageTargetScene
  • VirtualButtonScene

Then run the sample, starting from the InitScene, and press any of the buttons to switch back and forth among the Image Target and the Virtual Button scenes.

Topic locked