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

时间:2015-12-27 22:16:44

标签: c# unity3d augmented-reality

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

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

所以现在它获得一个随机游戏对象并播放它的相关AudioClip(前5号),现在在DefaultTrackableEventHandler中,我需要检查显示给摄像机的数字是否等于数字这是随机选择的。

我发布了下面的DefaultTrackableEventHandler脚本:

app.use('/', function (req, res, next)
{
    app.locals.logedIn = req.session.user_id !== undefined;
    next();
});

请帮助我!!

1 个答案:

答案 0 :(得分:0)

Vuforia使用包含可跟踪源的数据集,因此假设您有5个对象,它们代表1到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) { }
}