Forest_Client/Forest/Assets/PhxhSDK/Phxh/Audio/AudioBase.cs

166 lines
4.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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);
}
}
/// <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;
}
//刷新函数,需要子类去实现
public abstract void Update();
}
}