public static void Play(string ePkg, AudioPlay2D.AudioType type, string name, float volume, bool loop) { if (string.IsNullOrEmpty(name)) { return; } InitInstance(); gInstance.PlayAudio(ePkg, type, name, volume, loop); }
/************************************ * 函数说明: 设置某种类型声音是否静音 * 返 回 值: void * 参数说明: type * 参数说明: volume 音量,百分比 ************************************/ public static void SetVolume(AudioPlay2D.AudioType type, int volume) { InitInstance(); /** @brief: 查询所有的声音 设置是否静音 */ foreach (KeyValuePair <int, Audio2DData> mianKey in gInstance.mDicOpenAudioList) { if (mianKey.Value.eType == type) { if (mianKey.Value.audioSource != null) { mianKey.Value.audioSource.volume = volume / 100f; } } } }
/************************************ * 函数说明: 设置某种类型声音是否静音 * 返 回 值: void * 参数说明: type * 参数说明: bMute * 注意事项: true 表示静音 , false 表示非静音 * 根据需求自行添加音效类型 目前只区分 背景音效 和 其他音效 ************************************/ public static void SetMute(AudioPlay2D.AudioType type, bool bMute) { if (gAudioMute[(int)type] == bMute) { return; } InitInstance(); gAudioMute[(int)type] = bMute; /** @brief: 查询所有的声音 设置是否静音 */ foreach (KeyValuePair <int, Audio2DData> mianKey in gInstance.mDicOpenAudioList) { if (mianKey.Value.eType == type) { if (mianKey.Value.audioSource != null) { mianKey.Value.audioSource.mute = bMute; } } } }
/************************************ * 函数说明: 播放声音 * 返 回 值: void * 参数说明: ePkg * 参数说明: name * 参数说明: volume * 参数说明: loop * 注意事项: ************************************/ void PlayAudio(string ePkg, AudioPlay2D.AudioType type, string name, float volume, bool loop) { int iKey = GetAudioHashCode(ePkg, name); if (iKey == 0) { return; } /** @brief: 如果当前类型为静音 且不重复 那么跳过 */ if (gAudioMute[(int)type] == true && loop == false) { return; } /** @brief: 如果播放还未停止或者已经加载过 */ if (mDicOpenAudioList.ContainsKey(iKey)) { AudioSource audio = mDicOpenAudioList[iKey].audioSource; /** @brief: 资源已经加载过 直接播放 */ if (audio.clip != null) { audio.volume = volume; audio.loop = loop; audio.pitch = 1.0f; audio.mute = gAudioMute[(int)type]; audio.Play(); mDicOpenAudioList[iKey].ePlayState = Audio2DData.PlayState.Play; return; } else { mDicOpenAudioList[iKey].ePlayState = Audio2DData.PlayState.Stop; } } else { /** @brief: 初次次播放 */ mDicOpenAudioList[iKey] = new Audio2DData(type); /** @brief: 创建播放者 */ GameObject audioPlayer = new GameObject(iKey.ToString()); audioPlayer.transform.parent = mPlayerRoot.transform; /** @brief: 添加记录 */ mDicOpenAudioList[iKey].audioSource = audioPlayer.AddComponent <AudioSource>(); /** @brief: 设置为2D音效 */ mDicOpenAudioList[iKey].audioSource.spatialBlend = 0; } /** @brief: 加载音频 */ mDicOpenAudioList[iKey].ePlayState = Audio2DData.PlayState.eWaitPlay; GetAudioClip(ePkg, name, (clip, bRet) => { if (mDicOpenAudioList.ContainsKey(iKey)) { if (mDicOpenAudioList[iKey].ePlayState != Audio2DData.PlayState.eWaitPlay) { Debug.LogWarning("You have Stop or Pause this audio first!"); return; } AudioSource audio = mDicOpenAudioList[iKey].audioSource; if (audio.clip == null) { #region 第一次加载 if (bRet == true && clip != null) { audio.clip = clip; audio.volume = volume; audio.loop = loop; audio.pitch = 1.0f; audio.mute = gAudioMute[(int)type]; audio.Play(); mDicOpenAudioList[iKey].ePlayState = Audio2DData.PlayState.Play; } else { Destroy(audio.gameObject); mDicOpenAudioList.Remove(iKey); } #endregion } else { #region 已经加载过 audio.volume = volume; audio.loop = loop; audio.pitch = 1.0f; audio.mute = gAudioMute[(int)type]; if (audio.isPlaying == false) { audio.Play(); } mDicOpenAudioList[iKey].ePlayState = Audio2DData.PlayState.Play; #endregion } } }); }
public Audio2DData(AudioPlay2D.AudioType type) { eType = type; }
public static void PlayOfType(string ePkg, AudioPlay2D.AudioType type, string name, bool loop) { Play(ePkg, type, name, 1.0f, loop); }