Custom Sequencer Command Example: Volume

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
User avatar
Tony Li
Posts: 22056
Joined: Thu Jul 18, 2013 1:27 pm

Custom Sequencer Command Example: Volume

Post by Tony Li »

Someone asked in email how to write a custom sequencer command to set an Audio Source's volume. Here's the code:

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;
            }
        }
    }
}
Post Reply