This article shares the specific code of Unity's audio playback manager for your reference. The specific content is as follows
1. Modular, use directly.Create an empty object, drag the script up, and then drag all the audio you want to play into the "AudioList" on the panel;
2. Directly call the method through AudioManagerP._instance,Include:
- pause;
- Continue playing;
- Stop playing;
- Play background music (player No. 0 is dedicated to playing background sound);
- Play the sound directly (with parameters 1, player serial number 2, audio name 3, sound size 4, whether it is looped);
- Play groans directly (with parameters 1, player serial number 2, audio name);
- The sound fades in (with parameters 1, player serial number 2, audio name 3, whether to loop);
- The sound fades out (with parameters, player serial number).
Advantages: It can play many sound sources at the same time, and the status of each playback source can be accurately controlled.
Disadvantages: Don't know.
Source code
using ; using ; using UnityEngine; public class AudioManagerP : MonoBehaviour { public static AudioManagerP _instance;//Single private bool isOut; private bool isIn; void Awake() { _instance = this; DontDestroyOnLoad(this); } //audioClipl list public List<AudioClip> audioList; //Initial sound preset number private int initAudioPrefabcount = 7; //Record the volume before mute [HideInInspector] public float tempVolume = 0; //Whether it is muted private bool isMute = false; public bool IsMute { set { isMute = value; if (isMute) { tempVolume = ; = 0; } else { = tempVolume; } } private get { return isMute; } } //Sound Size Coefficient private float volumeScale = 1; public float VolumeScale { set { volumeScale = Mathf.Clamp01(value); if (!IsMute) { = value; } } private get { return volumeScale; } } //audio dictionary private Dictionary<string, AudioClip> audioDic = new Dictionary<string, AudioClip>(); //Background music private AudioSource bgAudioSource; //Sound object pool private AudioObjectPool audioObjectPool; private void Start() { GameObject audioPrefab = new GameObject("AudioObjectPool"); <AudioSource>(); <AudioSource>().playOnAwake = false; audioObjectPool = new AudioObjectPool(audioPrefab, initAudioPrefabcount); = ; (); foreach (AudioClip ac in audioList) { (, ac); } bgAudioSource = (0).GetComponent<AudioSource>(); } private void Update() { //Test code if ((0)) { //StartCoroutine(AudioFadeIn(0, "Shen Mu-Sold Out(Hot Version)", true)); //The music is gradually entering //PlayAudio(0, "Shen Mu-Sold Out(Hot Version)"); //Direct Play } if ((1)) { //StartCoroutine(AudioFadedOut(0)); //The music fades out } } /// <summary> /// Audio playback /// </summary> /// <param name="index">Player serial number (played with the AudioSource)</param> public void PauseAudio(int index) { AudioSource audioSource = (index).GetComponent<AudioSource>(); (); } /// <summary> /// Continue to play /// </summary> /// <param name="index">player serial number</param> public void ResumeAudio(int index) { AudioSource audioSource = (index).GetComponent<AudioSource>(); (); } /// <summary> /// Stop playing sound /// </summary> /// <param name="index">player serial number</param> public void StopAudio(int index) { AudioSource audioSource = (index).GetComponent<AudioSource>(); (); } /// <summary> /// Play background music, here the player No. 0 is directly used to play Beijing music /// </summary> /// <param name="audioNme"></param> public void PlayBGMAudio(string audioNme) { AudioClip audioClip; if ((audioNme, out audioClip)) { (true); = audioClip; (0).(true); (); = true; } } /// <summary> /// Play the sound directly /// </summary> /// <param name="index">player serial number</param> /// <param name="audioName">audio file name</param> /// <param name="volume">Volume size</param> /// <param name="isLoop">Is it looping</param> public void PlayAudio(int index, string audioName, float volume = 1, bool isLoop = false) { //("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- AudioSource audioSource = (index).GetComponent<AudioSource>(); if (IsMute || audioSource == null) { return; } StopAllCoroutines(); AudioClip audioClip; if ((audioName, out audioClip)) { //("The clip name of the button click is: "+); (true); = isLoop; = audioClip; = volume; if () { (); } (); } } /// <summary> /// Reload playback, only player and audio /// </summary> /// <param name="index">player serial number</param> /// <param name="audioName">Name of audio</param> public void PlayAudio(int index, string audioName) { //("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- AudioSource audioSource = (index).GetComponent<AudioSource>(); if (IsMute || audioSource == null) { return; } StopAllCoroutines(); AudioClip audioClip; if ((audioName, out audioClip)) { //("The clip name of the button click is: "+); (true); = audioClip; if () { (); } (); } } /// <summary> /// The sound fades /// </summary> /// <param name="index">player serial number</param> /// <param name="audioName">audio name</param> /// <param name="isLoop">Is it looping</param> /// <returns></returns> public IEnumerator AudioFadeIn(int index, string audioName, bool isLoop) { float initVolume = 0; float preTime = 1.0f / 5; //Gradial Rate AudioClip audioClip; if ((audioName, out audioClip)) { AudioSource audioSource = (index).GetComponent<AudioSource>(); if (audioSource == null) yield break; (true); //The sound playback object is false by default. When playing, the corresponding object is set to true = audioClip; = 0; = isLoop; print("zhi 0"); if () { (); } (); = 0; while (true) { initVolume += 1 * * preTime; //The sound is getting higher = initVolume; // Assign the incremental variable to the player volume if (initVolume >= 1 - 0.02f) //If it is very close to the value in the configuration file, then pin it to the value in the configuration file (maximum value) { = 1; break; } yield return 1; } } } /// <summary> /// The sound fades out /// </summary> /// <param name="index">Failout player serial number</param> /// <returns></returns> public IEnumerator AudioFadedOut(int index) { AudioSource audioSource = (index).GetComponent<AudioSource>(); if (audioSource == null || == 0 || audioSource == null) { yield break; } float initVolume = ; float preTime = 1.0f / 5; while (true) { initVolume += -1 * * preTime; = initVolume; if (initVolume <= 0) { = 0; (); break; } yield return 1; } } /// <summary> /// Initialize audio /// </summary> /// <param name="audioSource"></param> private void InitAudioSource(AudioSource audioSource) { if (audioSource == null) { return; } if () { (); } = false; = false; = 1; = null; = "AudioObjectPool"; } private void Destroy() { StopAllCoroutines(); } } #region Sound object pool/*Sound object pool, to be improved, there may be multiple sound sources playing at the same time, hard cut or cut after playback, it cannot be determined which one is, and the AudioObjectPool cannot be released accurately Here, only the object pool is used to generate a specified number of players at the beginning, and there is no use to obtain and release the player object*/ public class AudioObjectPool { //The object pool preset to be generated private GameObject prefab; // Object pool list private List<GameObject> pool; //Constructor public AudioObjectPool(GameObject prefab, int initialSize) { = prefab; = new List<GameObject>(); for (int i = 0; i < initialSize; i++) { //Initialize AllLocateInstance(); } } //Get the instance public GameObject GetInstance() { if ( == 0) { //Create an instance } GameObject instance = pool[0]; (0); (true); return instance; } //Release strength public void ReleaseInstance(GameObject instance) { (false); (instance); } //Grow local strength private GameObject AllLocateInstance() { GameObject instance = (GameObject)(prefab); (AudioManagerP._instance.transform); (false); (instance); return instance; } } #endregion
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.