2023-10-21 11:55:23 +08:00
using UnityEngine ;
2023-08-22 19:08:45 +08:00
//音频播放对象的基类, 需要兼容AudioClip和Criware
2023-10-21 11:55:23 +08:00
public abstract class AudioBase
2023-08-22 19:08:45 +08:00
{
public enum PlayState
{
Playing ,
Stopped ,
2023-10-19 15:00:19 +08:00
Paused ,
Loading ,
2023-08-22 19:08:45 +08:00
}
2023-10-21 11:55:23 +08:00
2023-08-22 19:08:45 +08:00
protected static int audioCounter = 0 ;
protected float volume ;
protected float targetVolume ;
protected float initTargetVolume ;
protected float tempFadeSeconds ;
protected float fadeInterpolater ;
protected float onFadeStartVolume ;
protected 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 bool IsPlaying
{
get = > playState = = PlayState . Playing ;
}
public bool IsPaused
{
get = > playState = = PlayState . Paused ;
}
public bool IsStopped
{
get = > playState = = PlayState . Stopped ;
}
2023-10-19 15:00:19 +08:00
public bool IsLoading
{
get = > playState = = PlayState . Loading ;
}
2023-08-22 19:08:45 +08:00
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 ) ;
}
2023-10-19 15:00:19 +08:00
public virtual void Play ( string CueName , bool loop = false )
{
}
2023-10-21 11:55:23 +08:00
public abstract void Play ( float volume ) ;
2023-08-22 19:08:45 +08:00
//终止
public void Stop ( )
{
fadeInterpolater = 0f ;
onFadeStartVolume = volume ;
targetVolume = 0f ;
playState = PlayState . Stopped ;
}
//暂停
2023-10-21 11:55:23 +08:00
public abstract void Pause ( ) ;
2023-08-22 19:08:45 +08:00
//继续播放
2023-10-21 11:55:23 +08:00
public abstract void Resume ( ) ;
2023-08-22 19:08:45 +08:00
//设置音量
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 ) ;
}
}
/// <summary>
/// Sets the audio volume
/// </summary>
/// <param name="volume">The target volume</param>
/// <param name="fadeSeconds">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</param>
/// <param name="startVolume">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</param>
public void SetVolume ( float volume , float fadeSeconds , float startVolume )
{
targetVolume = Mathf . Clamp01 ( volume ) ;
fadeInterpolater = 0 ;
onFadeStartVolume = startVolume ;
tempFadeSeconds = fadeSeconds ;
}
//刷新函数,需要子类去实现
2023-10-21 11:55:23 +08:00
public abstract void Update ( ) ;
2023-08-22 19:08:45 +08:00
}