/// <summary>/// 普通的音效播放/// </summary> /// <param name="isforceReplay">如果正在播放是否强制重播</param> public bool PlayNormalAudioSync(string audioPath, float volume, bool isforceReplay = false) { if (string.IsNullOrEmpty(audioPath)) { return(false); } string audioName = IOUtility.GetFileNameWithoutExtensionEx(audioPath); if (string.IsNullOrEmpty(audioName)) { return(false); } if (IsPlayingNormalAudio(audioName, out var curAudioSource)) { if (isforceReplay) { curAudioSource.Stop(); curAudioSource.Play(); } return(true); } curAudioSource = GetNextAvailableAudioSource(); LoadAssetResult <AudioClip> assetInfor = ResourcesManagerUtility.LoadAssetSync <AudioClip>(audioPath, null, null); if (assetInfor == null) { mAllAvailableAudioSources.Push(curAudioSource); return(false); } else { assetInfor.ReferenceWithComponent(curAudioSource, (audioClip) => { if (curAudioSource.clip == audioClip) { return; } ResourcesManagerUtility.ReleaseComponentReferenceAsset(curAudioSource, curAudioSource.clip); curAudioSource.clip = audioClip; }); curAudioSource.volume = volume; mAllPlayingAudioSources.AddLast(curAudioSource); return(true); } }
private bool PlayBackgroundAudioSync(LoadAssetResult <AudioClip> clipAsset, float volume, bool isLoop = true, bool isforceReplay = false) { if (clipAsset == null) { return(false); } if (mCurBackgroundAudioSource.clip != null) { if (mCurBackgroundAudioSource.clip.name == clipAsset.mLoadAssetInstanceName) { mCurBackgroundAudioSource.volume = volume; mCurBackgroundAudioSource.loop = isLoop; if (isforceReplay) { mCurBackgroundAudioSource.Stop(); mCurBackgroundAudioSource.Play(); } return(true); } //当前正在播放这个背景音 else { mCurBackgroundAudioSource.Stop(); //结束当前的背景音 } } clipAsset.ReferenceWithComponent(mCurBackgroundAudioSource, (audioClip) => { if (mCurBackgroundAudioSource.clip == audioClip) { return; } ResourcesManagerUtility.ReleaseComponentReferenceAsset(mCurBackgroundAudioSource, mCurBackgroundAudioSource.clip); mCurBackgroundAudioSource.clip = audioClip; return; }); mCurBackgroundAudioSource.volume = volume; mCurBackgroundAudioSource.loop = isLoop; mCurBackgroundAudioSource.Play(); return(true); }
/// <summary> /// 播放背景音 /// </summary> /// <param name="audioPath"></param> /// <param name="volume"></param> /// <param name="isLoop"></param> /// <param name="isforceReplay"></param> /// <returns></returns> public bool PlayBackgroundAudioSync(string audioPath, float volume, bool isLoop = true, bool isforceReplay = false) { if (string.IsNullOrEmpty(audioPath)) { return(false); } string audioName = IOUtility.GetFileNameWithoutExtensionEx(audioPath); if (string.IsNullOrEmpty(audioName)) { return(false); } if (mCurBackgroundAudioSource.clip != null) { if (mCurBackgroundAudioSource.clip.name == audioName) { mCurBackgroundAudioSource.volume = volume; mCurBackgroundAudioSource.loop = isLoop; if (isforceReplay) { mCurBackgroundAudioSource.Stop(); mCurBackgroundAudioSource.Play(); } return(true); } //当前正在播放这个背景音 } LoadAssetResult <AudioClip> clipAsset = ResourcesManagerUtility.LoadAssetSync <AudioClip>(audioPath, null, null); if (clipAsset == null) { return(false); } return(PlayBackgroundAudioSync(clipAsset, volume, isLoop, isforceReplay)); }