197 lines
5.5 KiB
C#
197 lines
5.5 KiB
C#
using UnityEngine;
|
|
|
|
//AudioClip实现音频类
|
|
|
|
namespace PhxhSDK
|
|
{
|
|
public class AudioAuClip : AudioBase
|
|
{
|
|
private AudioClip initClip;
|
|
|
|
public AudioSource audioSource;
|
|
|
|
public AudioClip clip
|
|
{
|
|
get { return audioSource == null ? initClip : audioSource.clip; }
|
|
}
|
|
|
|
protected AudioAuClip(AudioType audioType, AudioClip clip, bool loop, bool persist, float volume,
|
|
float fadeInValue,
|
|
float fadeOutValue, Transform sourceTransform, float minDis = 0f, float maxDis = 0f)
|
|
{
|
|
if (sourceTransform == null)
|
|
{
|
|
this.sourceTransform = AudioManager.Instance.initGameObject.transform;
|
|
}
|
|
else
|
|
{
|
|
this.sourceTransform = sourceTransform;
|
|
}
|
|
|
|
this.audioID = audioCounter;
|
|
audioCounter++;
|
|
|
|
this.audioType = audioType;
|
|
this.initClip = clip;
|
|
this.loop = loop;
|
|
this.persist = persist;
|
|
this.targetVolume = volume;
|
|
this.initTargetVolume = volume;
|
|
this.tempFadeSeconds = -1;
|
|
this.volume = 0f;
|
|
this.fadeInSeconds = fadeInValue;
|
|
this.fadeOutSeconds = fadeOutValue;
|
|
|
|
this.playState = PlayState.Stopped;
|
|
this.activated = false;
|
|
|
|
CreateAudiosource(clip, loop, minDis, maxDis);
|
|
Play();
|
|
}
|
|
|
|
public override void CreateSource()
|
|
{
|
|
}
|
|
|
|
//创建AudioSource
|
|
void CreateAudiosource(AudioClip _clip, bool _loop, float min = 0f, float max = 0f)
|
|
{
|
|
audioSource = sourceTransform.gameObject.AddComponent<AudioSource>() as AudioSource;
|
|
|
|
audioSource.clip = _clip;
|
|
audioSource.loop = _loop;
|
|
audioSource.volume = 1f;
|
|
if (sourceTransform != AudioManager.Instance.initGameObject.transform)
|
|
{
|
|
//3D 1 2D 0
|
|
audioSource.spatialBlend = 1;
|
|
//Set3DDistances(min, max);
|
|
}
|
|
}
|
|
|
|
public override void Play(float volume)
|
|
{
|
|
if (audioSource == null)
|
|
{
|
|
CreateAudiosource(initClip, loop);
|
|
}
|
|
|
|
audioSource.Play();
|
|
playState = PlayState.Playing;
|
|
|
|
fadeInterpolater = 0f;
|
|
onFadeStartVolume = this.volume;
|
|
targetVolume = volume;
|
|
}
|
|
|
|
//暂停
|
|
public override void Pause()
|
|
{
|
|
audioSource.Pause();
|
|
playState = PlayState.Paused;
|
|
}
|
|
|
|
//继续播放
|
|
public override void Resume()
|
|
{
|
|
audioSource.UnPause();
|
|
playState = PlayState.Playing;
|
|
}
|
|
|
|
//设置播放速度
|
|
public void SetPitch(float _pitch)
|
|
{
|
|
if (audioSource != null)
|
|
{
|
|
audioSource.pitch = _pitch;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the Audio 3D max distance
|
|
/// </summary>
|
|
/// <param name="max">the max distance</param>
|
|
public void Set3DMaxDistance(float max)
|
|
{
|
|
audioSource.maxDistance = max;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the Audio 3D min distance
|
|
/// </summary>
|
|
/// <param name="min">the min distance</param>
|
|
public void Set3DMinDistance(float min)
|
|
{
|
|
audioSource.minDistance = min;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the Audio 3D distances
|
|
/// </summary>
|
|
/// <param name="min">the min distance</param>
|
|
/// <param name="max">the max distance</param>
|
|
public void Set3DDistances(float min, float max)
|
|
{
|
|
Set3DMinDistance(min);
|
|
Set3DMaxDistance(max);
|
|
}
|
|
|
|
public override void Update()
|
|
{
|
|
if (audioSource == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
activated = true;
|
|
|
|
if (volume != targetVolume)
|
|
{
|
|
float fadeValue;
|
|
fadeInterpolater += Time.unscaledDeltaTime;
|
|
if (volume > targetVolume)
|
|
{
|
|
fadeValue = tempFadeSeconds != -1 ? tempFadeSeconds : fadeOutSeconds;
|
|
}
|
|
else
|
|
{
|
|
fadeValue = tempFadeSeconds != -1 ? tempFadeSeconds : fadeInSeconds;
|
|
}
|
|
|
|
volume = Mathf.Lerp(onFadeStartVolume, targetVolume, fadeInterpolater / fadeValue);
|
|
}
|
|
else if (tempFadeSeconds != -1)
|
|
{
|
|
tempFadeSeconds = -1;
|
|
}
|
|
|
|
switch (audioType)
|
|
{
|
|
case AudioType.Music:
|
|
{
|
|
audioSource.volume = volume * AudioManager.Instance.GlobalMusicVolume *
|
|
AudioManager.Instance.GlobalVolume;
|
|
break;
|
|
}
|
|
case AudioType.Sound:
|
|
{
|
|
audioSource.volume = volume * AudioManager.Instance.GlobalSoundsVolume *
|
|
AudioManager.Instance.GlobalVolume;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (volume == 0f && IsStopped)
|
|
{
|
|
audioSource.Stop();
|
|
}
|
|
|
|
// Update playing status
|
|
if (audioSource.isPlaying != IsPlaying && Application.isFocused)
|
|
{
|
|
//playState = PlayState.Playing;
|
|
playState = PlayState.Stopped;
|
|
}
|
|
}
|
|
}
|
|
} |