如何从矢量或ImageTargets列表生成随机Target

时间:2016-01-02 00:14:02

标签: c# unity3d augmented-reality vuforia

我有9个ImageTargets,我想在按钮点击时选择一个随机目标。任何人都可以帮我弄清楚我该怎么做? 这是DefaultTrackableEventHandler的代码,我需要在此脚本中添加该代码

using UnityEngine;
using System.Collections.Generic;
namespace Vuforia
{
    /// <summary>
    /// A custom handler that implements the ITrackableEventHandler interface.
    /// </summary>
    public class QuizLogic : MonoBehaviour,
    ITrackableEventHandler
    {
        #region PRIVATE_MEMBER_VARIABLES

        #endregion // PRIVATE_MEMBER_VARIABLES

         TrackableBehaviour mTrackableBehaviour;

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

1 个答案:

答案 0 :(得分:0)

这是我扩展列表以便能够挑选随机成员的类。

using System.Collections.Generic;
using System;

public static class ListExtension {

    public static T Random<T>(this List<T> list) {
        if (list.Count < 1) {
            throw new ArgumentOutOfRangeException ("List too short");
        }
        Random r = new Random ();
        int randomIndex = r.Next (list.Count);
        T t = list [randomIndex];
        return t;
    }
}

您可以使用以下内容进行调用:

List<ImageTarget> targets = new List<ImageTarget> ();
targets.Add(target1);
targets.Add(target2);
//so on for 10 targets..

ImageTarget randomMember = targets.Random();

希望有所帮助!