扫描图像目标时调用场景

时间:2017-04-19 04:34:01

标签: c# android unity3d augmented-reality android-augmented-reality

大家好!..  我想建立一个Trivia增强现实游戏,AR场景在立方体对象上正常工作。我有游戏场景。 ... 任何人都可以帮助我,“当检测到我的图像目标时如何使游戏场景出现?”。

3 个答案:

答案 0 :(得分:2)

您可以使用 SceneManager.LoadScene 方法加载场景,如下所示 -

SceneManager.LoadScene("GameSceneName");

Here SceneManager.LoadScene 方法的文档。

答案 1 :(得分:0)

您使用的是哪种AR SDK? Vuforia还是什么? 如果您使用的是Vuforia,则可以使用ImageTargetBehaviou.CurrentStatus属性。 检测到图像目标时,currentState应等于

    imageTargetTemplate.CurrentStatus == TrackableBehaviour.Status.DETECTED

然后你可以使用SceneManager.LoadScene方法作为@Mukesh Saini说。

答案 2 :(得分:0)

这是我在自己的项目上编写和测试的完整代码。在对象扫描时它对我有用。我将此脚本放在“图像目标”中,只要对 //转到场景下的场景名称进行自己的更改,如果检测到目标部分即可。然后,将您的Image Target拖到Unity的Image Target组件中。

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
    }
}