Reputation: 21
everyone!.. I want build a Trivia Augmented Reality Game, AR scene is working properly on cube object. I have the game scene. ... Anyone can help me, "how to make the game scene appear when my image target is detected ?".
Upvotes: 1
Views: 1378
Reputation: 1
Here's the full code that I have written and tested on my own project. It works for me upon object scan. I placed this script in my Image Target, just make your own changes to the Scene names under the //Go to Scene, IF detect a target section. Afterwards, drag your Image Target in the Image Target component in Unity.
using UnityEngine;
using UnityEngine.Events;
using Vuforia;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
namespace Vuforia
{
/// <summary>
/// A custom handler that implements the ITrackableEventHandler interface.
/// </summary>
public class CustomsTrackableEventHandler : MonoBehaviour,
ITrackableEventHandler
{
public ImageTargetBehaviour imageTarget;
private ImageTargetBehaviour[] allImageSources;
#region PRIVATE_MEMBER_VARIABLES
private TrackableBehaviour mTrackableBehaviour;
#endregion // PRIVATE_MEMBER_VARIABLES
#region UNTIY_MONOBEHAVIOUR_METHODS
void Start()
{
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
{
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
imageTarget = (ImageTargetBehaviour)gameObject.AddComponent<ImageTargetBehaviour>();
}
#endregion // UNTIY_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)
{
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
{
OnTrackingFound();
}
else
{
OnTrackingLost();
}
}
#endregion // PUBLIC_METHODS
#region PRIVATE_METHODS
private void OnTrackingFound()
{
Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
// Enable rendering:
foreach (Renderer component in rendererComponents)
{
component.enabled = true;
}
// Enable colliders:
foreach (Collider component in colliderComponents)
{
component.enabled = true;
}
Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
//Go to Scene, IF detect a target
if (mTrackableBehaviour.TrackableName == "Scene 1")
{
SceneManager.LoadScene("YourUnityProjectName 1");
}
if (mTrackableBehaviour.TrackableName == "Scene 2")
{
SceneManager.LoadScene("YourUnityProjectName 2");
}
if (mTrackableBehaviour.TrackableName == "Scene 3")
{
SceneManager.LoadScene("YourUnityProjectName 3");
}
}
private void OnTrackingLost()
{
Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
// Disable rendering:
foreach (Renderer component in rendererComponents)
{
component.enabled = false;
}
// Disable colliders:
foreach (Collider component in colliderComponents)
{
component.enabled = false;
}
Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
}
#endregion // PRIVATE_METHODS
}
}
Upvotes: 0
Reputation: 1
Which AR SDK you are using? Vuforia or what? If you are using Vuforia, you can use ImageTargetBehaviou.CurrentStatus property. When your image target is detected, currentState should be equal to
imageTargetTemplate.CurrentStatus == TrackableBehaviour.Status.DETECTED
Then you can use SceneManager.LoadScene method as @Mukesh Saini said.
Upvotes: 0
Reputation: 1498
You can use SceneManager.LoadScene method to load a scene as follow -
SceneManager.LoadScene("GameSceneName");
Here is the documentation for SceneManager.LoadScene method.
Upvotes: 1