• Unity3D 简易音频管理器


    依赖于Addressable
    依赖于单例模板:传送门
    在这里插入图片描述

    using System.Collections.Generic;
    using System.Security.Cryptography;
    using System;
    using UnityEngine;
    using UnityEngine.AddressableAssets;
    
    namespace EasyAVG
    {
        public class AudioManager : MonoSingleton<AudioManager>
        {
            private AudioSource backgroundMusic = null;
    
            private float backgroundValue = 1;
    
            private float soundValue = 1;
    
            private GameObject soundObj = null;
    
            private List<AudioSource> soundList = new List<AudioSource>();
    
    
            void Update()
            {
                for (int i = soundList.Count - 1; i > 0; i--)
                {
                    if (!soundList[i].isPlaying)
                    {
                        GameObject.Destroy(soundList[i]);
                        soundList.RemoveAt(i);
                    }
                }
            }
            public void PlayBackgroundMusic(string name)
            {
                if (backgroundMusic == null)
                {
                    GameObject obj = new GameObject("backgroundMusic");
                    backgroundMusic = obj.AddComponent<AudioSource>();
                }
    
                var handle = Addressables.LoadAssetAsync<AudioClip>(name);
                handle.Completed +=
                    (x) =>
                {
                    var clip = (AudioClip)x.Result;
                    backgroundMusic.clip = clip;
                    backgroundMusic.loop = true;
                    backgroundMusic.volume = backgroundValue;
                    backgroundMusic.Play();
                };
    
            }
    
            public void ChangeBackgroundMusicValue(float v)
            {
                backgroundValue = v;
                if (backgroundMusic == null)
                    return;
                backgroundMusic.volume = backgroundValue;
            }
    
            public void PauseBackgroundMusic()
            {
                if (backgroundMusic == null)
                    return;
                backgroundMusic.Pause();
            }
    
            public void StopBackgroundMusic()
            {
                if (backgroundMusic == null)
                    return;
                backgroundMusic.Stop();
            }
    
            public void PlaySound(string name, bool isLoop, Action<AudioSource> callback = null)
            {
                if (soundObj == null)
                {
                    soundObj = new GameObject();
                    soundObj.name = "Sounds";
                }
                AudioSource source = soundObj.AddComponent<AudioSource>();
                var handle = Addressables.LoadAssetAsync<AudioClip>(name);
                handle.Completed +=
                    (x) =>
                    {
                        source.clip = (AudioClip)x.Result;
                        source.loop = isLoop;
                        source.volume = soundValue;
                        source.Play();
                        //音效异步加载结束后,将这个音效组件加入集合
                        soundList.Add(source);
                        if (callback != null)
                            callback(source);
                    };
            }
    
            public void ChangeSoundValue(float v)
            {
                soundValue = v;
                for (int i = 0; i < soundList.Count; i++)
                {
                    soundList[i].volume = v;
                }
            }
    
            public void StopSound(AudioSource source)
            {
                if (soundList.Contains(source))
                {
                    soundList.Remove(source);
                    source.Stop();
                    GameObject.Destroy(source);
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
  • 相关阅读:
    动画应用的五个阶段
    区服分析丨更透彻的游戏营运数据解读,助力高效增长
    画质超清晰的行车记录仪,配置确实很强,盯盯拍Z50上手
    KingbbaseES V8R6集群维护案例之---集群之间数据迁移
    算符与数据结构 --- 类C语言有关操作和补充
    json转化成Map数据结构,Map转Array
    优惠劵秒杀优化-分布式锁
    一位大咖写给软件编程新手的建议 - 经验谈
    基于ZYNQ的PCIE高速数据采集卡的设计(三)硬件设计
    安卓实现沉浸式安卓状态栏实现
  • 原文地址:https://blog.csdn.net/qq_46273241/article/details/133242209