admin 管理员组

文章数量: 1184232

11. 声音管理器

  • 背景音乐
    • 播放、暂停、恢复、停止、音量
  • 音效
    • 播放、音量

Scripts/Framework/Manager/创建SoundManager.cs,给BuildResources/Audio下面添加音频

publicclassSoundManager:MonoBehaviour{
   
   AudioSource m_MusicAudio;//背景音乐AudioSource m_SoundAudio;//音效privatefloat SoundVolume
    {
   
   get{
   
   return PlayerPrefs.GetFloat("SoundVolume",1.0f);}set{
   
   
            m_SoundAudio.volume =value;
            PlayerPrefs.SetFloat("SoundVolume",value);}}privatefloat MusicVolume
    {
   
   get{
   
   return PlayerPrefs.GetFloat("MusicVolume",1.0f);}set{
   
   
            m_MusicAudio.volume =value;
            PlayerPrefs.SetFloat("MusicVolume",value);}}privatevoidAwake(){
   
   //背景音乐需要循环,并且不能直接播放,因为背景音乐肯定有
        m_MusicAudio =this.gameObject.AddComponent<AudioSource>();
        m_MusicAudio.playOnAwake =false;
        m_MusicAudio.loop =true;//音效不需要循环,音效不一定有,不用设置playOnAwake
        m_SoundAudio =this.gameObject.AddComponent<AudioSource>();
        m_SoundAudio.loop =false;}publicvoidPlayMusic(string name){
   
   //音量小于0.1f因为听不见了,同时节省开销,直接不播放了if(this.MusicVolume <0.1f){
   
   return;}//如果背景音乐是正在播放的,也跳过string oldName ="";if(m_MusicAudio.clip !=null){
   
   
            oldName = m_MusicAudio.clip.name;}if(oldName == name.Substring(0,name.IndexOf("."))){
   
   //m_MusicAudio.Play();return;}//否则就去播放背景音乐
        Manager.Resource.LoadMusic(name,(UnityEngine.Object obj)=>{
   
   
            m_MusicAudio.clip = obj asAudioClip;
            m_MusicAudio.Play();});}//暂停播放背景音乐publicvoidPauseMusic(){
   
   
        m_MusicAudio.Pause();}//继续播放背景音乐publicvoidOnUnPauseMusic(){
   
   
        m_MusicAudio.UnPause();}//停止播放背景音乐publicvoidStopMusic(){
   
   
        m_MusicAudio.clip =null;
        m_MusicAudio.Stop();}//设置背景音乐音量publicvoidSetMusicVolume(floatvalue){
   
   this.MusicVolume =value;}//设置音效音量publicvoidSetSoundVolume(floatvalue){
   
   this.SoundVolume =value;}}

设计UI

privatestaticSoundManager _sound;publicstaticSoundManager Sound
{
   
   get{
   
   return _sound;}}publicvoidAwake(){
   
   
    _resource =this.gameObject.AddComponent<ResourceManager>();
    _lua =this.gameObject.AddComponent<LuaManager>();
    _ui =this.gameObject.AddComponent<UIManager>();
    _entity =this.gameObject.AddComponent<EntityManager>();
    _scene =this.gameObject.AddComponent<MySceneManager>();
    _sound =this.gameObject.AddComponent<SoundManager>();}


XLua.CSharpCallLua

如果希望把一个lua函数适配到一个C# delegate(一类是C#侧各种回调:UI事件,delegate参数,比如List:ForEach;另外一类场景是通过LuaTable的Get函数指明一个lua函数绑定到一个delegate)。或者把一个lua table适配到一个C# interface,该delegate或者interface需要加上该配置。
:::info
如果lua需要添加Action,并且带参数,需要添加这个标签
:::

[CSharpCallLua]publicstaticList<Type> mymodule_cs_call_lua_list =newList<Type>(){
   
   typeof(UnityEngine.Events.UnityAction<float>),};

为什么需要这么作,因为Slider的Action要传float并且,lua中的Action也要传float,无参数的不会报错,有参数需要在静态列表中添加说明。添加完之后,在XLua-GenerateCode一下,就可以用了。

再次构建Bundle会报错,因为这些扩展方法是编辑器下使用的,lua不允许使用,避免编辑器方法生成code代码,需要生成黑名单列表
如果出现不播放声音的问题,是因为delegate之前出现的错误,导致直接把音量0写进playerprefs了,所以一直没有办法播放,音量为0.

//黑名单[BlackList]publicstaticList<List<string>> BlackList =newList<List<string>>(){
   
   newList<string>(){
   
   "UnityEngine.Light","ShadowRadius"}

本文标签: 背景音乐 音量 编程