从TextRecognition到使用Unity和Vuforia加载场景

时间:2017-12-08 15:24:09

标签: c# unity3d vuforia hololens

我使用Unity引擎工作了3个月,上个月我开始为我的Unity项目添加Vuforia功能。现在,我正在开发一个包含TextRecognition(Vuforia Prefab)的应用程序,见下图。所以,当这个词出现在" Unterwagen"被识别,它应该加载另一个完美的场景。但是,这应该是这种转变之间的延迟,我现在不知道如何去做。如果有人能帮助我,我将不胜感激。

enter image description here

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

    }
}

1 个答案:

答案 0 :(得分:1)

协同程序使这种基于延迟的逻辑非常简单:

void SomeFunction() {
    StartCoroutine(WaitThenDoTheThing());
}

IEnumerator WaitThenDoTheThing() {
    yield return new WaitForSeconds(NUM_SECONDS);
    TheThing();
}

如果您因任何原因需要中断此操作,StartCoroutine将返回Coroutine对象,然后您可以保留对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);
    }
}