It is indispensable to use sound (AudioSource) to achieve some sound effects in the process of Unity development. Next, let’s take a look at the content of sound (AudioSource).
When I took the screenshot, I forgot that the Volume is 0-1 to adjust the volume of the sound. 0 means no sound, and 1 means full volume (too lazy to take another screenshot)
Sound (AudioSource) play, end, pause, continue code part
using UnityEngine;
public class AudioController : MonoBehaviour
{
AudioSource audioSource;
private void Start()
{
//获取AudioSource组件
audioSource = GetComponent<AudioSource>();
}
public void PlayAudio()
{
//播放音频
audioSource.Play();
}
public void StopAudio()
{
//停止音频
audioSource.Stop();
}
public void PauseAudio()
{
//暂停音频
audioSource.Pause();
}
public void UnPauseAudio()
{
//继续播放音频
audioSource.UnPause();
}
}
The volume of the sound (AudioSource) I call it with the Button event here (modify it according to your own needs)
public void VolumeUp()
{
//将音频音量增加0.1
audioSource.volume += 0.1f;
}
public void VolumeDown()
{
//将音频音量减少0.1
audioSource.volume -= 0.1f;
}
public void SetVolume(float volume)
{
//设置音频音量
audioSource.volume = volume;
}