An easy to use Unity 3D library for creating illustrated Interactive Fiction games and more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
1.7 KiB

using UnityEngine;
using System.Collections;
namespace Fungus
{
/*
* Helper class to manage play, pause & stop operations on voiceover and writing sound effects
*/
public class DialogAudio
{
public AudioSource audioSource;
public AudioClip audioClip;
public float volume;
public bool loop;
public virtual void Play()
{
if (audioSource == null ||
audioClip == null)
{
return;
}
audioSource.clip = audioClip;
audioSource.loop = loop;
// Fade in the audio at start
LeanTween.value(audioSource.gameObject, 0f, volume, 0.1f).setOnUpdate( (value) => {
audioSource.volume = value;
});
audioSource.Play();
}
public virtual void Pause()
{
if (audioSource == null)
{
return;
}
// Fade out the audio
// There's an audible click if you call audioSource.Pause() so instead just
// drop the volume to 0.
LeanTween.value(audioSource.gameObject, volume, 0f, 0.1f).setOnUpdate( (value) => {
audioSource.volume = value;
});
}
public virtual void Stop()
{
if (audioSource == null)
{
return;
}
// Fade out the audio
LeanTween.value(audioSource.gameObject, audioSource.volume, 0f, 0.1f).setOnUpdate( (value) => {
audioSource.volume = value;
}).setOnComplete( () => {
// There's an audible click if you call audioSource.Stop() so instead we just switch off
// looping and let the audio stop automatically at the end of the clip
audioSource.loop = false;
});
}
public virtual void Resume()
{
if (audioSource == null)
{
return;
}
audioSource.volume = volume;
if (!audioSource.isPlaying)
{
audioSource.loop = loop;
audioSource.Play();
}
}
}
}