using UnityEngine;
namespace PhxhSDK
{
//音频播放对象的基类,需要兼容AudioClip和Criware
public abstract class AudioBase
{
protected static int _audioIDCount;
public static int GenAudioID()
{
return ++_audioIDCount;
}
public static void ReleaseAudioIDs()
{
_audioIDCount = 0;
}
public enum PlayState
{
Playing,
Stopped,
Paused,
Loading,
}
protected GameObject _rootGo;
public GameObject RootGo => _rootGo;
protected static int audioCounter = 0;
protected float volume;
protected float targetVolume;
protected float initTargetVolume;
protected float tempFadeSeconds;
protected float fadeInterpolater;
protected float onFadeStartVolume;
protected float spatialBlend;
public AudioType audioType;
protected Transform sourceTransform;
protected PlayState playState;
public int audioID { get; protected set; }
public bool loop { get; set; }
public bool persist { get; set; }
public float fadeInSeconds { get; set; }
public float fadeOutSeconds { get; set; }
public AudioBase()
{
audioID = GenAudioID();
}
public void GenNewID()
{
audioID = GenAudioID();
}
public bool IsPlaying
{
get => playState == PlayState.Playing;
}
public bool IsPaused
{
get => playState == PlayState.Paused;
}
public bool IsStopped
{
get => playState == PlayState.Stopped;
}
public bool IsLoading
{
get => playState == PlayState.Loading;
}
public bool activated { get; protected set; }
public enum AudioType
{
Music,
Sound,
}
//创建一个Source组件用以播放音频
public virtual void CreateSource()
{}
//设置音高
public virtual void SetPitch()
{}
//播放
public void Play()
{
Play(initTargetVolume);
}
public virtual void Play(string CueName, bool loop = false)
{
}
public abstract void Play(float volume);
//终止
public void Stop()
{
fadeInterpolater = 0f;
onFadeStartVolume = volume;
targetVolume = 0f;
playState = PlayState.Stopped;
}
//暂停
public abstract void Pause();
//继续播放
public abstract void Resume();
//设置音量
public void SetVolume(float volume, float fadeSeconds = -1.0f)
{
if (fadeSeconds > 0.0f)
{
SetVolume(volume, fadeSeconds, this.volume);
}
else if (volume > targetVolume)
{
SetVolume(volume, fadeOutSeconds, this.volume);
}
else
{
SetVolume(volume, fadeInSeconds, this.volume);
}
}
///
/// Sets the audio volume
///
/// The target volume
/// How many seconds it needs for the audio to fade in/out to reach target volume. If passed, it will override the Audio's fade in/out seconds, but only for this transition
/// Immediately set the volume to this value before beginning the fade. If not passed, the Audio will start fading from the current volume towards the target volume
public void SetVolume(float volume, float fadeSeconds, float startVolume)
{
targetVolume = Mathf.Clamp01(volume);
fadeInterpolater = 0;
onFadeStartVolume = startVolume;
tempFadeSeconds = fadeSeconds;
}
//刷新函数,需要子类去实现
public abstract void Update();
}
}