Custom Sequencer Command Example: Volume
Posted: Tue Feb 19, 2019 9:24 am
Someone asked in email how to write a custom sequencer command to set an Audio Source's volume. Here's the code:
SequencerCommandVolume.cs
SequencerCommandVolume.cs
Code: Select all
using UnityEngine;
namespace PixelCrushers.DialogueSystem.SequencerCommands
{
public class SequencerCommandVolume : SequencerCommand
{
public void Start()
{
Stop(); // Immediately go to OnDestroy.
}
public void OnDestroy()
{
// Do it here so "required" commands run even if the line is skipped:
var subject = GetSubject(0, speaker);
var audioSource = (subject != null) ? subject.GetComponent<AudioSource>() : null;
var volume = GetParameterAsFloat(1);
if (audioSource == null)
{
if (DialogueDebug.logWarnings) Debug.LogWarning("Dialogue System: Volume(" + GetParameters() + ") can't find audio source.", subject);
}
else
{
if (DialogueDebug.logInfo) Debug.Log("Dialogue System: Volume(" + GetParameters() + ").", subject);
audioSource.volume = volume;
}
}
}
}