当前位置: 首页 > news >正文

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);}}}
}
http://www.lryc.cn/news/175341.html

相关文章:

  • 【李沐深度学习笔记】线性回归
  • 微信收款码费率0.38太坑了
  • 【学习笔记】CF1103D Professional layer
  • vue之Pinia
  • antd-vue 级联选择器默认值不生效解决方案
  • 分享53个Python源码源代码总有一个是你想要的
  • 【每日一题】658. 找到 K 个最接近的元素
  • 并发任务队列(字节青训测试题)
  • Ubuntu 安装Nacos
  • CSS 小球随着椭圆移动
  • 【李沐深度学习笔记】线性代数
  • vuejs - - - - - 递归组件的实现
  • 精准对接促合作:飞讯受邀参加市工信局举办的企业供需对接会
  • 数学建模之遗传算法
  • ISO9001认证常见的不符合项
  • crypto:看我回旋踢
  • Springcloud实战之自研分布式id生成器
  • java 企业工程管理系统软件源码 自主研发 工程行业适用
  • Spring Cloud Alibaba Nacos 2.2.3 (4) - 本地源码编译 调试
  • WKB近似
  • LeetCode算法二叉树—108. 将有序数组转换为二叉搜索树
  • 如何设置 Git 短命令
  • virtualbox无界面打开linux虚拟机的bat脚本,以及idea(代替Xshell)连接linux虚拟机的方法
  • mockito 的 InjectMocks 和 Mock 有什么区别?
  • 网络工程师的爬虫技术之路:跨界电商与游戏领域的探索
  • 【TCP】确认应答 与 超时重传
  • Kubernetes中Pod的扩缩容介绍
  • vue点击pdf文件直接在浏览器中预览文件
  • 通讯网关软件012——利用CommGate X2OPC实现MS SQL数据写入OPC Server
  • ISE_ChipScope Pro的使用