/// <summary> /// 初始化声音代理的新实例 /// </summary> /// <param name="soundGroup">所在的声音组</param> /// <param name="soundHelper">声音辅助器接口</param> /// <param name="soundAgentHelper">声音代理辅助器接口</param> public SoundAgent(SoundGroup soundGroup, SoundAgentHelper soundAgentHelper) { if (soundGroup == null) { throw new Exception("Sound group is invalid."); } if (soundAgentHelper == null) { throw new Exception("Sound agent helper is invalid."); } m_SoundGroup = soundGroup; m_SoundAgentHelper = soundAgentHelper; m_SoundAgentHelper.ResetSoundAgent += OnResetSoundAgent; m_SerialId = 0; m_SoundAsset = null; Reset(); }
/// <summary> /// 播放声音 /// </summary> /// <param name="soundAssetName">声音资源名称</param> /// <param name="soundGroupName">声音组名称</param> /// <param name="priority">加载声音资源的优先级</param> /// <param name="playSoundParams">播放声音参数</param> /// <param name="userData">用户自定义数据</param> /// <returns>声音的序列编号</returns> public int PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams, object userData) { if (playSoundParams == null) { playSoundParams = new PlaySoundParams(); } int serialId = m_Serial++; PlaySoundErrorCode?errorCode = null; string errorMessage = null; SoundGroup soundGroup = GetSoundGroup(soundGroupName); if (soundGroup == null) { errorCode = PlaySoundErrorCode.SoundGroupNotExist; errorMessage = string.Format("Sound group '{0}' is not exist.", soundGroupName); } else if (soundGroup.SoundAgentCount <= 0) { errorCode = PlaySoundErrorCode.SoundGroupHasNoAgent; errorMessage = string.Format("Sound group '{0}' is have no sound agent.", soundGroupName); } if (errorCode.HasValue) { if (PlaySoundFailure != null) { PlaySoundFailure(serialId, soundAssetName, soundGroupName, playSoundParams, errorCode.Value, errorMessage, userData); return(serialId); } throw new Exception(errorMessage); } m_SoundsBeingLoaded.Add(serialId); GameEntry.Resource.LoadAsset(soundAssetName, typeof(AudioClip), priority, m_LoadAssetCallbacks, new SoundInfo(serialId, soundGroup, playSoundParams, userData)); return(serialId); }
/// <summary> /// 增加声音组 /// </summary> /// <param name="soundGroupName">声音组名称</param> /// <param name="soundGroupAvoidBeingReplacedBySamePriority">声音组中的声音是否避免被同优先级声音替换</param> /// <param name="soundGroupMute">声音组是否静音</param> /// <param name="soundGroupVolume">声音组音量</param> /// <param name="soundGroupHelper">声音组辅助器</param> /// <returns>是否增加声音组成功</returns> public bool AddSoundGroup(string soundGroupName, bool soundGroupAvoidBeingReplacedBySamePriority, bool soundGroupMute, float soundGroupVolume) { if (string.IsNullOrEmpty(soundGroupName)) { throw new Exception("Sound group name is invalid."); } if (HasSoundGroup(soundGroupName)) { return(false); } SoundGroup soundGroup = new SoundGroup(soundGroupName) { AvoidBeingReplacedBySamePriority = soundGroupAvoidBeingReplacedBySamePriority, Mute = soundGroupMute, Volume = soundGroupVolume }; m_SoundGroups.Add(soundGroupName, soundGroup); return(true); }