[Unity tips] Unity music and sound effects manager

final effect

Insert image description here

Preface

In game development, the management of music and sound effects is an important link. Good music and the right sound effects can add atmosphere to a game and enhance the player's experience. In order to better manage music and sound effects, we can use a dedicated music and sound effects manager.

In this article, I will show you how to create a simple and practical music and sound effects manager in Unity. Through this manager, we can easily control the playback, muting, and volume adjustment of music and sound effects.

First, we need to create a C# script called AudioManager. In this script, we define the Sound array of music and sound effects and the corresponding AudioSource. Through the Sound array, we can easily manage multiple music and sound effects resources. AudioSource is responsible for the actual audio playback.

In AudioManager, we provide several methods to control the playback of music and sound effects. For example, we can use the PlayMusic method to play music with a specified name, and use the PlaySFX method to play sound effects with a specified name. At the same time, we also provide ToggleMusic and ToggleSFX methods for switching the mute status of music and sound effects. In addition, we can also use the MusicVolume and SFXVolume methods to adjust the volume of music and sound effects.

For further convenience, we created a script called UIController. In this script, we can control the playback, muting, and volume adjustment of music and sound effects by interacting with the user interface. By binding UI elements (such as Slider) to corresponding methods, we can easily control music and sound effects.

By using this music and sound effects manager, we can more conveniently manage audio resources in the game and provide a better music and sound effects experience. In the following article, I will introduce in detail how to create and use this manager, and give some practical application examples.

I hope this article will be helpful to everyone in music and sound effects management in Unity game development! Let's enter the world of music together and add more fun to the game!

start

Add Sound

using UnityEngine;

[System.Serializable]
public class Sound
{
    
    
    public string name;         // 音频剪辑的名称
    public AudioClip clip;      // 音频剪辑
    [Range(0f, 1f)]
    public float volume = 0.7f; // 音量大小
}

Add a new AudioManager class to manage the playback of sound effects and music. This includes the Sound array that defines music and sound effects, the AudioSource that defines music and sound effects, and the methods for playing music and sound effects. When the program wakes up, it will determine whether an Instance already exists to ensure that only one AudioManager exists in the entire program.

public class AudioManager : MonoBehaviour
{
    
    
	public static AudioManager Instance;
	
	//定义音乐和音效的Sound数组
	public Sound[] musicSounds, sfxSounds;
	//音乐和音效的AudioSource
	public AudioSource musicSource, sfxSource;

	private void Awake() 
	{
    
    
	    if (Instance == null) 
	    {
    
    
	        Instance = this;
	        //在场景切换时不销毁该对象
	        DontDestroyOnLoad(gameObject);
	    }
	    else 
	    {
    
    
	        Destroy(gameObject);
	    }
	}
	
	//播放音乐的方法,参数为音乐名称
	public void PlayMusic(string name) 
	{
    
    
	    //从音乐Sounds数组中找到名字匹配的Sound对象
	    Sound s = Array.Find(musicSounds, x => x.name == name);
	    //如果找不到对应的Sound,输出错误信息
	    if (s == null) 
	    {
    
    
	        Debug.Log("没有找到音乐");
	    }
	    //否则将音乐源的clip设置为对应Sound的clip并播放
	    else 
	    {
    
    
	        musicSource.clip = s.clip;
	        musicSource.Play();
	    }
	}

	//播放音效的方法,参数为音效名称
	public void PlaySFX(string name) 
	{
    
    
	    //从音效Sounds数组中找到名字匹配的Sound对象
	    Sound s = Array.Find(sfxSounds, x => x.name == name);
	    //如果找不到对应的Sound,输出错误信息
	    if (s == null) 
	    {
    
    
	        Debug.Log("没有找到音效");
	    }
	    //否则播放对应Sound的clip
	    else 
	    {
    
    
	        sfxSource.PlayOneShot(s.clip);
	    }
	}
}

Mount the script and configure music sound effect parameters
Insert image description here
Insert image description here

use

# 调用音乐
AudioManager.Instance.PlayMusic("Theme");

# 调用音效
AudioManager.Instance.PlaySFX("Jump");

# 停止音乐
AudioManager.Instance.musicSource.Stop();

# 停止音效
AudioManager.Instance.sfxSource.Stop();

Music sound effect control

1. Draw an interface to control music sound effects

Insert image description here

2. Modify AudioManager

//切换音乐的静音状态
public void ToggleMusic()
{
    
    
	musicSource.mute = !musicSource.mute;
}

//切换音效的静音状态
public void ToggleSFX()
{
    
    
	sfxSource.mute = !sfxSource.mute;
}

