vuforia add sound effects


1. Import sound effects

2. Identify imageTarget and play sound effects

1. Add Audio Source component

Specify the background music, uncheck the play on awake, and cancel the automatic play, because the sound effect will be played after the object is recognized
insert image description here

2. Modify the script

  1. Open the script on imageTarget myDefaultObserverEventHandler(originally DefaultObserverEventHandler, because I have modified this script, so I renamed it tomyDefaultObserverEventHandler
  2. variables defined in the class
private AudioSource audio;//识别到目标物体后播放的音效
public AudioClip welcomClip;//识别到目标物体8秒后播放的音效

注:AudioSource是一个组件,AudioClip 是AudioSource的一个属性,前者是音乐源,可以控制音乐的播放等;后者可以从资源ReSources文件中直接获得音频文件。
insert image description here

  1. Modify the Start() function
protected virtual void Start()
    {
    
    
        mObserverBehaviour = GetComponent<ObserverBehaviour>();

        if (mObserverBehaviour)
        {
    
    
            mObserverBehaviour.OnTargetStatusChanged += OnObserverStatusChanged;
            mObserverBehaviour.OnBehaviourDestroyed += OnObserverDestroyed;

            OnObserverStatusChanged(mObserverBehaviour, mObserverBehaviour.TargetStatus);
            SetupPoseSmoothing();
        }
        audio = this.GetComponent<AudioSource>();//添加这一句
    }
  1. Modify the OnTrackingFound() function
if (!audio.isPlaying)
{
    
    //如果音效没有在播放,就播放声音
audio.Play();
}
  1. Play another sound effect
    Create a new function
 private void PlayWelcomeClip()
 {
    
    
  AudioSource.PlayClipAtPoint(welcomClip, transform.position);
 }

Go back to imageTarget and specify the clip
insert image description here

  1. Set the sound effect to play after 8 seconds
    Add a sentence in the Start() function
Invoke("PlayWelcomeClip", 8.0f);//8秒后调用PlayWelcomeClip方法

Reference article: Sounds and effects in games

Guess you like

Origin blog.csdn.net/zjjjjjjj_/article/details/125597062