using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
[LuaCallCSharp]
public class SoundManager : MonoBehaviour
{
private float bgmVolumScale = 1f; //背景音乐声音大小比例
private float effVolumScale = 0.5f; //音效声音大小比例
public List<AudioSource> audioSourceList = new List<AudioSource>(); //音效播放器
private AudioSource musicAudio; //背景音乐播放器
GameObject thisObj;
Transform thisTrans;
// Start is called before the first frame update
public static SoundManager Init()
{
GameObject obj = new GameObject("SoundManager");
SoundManager soundManager = obj.AddComponent<SoundManager>();
obj.AddComponent<AudioListener>();
return soundManager;
}
private void Awake()
{
thisObj = gameObject;
thisTrans = transform;
//存储关闭时的音量比例
if (PlayerPrefs.HasKey("bgmVolumScale"))
bgmVolumScale = PlayerPrefs.GetFloat("bgmVolumScale");
if (PlayerPrefs.HasKey("effVolumScale"))
effVolumScale = PlayerPrefs.GetFloat("effVolumScale");
musicAudio = gameObject.AddComponent<AudioSource>();
musicAudio.playOnAwake = false;
musicAudio.loop = true;
musicAudio.volume = bgmVolumScale;
//FadeVolumeScale = FadeVolume / FadeDistance;
InitAudioSources();
DontDestroyOnLoad(gameObject);
}
//增加as组件播放器
void InitAudioSources()
{
for (int i = 0; i < 20; i++)
{
Unity 音频管理器SoundManager详解

本文档介绍了Unity中用于音频管理的SoundManager类,包括背景音乐和音效的播放、暂停、停止、音量调整等功能。SoundManager通过初始化AudioSource列表,实现了对多个音效的管理和控制,同时提供了静音、设置音量比例等实用方法。
最低0.47元/天 解锁文章
&spm=1001.2101.3001.5002&articleId=106853792&d=1&t=3&u=c7e145a9750e45d4b8d6512afed9742f)
1317





