如何在检测到多个图像目标时更改音频源

How to change Audio Source when multiple Image Targets are detected

在我的 Vuforia Unity 项目中,当我定位图像目标时会播放声音。我现在想在检测到 2 个目标时播放特定的其他声音,如果检测到 3 个目标则播放另一个声音。

我在 ARCamera 中成功列出了目标图像:

void Update () {
    // Get the Vuforia StateManager
    StateManager sm = TrackerManager.Instance.GetStateManager ();

    // Query the StateManager to retrieve the list of
    // currently 'active' trackables 
    //(i.e. the ones currently being tracked by Vuforia)
    IEnumerable<TrackableBehaviour> activeTrackables = sm.GetActiveTrackableBehaviours ();

   // Iterate through the list of active trackables
    Debug.Log ("List of trackables currently active (tracked): ");
    foreach (TrackableBehaviour tb in activeTrackables) {
        Debug.Log("Trackable: " + tb.TrackableName);
    }
}

你知道如何实现我想做的事情吗?

I want now to play a specific other sound if 2 targets are detected and another if 3 are detected.

我不知道你到底在哪里挣扎,但你用 activeTrackables.Count() 检测到有多少目标,然后你用 AudioSource.Play(); 播放声音。包括在顶部使用 System.Linq; 以使用 IEnumerable.Count().

private AudioSource otherAudio;
private AudioSource anotherAudio;

void Start()
{
    otherAudio = GameObject.Find("OtherAudio").GetComponent<AudioSource>();
    anotherAudio = GameObject.Find("OtherAudio").GetComponent<AudioSource>();
}

void Update()
{
    // Get the Vuforia StateManager
    StateManager sm = TrackerManager.Instance.GetStateManager();

    // Query the StateManager to retrieve the list of
    // currently 'active' trackables 
    //(i.e. the ones currently being tracked by Vuforia)
    IEnumerable<TrackableBehaviour> activeTrackables = sm.GetActiveTrackableBehaviours();

    if (activeTrackables.Count() == 2)
    {
        if (!otherAudio.isPlaying)
        {
            otherAudio.Play();
        }
    }

    else if (activeTrackables.Count() == 3)
    {
        if (!anotherAudio.isPlaying)
        {
            anotherAudio.Play();
        }
    }

    // Iterate through the list of active trackables
    Debug.Log("List of trackables currently active (tracked): ");
    foreach (TrackableBehaviour tb in activeTrackables)
    {
        Debug.Log("Trackable: " + tb.TrackableName);
    }
}

您也可以使用一个 AudioSource 并根据检测到的目标数量更改其 AudioClip