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