82 lines
2.2 KiB
C#
82 lines
2.2 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Audio;
|
|
using Platinio.TweenEngine;
|
|
|
|
namespace VRBeats
|
|
{
|
|
[RequireComponent(typeof(AudioSource))]
|
|
public class AudioManager : MonoBehaviour
|
|
{
|
|
[SerializeField] private AudioMixerGroup mixerGroup = null;
|
|
[SerializeField] private float fadeOutTime = 4.0f;
|
|
|
|
private AudioSource audioSource = null;
|
|
private double scheduledDspStartTime = -1.0;
|
|
private bool hasScheduledClip = false;
|
|
|
|
private void Start()
|
|
{
|
|
audioSource = GetComponent<AudioSource>();
|
|
audioSource.outputAudioMixerGroup = mixerGroup;
|
|
|
|
ResetThisComponent();
|
|
|
|
}
|
|
|
|
private void ResetThisComponent()
|
|
{
|
|
SetAudioMixerPitch(1.0f);
|
|
gameObject.CancelAllTweens();
|
|
}
|
|
|
|
public BaseTween BlendAudioMixerPitch(float from ,float to)
|
|
{
|
|
return PlatinioTween.instance.ValueTween(from , to , fadeOutTime).SetEase(Ease.EaseOutExpo).SetOnUpdateFloat(delegate (float v)
|
|
{
|
|
if(audioSource != null)
|
|
SetAudioMixerPitch(v);
|
|
}).SetOwner(gameObject);
|
|
}
|
|
|
|
public void SetAudioMixerPitch(float value)
|
|
{
|
|
audioSource.outputAudioMixerGroup.audioMixer.SetFloat("Pitch", value);
|
|
}
|
|
|
|
public void PlayClip(AudioClip clip)
|
|
{
|
|
PlayClipScheduled(clip);
|
|
}
|
|
|
|
public double PlayClipScheduled(AudioClip clip, double delaySeconds = 0.1)
|
|
{
|
|
ResetThisComponent();
|
|
audioSource.Stop();
|
|
audioSource.clip = clip;
|
|
audioSource.time = 0.0f;
|
|
|
|
scheduledDspStartTime = AudioSettings.dspTime + delaySeconds;
|
|
hasScheduledClip = true;
|
|
audioSource.PlayScheduled(scheduledDspStartTime);
|
|
|
|
return scheduledDspStartTime;
|
|
}
|
|
|
|
public float CurrentTime
|
|
{
|
|
get
|
|
{
|
|
if (audioSource == null)
|
|
return 0.0f;
|
|
|
|
if (hasScheduledClip)
|
|
return (float)(AudioSettings.dspTime - scheduledDspStartTime);
|
|
|
|
return audioSource.time;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|