Reputation: 1155
I'm working since 3 months with Unity engine and the last month I began to adding Vuforia capabilities to my Unity projects. Right now, I'm developing an application which comprises TextRecognition (Vuforia Prefab), see picture below. So, when the word "Unterwagen" is recognized, it should be load another scene that works perfectly. However, it should be a delay between this transition and I don't now how to do it. I would appreciate if someone can help me.
DefaultTrackableEvent.cs
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
namespace Vuforia
{
/// <summary>
/// A custom handler that implements the ITrackableEventHandler interface.
/// </summary>
public class DefaultTrackableEventHandler1 : MonoBehaviour,
ITrackableEventHandler
{
#region PRIVATE_MEMBER_VARIABLES
private TrackableBehaviour mTrackableBehaviour;
#endregion // PRIVATE_MEMBER_VARIABLES
public GameObject _statusTxt;
public AudioSource clickRight;
public AudioSource clickWrong;
public Text text;
#region UNTIY_MONOBEHAVIOUR_METHODS
void Start()
{
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
{
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
text.text = "Auf der Suche nach der Unterwagen Schrift";
}
#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;
}
text.text = mTrackableBehaviour.TrackableName + " erkannt";
if (mTrackableBehaviour.TrackableName == "Unterwagen") {
clickRight.Play ();
text.text = "nimm ein Stück von diesem Sichtlagerkaste";
//delay
Invoke("", 10f); //-> it doesn't work
SceneManager.LoadScene ("Scene3_Teil1_Bagger");
} else {
clickWrong.Play ();
}
Debug.Log ("Trackable " + mTrackableBehaviour.TrackableName + " found");
}
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");
text.text = "Auf der Suche nach der Unterwagen Schrift";
}
#endregion // PRIVATE_METHODS
}
}
Upvotes: 1
Views: 112
Reputation: 9821
Coroutines make this sort of delay-based logic very straight-forward:
void SomeFunction() {
StartCoroutine(WaitThenDoTheThing());
}
IEnumerator WaitThenDoTheThing() {
yield return new WaitForSeconds(NUM_SECONDS);
TheThing();
}
If you need to interrupt this for any reason, StartCoroutine
will return a Coroutine
object you may then hold on a reference to and kill with StopCoroutine(coroutineObject)
:
Coroutine routine;
void SomeFunction() {
routine = StartCoroutine(WaitThenDoTheThing());
}
IEnumerator WaitThenDoTheThing() {
yield return new WaitForSeconds(NUM_SECONDS);
TheThing();
}
void KillCoroutine() {
if (routine != null) {
StopCoroutine(routine);
}
}
Upvotes: 1