简体   繁体   English

如何使DefaultTrackableEventHandler脚本检查随机选择的游戏对象是否与相机显示的游戏对象相同?

[英]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?

This is the script that gets a randomly selected object from an array of game objects: 这是从一系列游戏对象中获取随机选择的对象的脚本:

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

So now it gets a random game object and plays it's relevant AudioClip (for ex number 5), and now in the DefaultTrackableEventHandler i need to check if the number that is shown to the camera is equal to the number that was randomly selected. 因此,现在它得到一个随机的游戏对象并播放它的相关AudioClip(例如ex编号5),现在在DefaultTrackableEventHandler中,我需要检查显示在摄像机上的数字是否等于随机选择的数字。

I am posting the DefaultTrackableEventHandler script below: 我在下面发布了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
    }
}

Please help me !! 请帮我 !!

Vuforia works with data sets containing trackable source, so let's say you have 5 objects and they represent numbers from 1 to 5, you need to have five trackables in your data sets. Vuforia使用包含可追踪源的数据集,因此,假设您有5个对象,它们代表1到5之间的数字,则您的数据集中需要有五个可追踪对象。 Those are created on the Vuforia portal. 这些是在Vuforia门户上创建的。

Then, you pick a game object, this game object should be related to a trackable. 然后,选择一个游戏对象,该游戏对象应与一个可跟踪对象相关。 You can define the names to match, then you can do: 您可以定义要匹配的名称,然后可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM