/// <summary> /// 预加载音频资源 /// </summary> public void Preload(string location, EAudioLayer audioLayer) { if (_assets.ContainsKey(location) == false) { AssetAudio asset = new AssetAudio(location, audioLayer); _assets.Add(location, asset); asset.Load(null); } }
/// <summary> /// 使用外部音频源播放音效 /// </summary> /// <param name="audioSource">外部的音频源</param> /// <param name="location">资源地址</param> public void PlaySound(AudioSource audioSource, string location) { if (audioSource == null) { return; } if (audioSource.isActiveAndEnabled == false) { return; } if (string.IsNullOrEmpty(location)) { return; } // 如果静音状态直接跳过播放 if (IsMute(EAudioLayer.Sound)) { return; } if (_assets.ContainsKey(location)) { if (_assets[location].Clip != null) { audioSource.PlayOneShot(_assets[location].Clip); } } else { // 新建加载资源 AssetAudio assetAudio = new AssetAudio(location, EAudioLayer.Sound); _assets.Add(location, assetAudio); assetAudio.Load((AudioClip clip) => { if (clip != null) { if (audioSource != null) //注意:在加载过程中音频源可能被销毁,所以需要判空 { audioSource.PlayOneShot(_assets[location].Clip); } } }); } }
private void PlayAudioClip(EAudioLayer layer, string location, bool isLoop) { if (_assets.ContainsKey(location)) { if (_assets[location].Clip != null) { PlayAudioClipInternal(layer, _assets[location].Clip, isLoop); } } else { // 新建加载资源 AssetAudio assetAudio = new AssetAudio(location, layer); _assets.Add(location, assetAudio); assetAudio.Load((AudioClip clip) => { if (clip != null) { PlayAudioClipInternal(layer, clip, isLoop); } }); } }