Log in or register to post comments

When image target is detected, trigger a new scene.

January 26, 2018 - 8:08am #1

Just curious on how I would implement this. I want to transition to a different scene when an image target is successfully recognized and is there a way to send information about the image target to the triggered scene? 

I want to create a script for it, but I don't know where to look in the documentation.

Also, when working with multiple image targets, do you just lay them out in a unity scene under the main AR camera? I would prefer to do it programmatically instead of having a hundred images laid out in a scene.

Thanks.

 

When image target is detected, trigger a new scene.

December 18, 2018 - 9:40am #3
/*==============================================================================
Copyright (c) 2017 PTC Inc. All Rights Reserved.
Copyright (c) 2010-2014 Qualcomm Connected Experiences, Inc.
All Rights Reserved.
Confidential and Proprietary - Protected under copyright and other laws.
==============================================================================*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Vuforia;
/// <summary>
/// A custom handler that implements the ITrackableEventHandler interface.
///
/// Changes made to this file could be overwritten when upgrading the Vuforia version.
/// When implementing custom event handler behavior, consider inheriting from this class instead.
/// </summary>
public class DefaultTrackableEventHandler : MonoBehaviour, ITrackableEventHandler
{
    #region PROTECTED_MEMBER_VARIABLES
    protected int indxCena;
    protected TrackableBehaviour mTrackableBehaviour;
    protected TrackableBehaviour.Status m_PreviousStatus;
    protected TrackableBehaviour.Status m_NewStatus;
    #endregion // PROTECTED_MEMBER_VARIABLES
    #region UNITY_MONOBEHAVIOUR_METHODS
    protected virtual void LoadScene()
    {
        Debug.Log("Achei:" + mTrackableBehaviour.TrackableName);
        switch (mTrackableBehaviour.TrackableName)
        {
            case "animais":
            case "Maternal_capa_01":
            if (SceneManager.GetActiveScene().name != "1_animais")
                SceneManager.LoadScene("1_animais");
            break;
            case "abelha":
            case "elefante":
            case "indio":
            case "ovelha":
            case "uva":
            if (SceneManager.GetActiveScene().name != "2_letras")
                SceneManager.LoadScene("2_letras");
            break;
            case "profissoes":
            if (SceneManager.GetActiveScene().name != "3_profissoes")
                SceneManager.LoadScene("3_profissoes");
            break;
            case "dino_capa7a":
            case "dino_capa7":
            if (SceneManager.GetActiveScene().name != "4_tecnologia")
                SceneManager.LoadScene("4_tecnologia");
            break;
            case "Maternal_capa_04a":
            if (SceneManager.GetActiveScene().name != "5_formas")
                SceneManager.LoadScene("5_formas");
            break;
            case "Maternal_capa_06":
            if (SceneManager.GetActiveScene().name != "6_brincadeiras")
                SceneManager.LoadScene("6_brincadeiras");
            break;
        }
    }
    protected virtual void Play_Sound()
    {
        // SOLUTION
        if (mTrackableBehaviour.gameObject.GetComponentInChildren<AudioSource>() != null)
        {
            mTrackableBehaviour.gameObject.GetComponentInChildren<AudioSource>().Play();
        }
    }
    protected virtual void Stop_Sound()
    {
        // SOLUTION
        if (mTrackableBehaviour.gameObject.GetComponentInChildren<AudioSource>() != null)
        {
            mTrackableBehaviour.gameObject.GetComponentInChildren<AudioSource>().Stop();
        }
    }
    protected virtual void Check_Sair()
    {
        // se pressionar botão "VOLTAR/BACK" fecha app*
        if (Application.platform == RuntimePlatform.Android)
        {
            if (Input.GetKeyUp(KeyCode.Escape))
            {
                //quit application on return button
                Application.Quit();
                return;
            }
        }
    }
    protected virtual void Start()
    {
        mTrackableBehaviour = GetComponent<TrackableBehaviour>();
        if (mTrackableBehaviour)
            mTrackableBehaviour.RegisterTrackableEventHandler(this);
    }
    protected virtual void Update()
    {
        // verifica se pressionou "ESC/VOLTAR"
        Check_Sair();
    }
    protected virtual void OnDestroy()
    {
        if (mTrackableBehaviour)
            mTrackableBehaviour.UnregisterTrackableEventHandler(this);
    }
    protected virtual void MudaCena()
    {
        {
            Debug.Log("Cenas totais: " + SceneManager.sceneCount.ToString());
            Debug.Log("Cena: " + SceneManager.GetSceneAt(indxCena).name);
            if (indxCena > SceneManager.sceneCount)
                indxCena = 0;
            else
                indxCena++;
            SceneManager.LoadScene(SceneManager.GetSceneAt(indxCena).name);
            Debug.Log("Cena: " + SceneManager.GetSceneAt(indxCena).name);
        }
    }
    #endregion // UNITY_MONOBEHAVIOUR_METHODS
    #region PUBLIC_METHODS
    /// <summary>
    ///     Implementation of the ITrackableEventHandler function called when the
    ///     tracking state changes.
    /// </summary>
    public void OnTrackableStateChanged(
        TrackableBehaviour.Status previousStatus,
        TrackableBehaviour.Status newStatus)
    {
        m_PreviousStatus = previousStatus;
        m_NewStatus = newStatus;
        if (newStatus == TrackableBehaviour.Status.DETECTED ||
            newStatus == TrackableBehaviour.Status.TRACKED ||
            newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
        {
            Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
            LoadScene();
            OnTrackingFound();
        }
        else if (previousStatus == TrackableBehaviour.Status.TRACKED &&
                 newStatus == TrackableBehaviour.Status.NO_POSE)
        {
            OnTrackingLost();
        }
        else
        {
            OnTrackingLost();
        }
    }
    #endregion // PUBLIC_METHODS
    #region PROTECTED_METHODS
    protected virtual void OnTrackingFound()
    {
        var rendererComponents = GetComponentsInChildren<Renderer>(true);
        var colliderComponents = GetComponentsInChildren<Collider>(true);
        var canvasComponents = GetComponentsInChildren<Canvas>(true);
        // Enable rendering:
        foreach (var component in rendererComponents)
            component.enabled = true;
        // Enable colliders:
        foreach (var component in colliderComponents)
            component.enabled = true;
        // Enable canvas':
        foreach (var component in canvasComponents)
            component.enabled = true;
        // Toca o som se tiver...
        Play_Sound();
    }
    protected virtual void OnTrackingLost()
    {
        var rendererComponents = GetComponentsInChildren<Renderer>(true);
        var colliderComponents = GetComponentsInChildren<Collider>(true);
        var canvasComponents = GetComponentsInChildren<Canvas>(true);
        // Disable rendering:
        foreach (var component in rendererComponents)
            component.enabled = false;
        // Disable colliders:
        foreach (var component in colliderComponents)
            component.enabled = false;
        // Disable canvas':
        foreach (var component in canvasComponents)
            component.enabled = false;
        // Para o som se tiver
        Stop_Sound();
    }
    #endregion // PROTECTED_METHODS
}

Hi everyone, I have a project with 5 scenes and in each scene there can be up to 5 target images, "book" each page displays an animation, however with SceneManeger.LoadScene ("NameScene") is not working ....

When image target is detected, trigger a new scene.

January 26, 2018 - 10:24am #2

As to the first part of your question.  Replace DefaultTrackableEventHandler on an image target with something like this.  Maybe use some static variables to pass along data.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Vuforia;

public class MyDefaultTrackableEventHandler : DefaultTrackableEventHandler {

protected override void OnTrackingFound()
{
  base.OnTrackingFound ();
  SceneManager.LoadScene("OtherSceneName");
}
}
Log in or register to post comments