【问题标题】:How can I make the DefaultTrackableEventHandler script to check if the randomly selected game object is same as the game object shown to the camera?如何使 DefaultTrackableEventHandler 脚本检查随机选择的游戏对象是否与显示给相机的游戏对象相同?
【发布时间】:2015-12-27 22:16:43
【问题描述】:

这是从游戏对象数组中随机选择对象的脚本:

public GameObject[] models;
       GameObject currentPoint;
       int index;
 public AudioSource correct;
 public AudioSource notcorrect;

 void Start()
 {
     models = GameObject.FindGameObjectsWithTag("numbers");
     index = Random.Range (0, models.Length);
     currentPoint = models[index];
     print (currentPoint.name);
     models [index].GetComponent<AudioSource> ().PlayDelayed(2);
     }

所以现在它获取了一个随机游戏对象并播放它的相关 AudioClip(例如数字 5),现在在 DefaultTrackableEventHandler 中我需要检查显示给相机的数字是否等于随机选择的数字.

我在下面发布 DefaultTrackableEventHandler 脚本:

using UnityEngine;

namespace Vuforia
{
    /// <summary>
    /// A custom handler that implements the ITrackableEventHandler interface.
    /// </summary>
    public class DefaultTrackableEventHandler : MonoBehaviour,
    ITrackableEventHandler
    {
        #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);
            }
        }

        #endregion // UNTIY_MONOBEHAVIOUR_METHODS



        #region PUBLIC_METHODS
        public GameObject show;
        public GameObject hide;
        /// <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()
        {
            show.SetActive(true);
            Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
            Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
            AudioSource[] audiocomponents  = GetComponentsInChildren<AudioSource>(true);
            // Enable rendering:
            foreach (Renderer component in rendererComponents)
            {
                component.enabled = true;
            }


            // Enable colliders:
            foreach (Collider component in colliderComponents)
            {
                component.enabled = true;
            }

            //Enable AudioSource 
            foreach (AudioSource component in audiocomponents)
            {
                component.enabled = true;
            }

            Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
        }


        private void OnTrackingLost()
        {
            hide.SetActive(true);
            Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
            Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
            AudioSource[] audiocomponents  = GetComponentsInChildren<AudioSource>(true);
            // Disable rendering:
            foreach (Renderer component in rendererComponents)
            {
                component.enabled = false;
            }

            // Disable colliders:
            foreach (Collider component in colliderComponents)
            {
                component.enabled = false;
            }
            //Disable AudioSource
            foreach (AudioSource component in audiocomponents)
            {
                component.enabled = false;
            }

            Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
        }

        #endregion // PRIVATE_METHODS
    }
}

请帮帮我!!

【问题讨论】:

    标签: c# unity3d augmented-reality


    【解决方案1】:

    Vuforia 使用包含可跟踪源的数据集,因此假设您有 5 个对象,它们代表从 1 到 5 的数字,您的数据集中需要有 5 个可跟踪对象。这些是在 Vuforia 门户上创建的。

    然后,你选择一个游戏对象,这个游戏对象应该与一个可追踪对象相关联。您可以定义要匹配的名称,然后您可以这样做:

    StateManager sm = TrackerManager.Instance.GetStateManager ();
    // This gets all the trackable currently tracked
    // so if you are looking at 3 and 5 it will contain both
    IEnumerable<TrackableBehaviour> activeTrackables = sm.GetActiveTrackableBehaviours ();
     
    foreach (TrackableBehaviour tb in activeTrackables) {
        // As you iterate, you compare with current game object name
        // if you have 3 and 5 and target object is 5 then you get a match.
        if( tb.TrackableName == currentObject.name) { }
    }
    

    【讨论】:

    • 什么是 sm.GetActiveTrackableBehaviour() 中的 sm 我如何调用 GetActiveTrackableBehaviour 函数?您发布的代码应该在 DefaultTrackableEventHandler 或 Quiz.cs(获取随机游戏对象并播放其音频剪辑的脚本)中?
    • 我添加了缺失的行。这可以去任何你想检查的地方。第一行(我添加的)是一个单例引用调用,因此可以从任何脚本中完成。所以很可能是类似 Quiz.cs 的相关文件。
    • 我收到此错误:字段初始值设定项无法引用非静态字段、方法或属性 `Quiz.sm' 我将代码添加到 Quiz.cs 脚本
    • 没关系我修复了这个错误。我只是将初始化移到 Start() 函数中
    • 我将您的代码添加到 Quiz.cs,但是当我向相机显示 ImageTarget 时,如果显示的目标与随机选择的对象相同,则它不会播放正确或不正确的 AudioClips .我在下面发布代码:
    猜你喜欢
    • 2020-09-23
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    相关资源
    最近更新 更多