//设置音乐音量的方法,参数为音量值
public void MusicVolume(float volume)
{
    
    
	musicSource.volume = volume;
}

//设置音效音量的方法,参数为音量值
public void SFXVolume(float volume)
{
    
    
	sfxSource.volume = volume;
}

3. UI control script

Add a new UIController class to control user interface interaction

public class UIController : MonoBehaviour
{
    
    
	public Slider _musicSlider, _sfxSlider;
	
	//切换音乐静音状态的方法
	public void ToggleMusic()
	{
    
    
	    AudioManager.Instance.ToggleMusic();
	}
	
	//切换音效静音状态的方法
	public void ToggleSFX()
	{
    
    
	    AudioManager.Instance.ToggleSFX();
	}
	
	//设置音乐音量的方法
	public void MusicVolume()
	{
    
    
	    AudioManager.Instance.MusicVolume(_musicSlider.value);
	}
	
	//设置音效音量的方法
	public void SFXVolume()
	{
    
    
	    AudioManager.Instance.SFXVolume(_sfxSlider.value);
	}
}

Mount the script, configure volume sliding parameters
Insert image description here
and configure button events
Insert image description here
Insert image description here
Insert image description here
Insert image description here

4. Effect

Insert image description here

Complete code

public class AudioManager : MonoBehaviour
{
    
    
	public static AudioManager Instance;
	
	//定义音乐和音效的Sound数组
	public Sound[] musicSounds, sfxSounds;
	//音乐和音效的AudioSource
	public AudioSource musicSource, sfxSource;

	private void Awake() 
	{
    
    
	    if (Instance == null) 
	    {
    
    
	        Instance = this;
	        //在场景切换时不销毁该对象
	        DontDestroyOnLoad(gameObject);
	    }
	    else 
	    {
    
    
	        Destroy(gameObject);
	    }
	}
	
	//播放音乐的方法,参数为音乐名称
	public void PlayMusic(string name) 
	{
    
    
	    //从音乐Sounds数组中找到名字匹配的Sound对象
	    Sound s = Array.Find(musicSounds, x => x.name == name);
	    //如果找不到对应的Sound,输出错误信息
	    if (s == null) 
	    {
    
    
	        Debug.Log("没有找到音乐");
	    }
	    //否则将音乐源的clip设置为对应Sound的clip并播放
	    else 
	    {
    
    
	        musicSource.clip = s.clip;
	        musicSource.Play();
	    }
	}

	//播放音效的方法,参数为音效名称
	public void PlaySFX(string name) 
	{
    
    
	    //从音效Sounds数组中找到名字匹配的Sound对象
	    Sound s = Array.Find(sfxSounds, x => x.name == name);
	    //如果找不到对应的Sound,输出错误信息
	    if (s == null) 
	    {
    
    
	        Debug.Log("没有找到音效");
	    }
	    //否则播放对应Sound的clip
	    else 
	    {
    
    
	        sfxSource.PlayOneShot(s.clip);
	    }
	}
	
	//切换音乐的静音状态
	public void ToggleMusic()
	{
    
    
		musicSource.mute = !musicSource.mute;
	}
	
	//切换音效的静音状态
	public void ToggleSFX()
	{
    
    
		sfxSource.mute = !sfxSource.mute;
	}
	
	//设置音乐音量的方法,参数为音量值
	public void MusicVolume(float volume)
	{
    
    
		musicSource.volume = volume;
	}
	
	//设置音效音量的方法,参数为音量值
	public void SFXVolume(float volume)
	{
    
    
		sfxSource.volume = volume;
	}
}

end

Gifts of roses, hand a fragrance! If the content of this article is helpful to you, please don’t be stingy with your 点赞评论和关注feedback so that I can receive feedback as soon as possible. Your feedback 支持is the greatest motivation for me to continue creating. Of course, if you find 存在错误something in the article 更好的解决方法, please feel free to comment and send me a private message!

Okay, I am 向宇, https://xiangyu.blog.csdn.net

A developer who works quietly in a small company has recently started to learn Unity by himself out of hobbies. In his spare time, he records and shares while learning. Standing on the shoulders of giants, learning from the experiences of my predecessors will always give me a lot. Help and inspire! PHP is work, Unity is life! If you encounter any problems, you are also welcome to comment and send me a private message. Although I may not know some of the problems, I will check the information from all parties and try to give the best suggestions. I hope to help more people who want to learn programming. , encourage each other~

Insert image description here

Guess you like

Origin blog.csdn.net/qq_36303853/article/details/135276586