using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
namespace TFramework
{
///
/// 音频管理器
///
public class AudioManager : BaseManager
{
[SerializeField]
private AudioSource _backgroundSource;
[SerializeField]
private AudioSource _otherSource;
private IEnumerator _playOver;
///
/// 是否静音
///
public bool Mute
{
get { return _backgroundSource.mute && _otherSource.mute; }
set
{
_backgroundSource.mute = value;
_otherSource.mute = value;
}
}
///
/// 背景音乐音量
///
public float BackgroundVolme
{
get => _backgroundSource.volume;
set => _backgroundSource.volume = value;
}
///
/// 其它音效音量
///
public float OtherVolme
{
get => _otherSource.volume;
set => _otherSource.volume = value;
}
///
/// 播放背景音乐
///
/// 音频资源
/// 是否循环
/// 播放速度
/// /// 播放完事件
public void PlayBackgroundMusic(AudioClip clip,bool isLoop=true,float speed=1,TAction action=null)
{
_backgroundSource.volume = BackgroundVolme;
PlayMusic(AudioSourceType.BackgroundSource, clip, isLoop, speed, action);
}
///
/// 暂停播放背景音乐
///
public void PauseBackgroundMusic() => PauseMusic(AudioSourceType.BackgroundSource);
///
/// 恢复播放背景音乐
///
public void ResumeBackgroundMusic() => ResumeMusic(AudioSourceType.BackgroundSource);
///
/// 停止播放背景音乐
///
public void StopBackgroundMusic() => StopMusic(AudioSourceType.BackgroundSource);
///
/// 播放其它音乐
///
/// 音频资源
/// 是否循环
/// 播放速度
/// /// 播放完事件
public void PlayOtherMusic(AudioClip clip, bool isLoop = false, float speed = 1,TAction action=null)
{
_otherSource.volume = OtherVolme;
PlayMusic(AudioSourceType.OtherSource, clip, isLoop, speed, action);
}
public async Task PlayOtherMusicAsync(AudioClip clip, bool isLoop = false, float speed = 1)
{
_otherSource.volume = OtherVolme;
await PlayMusic(AudioSourceType.OtherSource, clip, isLoop, speed);
}
///
/// 暂停播放其它音乐
///
public void PauseOtherMusic() => PauseMusic(AudioSourceType.OtherSource);
///
/// 恢复播放其它音乐
///
public void ResumeOtherMusic() => ResumeMusic(AudioSourceType.OtherSource);
///
/// 停止播放其它音乐
///
public void StopOtherMusic() => StopMusic(AudioSourceType.OtherSource);
///
/// 播放音乐
///
/// 音源类型
/// 音源
/// 是否循环
/// 播放速度
/// 播放完事件
public void PlayMusic(AudioSourceType type, AudioClip clip, bool isLoop = true, float speed = 1,TAction action=null)
{
AudioSource source = GetAudioSource(type);
if (clip == null||source==null) return;
if (source.isPlaying)
source.Stop();
source.clip = clip;
source.loop = isLoop;
source.pitch = speed;
source.Play();
if(action!=null)
{
float time = clip.length / speed;
if(_playOver!=null)
{
StopCoroutine(_playOver);
_playOver = null;
}
_playOver = PlayOver(time,action);
StartCoroutine(_playOver);
}
}
public async Task PlayMusic(AudioSourceType type, AudioClip clip, bool isLoop = true, float speed = 1)
{
AudioSource source = GetAudioSource(type);
if (clip == null || source == null) return;
if (source.isPlaying)
source.Stop();
source.clip = clip;
source.loop = isLoop;
source.pitch = speed;
source.Play();
float time = clip.length / speed;
await Task.Delay((int)time*1000);
}
private IEnumerator PlayOver(float time,TAction action)
{
yield return YieldWaitTool.YieldWaitForSeconds(time);
action?.Invoke();
}
///
/// 停止播放音乐
///
/// 音源类型
public void StopMusic(AudioSourceType type)
{
AudioSource source = GetAudioSource(type);
if (source&&source.isPlaying)
source.Stop();
if (_playOver != null)
{
StopCoroutine(_playOver);
_playOver = null;
}
}
///
/// 暂停播放音乐
///
/// 音源类型
public void PauseMusic(AudioSourceType type)
{
AudioSource source = GetAudioSource(type);
source?.Pause();
}
///
/// 恢复播放音乐
///
/// 音源类型
public void ResumeMusic(AudioSourceType type)
{
AudioSource source = GetAudioSource(type);
source?.UnPause();
}
private AudioSource GetAudioSource(AudioSourceType type)
{
switch(type)
{
case AudioSourceType.BackgroundSource:
return _backgroundSource;
case AudioSourceType.OtherSource:
return _otherSource;
default:
return null;
}
}
public enum AudioSourceType
{
BackgroundSource,
OtherSource
}
}
}