不同的声音、不同的图像目标和一个按钮。Vuforia(Unity3d)

我将识别几个 imageTargets,我想用 onClick(button) 播放声音。此按钮位于画布上,并且在应用程序生命周期内始终位于顶部。所以你总能看到它。在这个按钮后面,您可以看到相机视图并识别标记。

Fe 我有两个标记:狗和牛。狗已分配音频 - 吠声。牛已分配音频- muu。

当我识别出 Cow -> 单击按钮时,它应该会发出 muu 的声音,但是当我识别出 Dog 时,同样的按钮在单击时应该会发出吠声。这是一个问题。我无法解决它。我想我应该为这个按钮编写一个脚本来为适当的标记播放 onClick 声音,但我不知道如何告诉按钮现在我可以看到 Cow 而另一次我可以看到 Dog。

我制作了一个在识别图像时播放声音的脚本,但我想用按钮来做。

如果有什么地方不够清楚 - 让我知道,我会写得更好或再写一遍。


qq_遁去的一_1
浏览 76回答 1
1回答

DIEA

因此,如果我对您的理解正确,您的代码基本上可以正常工作,但您不是直接为识别的目标播放相应的声音,而是只想在单击按钮时播放它,对吗?您可以简单地添加一个方法PlayCurrentSound并在中引用它onClick:// SET THIS NAME INSTEAD OF DIRECTLY PLAYING ITprivate string currentSoundName;// THIS IS THE METHOD CALLED BY THE BUTTONpublic void PlayCurrentSound(){&nbsp; &nbsp; if(!string.IsNullOrWhiteSpace(currentSoundName)) playSound(currentSoundName);}而不是OnTrackableStateChanged只改变它的值currentSoundName而不是直接重播它public void OnTrackableStateChanged(TrackableBehaviour.Status previousStatus, TrackableBehaviour.Status newStatus){&nbsp; &nbsp; m_PreviousStatus = previousStatus;&nbsp; &nbsp; m_NewStatus = newStatus;&nbsp; &nbsp; if (newStatus == TrackableBehaviour.Status.DETECTED ||&nbsp; &nbsp; &nbsp; &nbsp; newStatus == TrackableBehaviour.Status.TRACKED ||&nbsp; &nbsp; &nbsp; &nbsp; newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");&nbsp; &nbsp; &nbsp; &nbsp; // HERE BETTER USE A SWITCH INSTEAD&nbsp; &nbsp; &nbsp; &nbsp; switch(mTrackableBehaviour.TrackableName)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "1":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentSoundName = "audio/1_eng";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "2":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentSoundName = "audio/2_eng";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "3":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentSoundName = "audio/3_eng";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentSoundName = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // OR ALTERNATIVELY IF YOU ANYWAY WANT TO&nbsp; &nbsp; &nbsp; &nbsp; // SET THE NAME FOR ALL POSSIBLE NAMES YOU COULD EVEN GO&nbsp; &nbsp; &nbsp; &nbsp; currentSoundName = string.Format("audio/{0}_eng", mTrackableBehaviour.TrackableName);&nbsp; &nbsp; &nbsp; &nbsp; OnTrackingFound();&nbsp; &nbsp; }&nbsp; &nbsp; else if (previousStatus == TrackableBehaviour.Status.TRACKED &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;newStatus == TrackableBehaviour.Status.NO_POSE)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");&nbsp; &nbsp; &nbsp; &nbsp; StopAllAudio();&nbsp; &nbsp; &nbsp; &nbsp; // RESET currentSoundName&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; currentSoundName = "";&nbsp; &nbsp; &nbsp; &nbsp; OnTrackingLost();&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // For combo of previousStatus=UNKNOWN + newStatus=UNKNOWN|NOT_FOUND&nbsp; &nbsp; &nbsp; &nbsp; // Vuforia is starting, but tracking has not been lost or found yet&nbsp; &nbsp; &nbsp; &nbsp; // Call OnTrackingLost() to hide the augmentations&nbsp; &nbsp; &nbsp; &nbsp; OnTrackingLost();&nbsp; &nbsp; &nbsp; &nbsp; // RESET currentSoundName&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; currentSoundName = "";&nbsp; &nbsp; }}然而,还有一些其他的小事情我也会改变:soundTarget.loop = false;soundTarget.playOnAwake = false;这已经可以在游戏开始时完成,只能执行一次并且不应每次都重复。所以这样做:private void Awake(){&nbsp; &nbsp; soundTarget.loop = false;&nbsp; &nbsp; soundTarget.playOnAwake = false;}比clipTarget = (AudioClip)Resources.Load(ss);从资源中一遍又一遍地加载(也许)相同的声音……效率不高。您可能希望在加载后保留参考private Dictionary<string, AudioClip> clips = new Dictionary<string, AudioClip>();void playSound(string ss){&nbsp; &nbsp; if(clips.ContainsKey(ss) && clip[ss] != null)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; clipTarget = clips[ss];&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; clip = (AudioClip)Resources.Load(ss);&nbsp; &nbsp; &nbsp; &nbsp; if(clipTarget == null)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug.LogError("Couldn't get clip for " + ss, this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; clips.Add(ss, clipTarget);&nbsp; &nbsp; }&nbsp; &nbsp; soundTarget.clip = clipTarget;&nbsp; &nbsp; soundTarget.Play();}您也可能想soundTarget.PlayOneShot(clipTarget)改用。不同之处在于PlayOneShot播放整个声音并允许并发声音,同时Play中断当前声音并开始新声音(取决于您的需要)。
打开App,查看更多内容
随时随地看视频慕课网APP