[Unity] Audio Source component - use code to dynamically control the playback and pause of sound effects

1. Code control play, pause, stop

Add an Audio Source component to the game object
Drag the audio file into the AudioCilp of the Audio Source component
insert image description here
Create a script and mount it

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Lesson20 : MonoBehaviour
{
    
    
    //声明一个AudioSource类型变量
    AudioSource audioSource;

    void Start()
    {
    
    
        //获取AudioSource组件
        audioSource = this.GetComponent<AudioSource>();
    }
    void Update()
    {
    
    
        //1.播放
        //  按下P键播放
        if (Input.GetKeyDown(KeyCode.P))
        {
    
    
            audioSource.Play();

            //补充:延迟播放(参数1 延迟几秒)
            //audioSource.PlayDelayed(3);
        }

        //2.暂停
        //  按下空格暂停播放
        if (Input.GetKeyDown(KeyCode.Space))
        {
    
    
            audioSource.Pause();

            //补充:停止暂停,和暂停后再Play效果一样
            //audioSource.UnPause();
        }

        //3.停止播放
        //  按S键停止播放
        if (Input.GetKeyDown(KeyCode.S))
        {
    
    
            audioSource.Stop();
        }
    }
}

2. How to detect the completion of the sound effect

If we want to do something after the music is played,
we need to detect whether the music is played

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Lesson20 : MonoBehaviour
{
    
    
    //声明一个AudioSource类型变量
    AudioSource audioSource;

    void Start()
    {
    
    
        //获取AudioSource组件
        audioSource = this.GetComponent<AudioSource>();
    }
    void Update()
    {
    
    
        //使用.isPlaying来检测(返回值是bool)
        if (audioSource.isPlaying)
        {
    
    
            print("播放中");
        }
        else
        {
    
    
            print("播放结束了");
        }
    }
}

3. How to dynamically control sound playback

Method 1: Directly mount the script on the object to play music to control playback
1. Declare a variable of type AudioSource in the script on the object,
2. Get the mounted AudioSource sound source script through .GetComponent
3. Control the sound effect Source script to realize dynamic control playback
The above knowledge point 1 is the method used

Method 2: Instantiate the object that mounts the sound effect source script
(this method is less used, because the destruction of the sound effect prefab needs to be managed)
1. Make the sound effect to be played into a prefab and check the Play On Awake parameter
insert image description here
2 .Mount the script to an object in the scene

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Lesson20 : MonoBehaviour
{
    
    
    //外部拖入音效预制体
    public GameObject obj;

    void Start()
    {
    
    
        //实例化外部拖入的音效预制体
        GameObject.Instantiate(obj);
    }
}

4. So as long as the prefab is instantiated, the prefab sound effect will be played immediately.
insert image description here
Method 3: Use an AudioSource to control the playback of different sound effects. The
AudioClip parameter of the AudioSource component can be edited by code
insert image description here
and the AudioClip type can also be declared variable to drag in the sound source from the panel:
insert image description here
panel drag in:
insert image description here

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Lesson20 : MonoBehaviour
{
    
    
    //从外部面板拖入的音频
    public AudioClip clip;

    void Start()
    {
    
    
        //获取一个AudioSource组件
        //  获取了自己挂载的对象的AudioSource组件
        AudioSource audio = this.gameObject.GetComponent<AudioSource>();
        //让获取到的这个AudioSource组件的AudioClip参数 = 我从外部拖入的音频文件
        audio.clip = clip;
        //播放
        audio.Play();
    }
}

Guess you like

Origin blog.csdn.net/SEA_0825/article/details/127115125