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

C# 检测到多个图像目标时如何更改音频源,c#,unity3d,vuforia,C#,Unity3d,Vuforia,在我的Vuforia Unity项目中,当我瞄准图像目标时,会播放声音。现在,如果检测到2个目标,我想播放一个特定的其他声音,如果检测到3个目标,则播放另一个声音 我在ARCamera中列出了目标图像: void Update () { // Get the Vuforia StateManager StateManager sm = TrackerManager.Instance.GetStateManager (); // Query the StateManage

在我的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);
    }
}
void更新(){
//获取Vuforia StateManager
StateManager sm=TrackerManager.Instance.GetStateManager();
//查询StateManager以检索
//当前“活动”可跟踪项
//(即Vuforia目前正在跟踪的项目)
IEnumerable-activeTrackables=sm.getActiveTrackableBehaviors();
//遍历活动可跟踪项列表
Log(“当前活动的可跟踪列表(已跟踪):”;
foreach(activeTrackables中的trackablebehavior tb){
Log(“Trackable:+tb.TrackableName”);
}
}
你知道如何实现我想做的事情吗

如果检测到2个目标,我现在想播放一个特定的其他声音 如果检测到3个,则另一个

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

私有音频源otherAudio;
私人音频源另一个音频;
void Start()
{
otherAudio=GameObject.Find(“otherAudio”).GetComponent();
anotherAudio=GameObject.Find(“OtherAudio”).GetComponent();
}
无效更新()
{
//获取Vuforia StateManager
StateManager sm=TrackerManager.Instance.GetStateManager();
//查询StateManager以检索
//当前“活动”可跟踪项
//(即Vuforia目前正在跟踪的项目)
IEnumerable activeTrackables=sm.getActiveTrackableBehaviors();
if(activeTrackables.Count()==2)
{
如果(!otherAudio.isPlaying)
{
otherAudio.Play();
}
}
else if(activeTrackables.Count()==3)
{
如果(!anotherAudio.isPlaying)
{
另一个音频。播放();
}
}
//遍历活动可跟踪项列表
Log(“当前活动的可跟踪列表(已跟踪):”;
foreach(activeTrackables中的trackablebehavior tb)
{
Log(“Trackable:+tb.TrackableName”);
}
}

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

因此我在AR摄像头中实现了此脚本?只需将更新函数替换为我答案中的函数。它不会播放声音,只会播放1毫秒,然后停止OK,因此我添加了isPlaying变量在if条件下。现在可以了!谢谢:)很高兴我能帮助你。
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);
    }
}