228 lines
6.4 KiB
C#
228 lines
6.4 KiB
C#
using UnityEngine;
|
|
|
|
//AudioClip实现音频类
|
|
|
|
namespace PhxhSDK
|
|
{
|
|
public struct UnityAudio
|
|
{
|
|
public bool Loop;
|
|
public bool Persist;
|
|
public float Volume;
|
|
public float FadeInValue;
|
|
public float FadeOutValue;
|
|
public float SpatialBlend;
|
|
public float MinDis;
|
|
public float MaxDis;
|
|
|
|
public UnityAudio(bool loop)
|
|
{
|
|
Loop = loop;
|
|
Persist = false;
|
|
Volume = 1;
|
|
FadeInValue = 1f;
|
|
FadeOutValue = 1f;
|
|
SpatialBlend = 0;
|
|
MinDis = 0;
|
|
MaxDis = 0;
|
|
}
|
|
}
|
|
|
|
public class AudioAuClip : AudioBase
|
|
{
|
|
private AudioClip initClip;
|
|
|
|
public AudioSource audioSource;
|
|
|
|
public GameObject RootGo => sourceTransform.gameObject;
|
|
|
|
public AudioClip Clip
|
|
{
|
|
get { return audioSource == null ? initClip : audioSource.clip; }
|
|
}
|
|
|
|
public string AudioName => initClip.name;
|
|
|
|
protected AudioAuClip(AudioType audioType, AudioClip clip, Transform sourceTransform, bool loop, bool persist,
|
|
float volume,
|
|
float fadeInValue,
|
|
float fadeOutValue,
|
|
float spatialBlend = 0f,
|
|
float minDis = 0f, float maxDis = 0f)
|
|
{
|
|
if (sourceTransform == null)
|
|
{
|
|
this.sourceTransform = AudioUnityMgr.Instance.initGameObject.transform;
|
|
}
|
|
else
|
|
{
|
|
this.sourceTransform = sourceTransform;
|
|
}
|
|
|
|
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 = volume;
|
|
this.fadeInSeconds = fadeInValue;
|
|
this.fadeOutSeconds = fadeOutValue;
|
|
this.spatialBlend = spatialBlend;
|
|
this.playState = PlayState.Stopped;
|
|
this.activated = false;
|
|
|
|
CreateAudiosource(clip, loop, spatialBlend, minDis, maxDis);
|
|
Play();
|
|
}
|
|
|
|
public override void CreateSource()
|
|
{
|
|
}
|
|
|
|
//创建AudioSource
|
|
void CreateAudiosource(AudioClip _clip, bool _loop, float spatialBlend = 0f, float min = 0f, float max = 0f)
|
|
{
|
|
audioSource = sourceTransform.gameObject.AddComponent<AudioSource>();
|
|
|
|
audioSource.clip = _clip;
|
|
audioSource.loop = _loop;
|
|
audioSource.volume = 1f;
|
|
if (sourceTransform != AudioUnityMgr.Instance.initGameObject.transform)
|
|
{
|
|
//3D 1 2D 0
|
|
audioSource.spatialBlend = spatialBlend;
|
|
//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 * AudioUnityMgr.Instance.GlobalMusicVolume *
|
|
AudioUnityMgr.Instance.GlobalVolume;
|
|
break;
|
|
}
|
|
case AudioType.Sound:
|
|
{
|
|
audioSource.volume = volume * AudioUnityMgr.Instance.GlobalSoundsVolume *
|
|
AudioUnityMgr.Instance.GlobalVolume;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (volume == 0f && IsStopped)
|
|
{
|
|
audioSource.Stop();
|
|
}
|
|
|
|
// Update playing status
|
|
if (audioSource.isPlaying != IsPlaying && Application.isFocused)
|
|
{
|
|
//playState = PlayState.Playing;
|
|
playState = PlayState.Stopped;
|
|
}
|
|
}
|
|
}
|
|
} |