123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System;
- public class AudioManager : MonoSingleton<AudioManager>
- {
- #region 背景音乐的音乐名称常量
- /// <summary>
- /// 背景音乐
- /// </summary>
- public const string MainBackMusic = "Lonely Mountain";
- /// <summary>
- /// 登录背景音乐
- /// </summary>
- public const string LoginBackMusic = "Black Knight";
- /// <summary>
- /// 战斗背景音乐
- /// </summary>
- //public const string BattleBackMusic = "Blue Stahli-Overklock";
- public const string BattleBackMusic = "Dawn of the Apocalypse";
- /// <summary>
- /// 过场动画背景音乐
- /// </summary>
- public const string LoadingBackMusic = "Black Knight";
- /// <summary>
- /// PVP背景音乐
- /// </summary>
- public const string PVPBackMusic = "Music_PVP_B1";
- /// <summary>
- /// 关卡背景音乐
- /// </summary>
- public const string GateBackMusic = "Lonely Mountain";
- #endregion
- #region 特殊音效
- /// <summary>
- /// 战斗失败
- /// </summary>
- public const string BattleSepcial_LoseSound = "Music_Battle_Lose";
-
- /// <summary>
- /// 战斗胜利
- /// </summary>
- public const string BattleSepcial_WinSound = "Music_Battle_Win";
-
- /// <summary>
- /// boss出现音效
- /// </summary>
- public const string BattleSepcial_BossDisplaySound = "The Enemy";
- /// <summary>
- /// 战斗开始<四个字>特效 音效
- /// </summary>
- public const string BattleSepcial_StartFightSound = "Music_Battle_Start";
- /// <summary>
- /// 战斗——连击准备开始特效 音效
- /// </summary>
- public const string BattleSepcial_LianJISound = "Music_Battle_Lianji";
- #endregion
- private const string AudioContainer_HeroAnimation_Display = "AudioContainer_HeroAnimation_Display";
- private const string AudioContainer_HeroAnimation_Attack = "AudioContainer_HeroAnimation_Attack";
- private const string AudioContainer_HeroAnimation_BeAttack = "AudioContainer_HeroAnimation_BeAttack";
-
- /// <summary>
- /// 背景音乐容器
- /// </summary>
- private const string AudioContainer_Music = "AudioContainer_Music";
- /// <summary>
- /// UI音效容器
- /// </summary>
- private const string AudioContainer_UISound = "AudioContainer_UISound";
- /// <summary>
- /// UI音效容器
- /// </summary>
- private const string AudioContainer_Skill = "AudioContainer_Skill";
- #region
- /// <summary>
- /// 资源
- /// </summary>
- public Dictionary<string, AudioClip> mAudioResDic = new Dictionary<string, AudioClip>();
- /// <summary>
- /// 加载资源
- /// </summary>
- /// <param name="bundleName"> 资源包的名称 </param>
- /// <param name="ac"> 加载完后执行的回调 </param>
- private void LoadAudioBundleRes(string bundleName, Action ac)
- {
- ResourceHelper.Instance.LoadAssetBundle(bundleName, (AssetBundle bundle) =>
- {
- if (null != bundle)
- {
- GameObject audioObj = bundle.LoadAsset<GameObject>(bundleName);
- AudioContainer audioContainer = audioObj.GetComponent<AudioContainer>();
- foreach (var audio in audioContainer.mAudioList)
- {
- mAudioResDic[audio.name] = audio;
- }
- }
- });
- }
- /// <summary>
- /// 获取图标
- /// </summary>
- /// <param name="audioBundleContainer">容器bundle名称</param>
- /// <param name="audioName"> 图标的名称 </param>
- /// <param name="callBack">赋值成功后回调</param>
- public void GetAudioClip(string audioBundleContainer, string audioName, Action<AudioClip> callBack)
- {
- if (mAudioResDic.ContainsKey(audioName) && mAudioResDic[audioName] != null)
- {
- if (callBack != null)
- {
- callBack(mAudioResDic[audioName]);
- }
- }
- else
- {
- LoadAudioBundleRes(audioBundleContainer, () =>
- {
- if (mAudioResDic.ContainsKey(audioName))
- {
- if (callBack != null)
- {
- callBack(mAudioResDic[audioName]);
- }
- }
- else
- {
- //LogHelper.Log("null--->" + audioName);
- callBack(null);
- }
- });
- }
- }
-
- #endregion
- /// <summary>
- /// 背景音乐与音效的比例
- /// </summary>
- public const float mRatio = 0.44f;
-
- /// <summary>
- /// 背景声音音量
- /// </summary>
- public float _musicVolumeSize;
- /// <summary>
- /// 音效音量大小
- /// </summary>
- public float effectVolumeSize;
- /// <summary>
- /// 背景声音音量
- /// </summary>
- public float MusicVolumeSize
- {
- get { return _musicVolumeSize * mRatio; }
- }
- /// <summary>
- /// 已加载的AssetBundle
- /// </summary>
- private Dictionary<string, GameObject> musicAudioDic = new Dictionary<string, GameObject>();
- /// <summary>
- /// 已加载的AssetBundle
- /// </summary>
- private Dictionary<string, GameObject> _heroSoundAudioDic = new Dictionary<string, GameObject>();
-
- private void Start()
- {
- mAudioResDic.Clear();
- //读取本地存储的音乐音效的音量设置
- _musicVolumeSize = 1.0f * LocalSettings.MusicVolume / 2 / 100;
- effectVolumeSize = 1.0f * LocalSettings.EffectVolume * 3 / 100;
- }
- /// <summary>
- /// 初始化
- /// </summary>
- protected override void OnAwake()
- {
- // 切换关卡时不删除
- //DontDestroyOnLoad(this);
- if (this.GetComponent<AudioListener>() == null)
- {
- this.gameObject.AddComponent<AudioListener>();
- }
- }
- #region 音乐音效文件的卸载播放对象
- /// <summary>
- /// 卸载某个音效 gameobject
- /// </summary>
- /// <param name="name">名称</param>
- /// <returns>是否卸载成功</returns>
- public bool UnloadHeroSoundAudioObject(string heroModelId)
- {
- if (_heroSoundAudioDic.ContainsKey(heroModelId))
- {
- Destroy(_heroSoundAudioDic[heroModelId]);
- _heroSoundAudioDic.Remove(heroModelId);
- return true;
- }
- else
- {
- LogHelper.LogWarning("角色音效卸载失败_" + heroModelId);
- }
- return false;
- }
- /// <summary>
- /// 卸载某个音效 gameobject
- /// </summary>
- /// <param name="name">名称</param>
- /// <returns>是否卸载成功</returns>
- public bool UnloadMusicAudioObject(string name)
- {
- if (musicAudioDic.ContainsKey(name))
- {
- Destroy(musicAudioDic[name]);
- musicAudioDic.Remove(name);
- return true;
- }
- else
- {
- LogHelper.LogWarning("音效卸载失败_" + name);
- }
- return false;
- }
- /// <summary>
- /// 清理所有音效
- /// </summary>
- public void DestoryAllMusicAudioObject()
- {
- List<string> KeyList = new List<string>(musicAudioDic.Keys);
- for (int i = 0; i < KeyList.Count; i++)
- {
- UnloadMusicAudioObject(KeyList[i]);
- }
- }
- #endregion
- /// <summary>
- ///
- /// </summary>
- private string _curBackMusicResName = "";
- #region
- /// <summary>
- /// 关闭某个背景音乐的播放
- /// </summary>
- /// <param name="soundResName"></param>
- public void StopGameBackMusicPlay(string soundResName, float ts = 0.1f)
- {
- if (musicAudioDic.ContainsKey(soundResName))
- {
- GameObject _audioBack = musicAudioDic[soundResName];
- AudioSource tempSource = _audioBack.GetComponent<AudioSource>();
- tempSource.volume = 0;
- tempSource.Stop();
- //////////键值对儿的形式保存iTween所用到的参数
- ////////Hashtable args = new Hashtable();
- //////////音量
- ////////args.Add("volume", 0);
- //////////变化的时间
- ////////args.Add("time", ts);
- //////////播放结束时调用,参数和上面类似
- ////////////args.Add("oncomplete", "OnAudioPlayEnd");
- ////////////args.Add("oncompleteparams", soundResName);
- ////////////args.Add("oncompletetarget", _audioBack);
- ////////iTween.AudioTo(_audioBack, args);
- }
- }
- /// <summary>
- /// 播放背景音乐
- /// 渐变出现
- /// </summary>
- /// <param name="soundResName">音乐资源</param>
- /// <param name="ts">音乐播放声音正常的时间</param>
- private void PlayMusic(string soundResName, float ts)
- {
- if (string.IsNullOrEmpty(soundResName))
- {
- return;
- }
- if (musicAudioDic.ContainsKey(soundResName))
- {
- GameObject _audioBack = musicAudioDic[soundResName];
- AudioSource xxx = _audioBack.GetComponent<AudioSource>();
- if (soundResName != _curBackMusicResName)
- {
- xxx.Stop();
- xxx.volume = 0;
- //////iTween.AudioTo(_audioBack, 0, 0, 0.5f);
- }
- else
- {
- xxx.Play();
- xxx.volume = MusicVolumeSize;
- //////iTween.AudioTo(_audioBack, MusicVolumeSize, 0, 0.5f);
- }
- }
- else
- {
- GetAudioClip(AudioContainer_Music, soundResName, aClip =>
- {
- if (aClip != null)
- {
- GameObject _audioBack = new GameObject(soundResName);
- _audioBack.AddComponent<GameBackAudio>();
- _audioBack.transform.SetParent(this.transform);
- AudioSource xxx = _audioBack.GetComponent<AudioSource>();
- xxx.clip = aClip;
- xxx.loop = true;
- xxx.pitch = 1;
- xxx.volume = 0;
- xxx.Play();
- musicAudioDic.Add(soundResName, _audioBack);
- //键值对儿的形式保存iTween所用到的参数
- Hashtable args = new Hashtable();
- if (soundResName == _curBackMusicResName)
- {
- //音量
- args.Add("volume", MusicVolumeSize);
- }
- else
- {
- //音量
- args.Add("volume", 0);
- }
- //音调
- args.Add("pitch", 1);
- //变化的时间
- args.Add("time", ts);
- iTween.AudioTo(_audioBack, args);
- }
- });
- }
- }
- /// <summary>
- /// 背景音乐的过渡
- /// </summary>
- /// <param name="newBackMusic">新的背景音乐名称</param>
- /// <param name="delayTime">背景音乐的播放延迟时间</param>
- public void ChangeGameBackMusic(string newBackMusic, float delayTime)
- {
- if (string.IsNullOrEmpty(newBackMusic))
- {
- return;
- }
- if (_curBackMusicResName != newBackMusic)
- {
- if (!string.IsNullOrEmpty(_curBackMusicResName))
- {
- StopGameBackMusicPlay(_curBackMusicResName, delayTime);
- }
- else
- {
- delayTime = 0.1f;
- }
- _curBackMusicResName = newBackMusic;
- PlayMusic(_curBackMusicResName, delayTime);
- }
- else
- {
- }
- }
- /// <summary>
- /// 改变背景音乐的 音量
- /// </summary>
- /// <param name="_vol"></param>
- public void ChangeBackMusicVolume(float _vol)
- {
- _musicVolumeSize = _vol;
- foreach (string soundResName in musicAudioDic.Keys)
- {
- GameObject _audioBack = musicAudioDic[soundResName];
- //键值对儿的形式保存iTween所用到的参数
- Hashtable args = new Hashtable();
- //音量
- args.Add("volume", MusicVolumeSize);
- //音调
- args.Add("pitch", 1);
- //变化的时间
- args.Add("time", 0.01f);
- iTween.AudioTo(_audioBack, args);
- }
- }
- #endregion
- /// <summary>
- /// 改变音效的 音量
- /// </summary>
- /// <param name="_vol"></param>
- public void ChangeEffectMusicVolume(float _vol)
- {
- effectVolumeSize = _vol;
- }
- /// <summary>
- /// 技能的音效播放
- /// 技能音效一个音效文件一个bundle文件
- /// </summary>
- /// <param name="resName">技能音效的 资源名</param>
- public void PlayUISoundEffect(string soundName)
- {
- if (string.IsNullOrEmpty(soundName))
- {
- return;
- }
- GetAudioClip(AudioContainer_UISound, soundName, aClip =>
- {
- if (aClip != null)
- {
- GameObject _effectObj = new GameObject(soundName);
- _effectObj.transform.SetParent(this.transform);
- AudioSource xxx = _effectObj.AddComponent<AudioSource>();
- xxx.clip = aClip;
- xxx.loop = false;
- xxx.pitch = 1;
- xxx.volume = effectVolumeSize;
- xxx.PlayOneShot(aClip);
- Destroy(_effectObj, aClip.length + 1.0f);
- }
- });
- }
- /// <summary>
- /// 技能的音效播放
- /// 技能音效一个音效文件一个bundle文件
- /// </summary>
- /// <param name="resName">技能音效的 资源名</param>
- public void PlaySkillSoundEffect(string resName)
- {
- //LogHelper.LogError("PlaySkillSoundEffect::::" + resName);
- if (string.IsNullOrEmpty(resName))
- {
- return;
- }
- GetAudioClip(AudioManager.AudioContainer_Skill, resName, aClip =>
- {
- if (aClip != null)
- {
- GameObject _effectObj = new GameObject(resName);
- _effectObj.transform.SetParent(this.transform);
- AudioSource xxx = _effectObj.AddComponent<AudioSource>();
- xxx.clip = aClip;
- xxx.loop = false;
- xxx.pitch = 1;
- xxx.volume = effectVolumeSize;
- xxx.PlayOneShot(aClip);
- Destroy(_effectObj, aClip.length + 1.0f);
- }
- });
- }
- /// <summary>
- /// 播放玩家的声音
- /// 因为玩家的声音存在多次播放的可能
- /// </summary>
- /// <param name="heroModelId">模型ID</param>
- /// <param name="soundResName">声音</param>
- public void PlayHeroSound(string heroModelId, string soundResName, ESoundType soundType)
- {
- if (string.IsNullOrEmpty(soundResName))
- {
- return;
- }
- if (_heroSoundAudioDic.ContainsKey(heroModelId))
- {
- }
- else
- {
- GameObject _effectObj = new GameObject(heroModelId);
- _effectObj.transform.SetParent(this.transform);
- _effectObj.AddComponent<AudioSource>();
- _heroSoundAudioDic.Add(heroModelId, _effectObj);
- }
- string containerName = string.Empty;
- switch (soundType)
- {
- case ESoundType.HERO_SOUND_ATTACK:
- containerName = AudioManager.AudioContainer_HeroAnimation_Attack;
- break;
- case ESoundType.HERO_SOUND_DISPLAY:
- containerName = AudioManager.AudioContainer_HeroAnimation_Display;
- break;
- case ESoundType.HERO_SOUND_BEATTACK:
- containerName = AudioManager.AudioContainer_HeroAnimation_BeAttack;
- break;
- }
- if (!string.IsNullOrEmpty(containerName))
- {
- GetAudioClip(containerName, soundResName, aClip =>
- {
- if (aClip != null)
- {
- GameObject _audioBack = _heroSoundAudioDic[heroModelId];
- AudioSource xxx = _audioBack.GetComponent<AudioSource>();
- xxx.clip = aClip;
- xxx.loop = false;
- xxx.pitch = 1;
- xxx.volume = effectVolumeSize;
- if (!xxx.isPlaying)
- {
- xxx.Play();
- }
- }
- });
- }
- }
- }
- public enum ESoundType
- {
- SKILL,
- UI,
- ////HERO_NORMAL,
- ////HERO_ATTACK,
- ////HERO_SKILL,
- ////HERO_CRICAL,
- ////HERO_ANIMATION_IDLE,
- ////HERO_ANIMATION_IDLE1,
- ////HERO_ANIMATION_ATTACK,
- HERO_SOUND_DISPLAY,
- HERO_SOUND_ATTACK,
- HERO_SOUND_BEATTACK
- }
- /// <summary>
- ///
- /// </summary>
- public enum ESoundUIType
- {
- ToggleBtn,
- NormalBtn,
- CloseBtn
- }
|