Fade Audio Custom Sequencer Command

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Keiji
Posts: 17
Joined: Tue Feb 18, 2020 2:29 pm

Fade Audio Custom Sequencer Command

Post by Keiji »

Hi Tony!

I've been trying to get a custom sequencer command working for fading the volume of an objects audio source over time.
Basically, I want to be able to also fade out (and back in) the volume of the audiosource of the background music/ambience object when I use the Fade() sequencer command. Could you help me with this?

What I am trying to do is something like:

FadeAudio(ObjectWithAudiosource,EndVolume,Duration);

I think the problem I've been having is I wasn't really sure where to place the coroutine in the sequencer command template.
Thank you so much for making and continually improving this system, it is a blast to work with and I count it among the best experiences I have had with Unity.

Keiji
User avatar
Tony Li
Posts: 21636
Joined: Thu Jul 18, 2013 1:27 pm

Re: Fade Audio Custom Sequencer Command

Post by Tony Li »

Hi,

Thanks for the kind words!

You can run the Start() method as a coroutine. I recommend also setting the final volume in the OnDestroy() method, so you'll be guaranteed to end at the correct volume even if the player skips ahead before the full duration has elapsed Example"

Code: Select all

public class SequencerCommandFadeAudio : SequencerCommand
{
    IEnumerator Start()
    {
        //... (Your code here)
        Stop();
    }
        
     void OnDestroy()
     {
         // (Your code here to set final volume)
     }
 }
Keiji
Posts: 17
Joined: Tue Feb 18, 2020 2:29 pm

Re: Fade Audio Custom Sequencer Command

Post by Keiji »

Hi Tony! Thanks for the quick reply. I gave it a shot but unfortunately wasn't able to get it working. My coding knowledge is just not strong enough figure out what's wrong. Could you give it a look and maybe say how you would do it instead?

Thanks so much in advance,

Keiji

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace PixelCrushers.DialogueSystem.SequencerCommands
{

    // Syntax: FadeAudio(AudioObjectName, endValue, fadeDuration)
    public class SequencerCommandFadeAudio : SequencerCommand
    {

        private void Awake()
        {
            // Get the values of the parameters:
            var AudioObject = GetSubject(0);

            var audioSource = AudioObject.GetComponent<AudioSource>();
            var currentValue = audioSource.volume;

            var endValue = GetParameterAsFloat(1);
            var fadeDuration = GetParameterAsFloat(2);


            if (AudioObject == null)
            {
                Debug.LogWarning("Sequencer Command FadeAudio(): Can't find GameObject '" + GetParameter(0) + "'");
            }

            IEnumerator Start()
            {

                if (currentValue > endValue)  // If current volume of object is bigger than desired volume, lower it.
                {
                    while (audioSource.volume > endValue)
                    {
                        audioSource.volume -= currentValue * Time.deltaTime / fadeDuration;

                        yield return null;
                    }
                }

                else if (currentValue < endValue) // If current volume of object is smaller than desired volume, turn it up.
                {
                    while (audioSource.volume < endValue)
                    {
                        audioSource.volume += currentValue * Time.deltaTime / fadeDuration;

                        yield return null;
                    }
                }
                
                else if (currentValue == endValue)
                {
                    Debug.LogWarning("Sequencer Command FadeAudio() currentValue and endValue are already the same.");
                }
           

                Stop();
            }

            void OnDestroy()
            {
                audioSource.volume = endValue;
            }
        }


    }
}
User avatar
Tony Li
Posts: 21636
Joined: Thu Jul 18, 2013 1:27 pm

Re: Fade Audio Custom Sequencer Command

Post by Tony Li »

Hi,

Your Start() method was nested inside the Awake() method. Try this instead:

Code: Select all

using System.Collections;
using UnityEngine;

namespace PixelCrushers.DialogueSystem.SequencerCommands
{

    // Syntax: FadeAudio(AudioObjectName, endValue, fadeDuration)
    public class SequencerCommandFadeAudio : SequencerCommand
    {

        private AudioSource audioSource;
        private float endValue;

        IEnumerator Start()
        {
            // Get the values of the parameters:
            var AudioObject = GetSubject(0);
            endValue = GetParameterAsFloat(1);
            var fadeDuration = GetParameterAsFloat(2);

            if (AudioObject == null)
            {
                Debug.LogWarning("Sequencer Command FadeAudio(): Can't find GameObject '" + GetParameter(0) + "'");
            }
            else
            {
                audioSource = AudioObject.GetComponent<AudioSource>();
                if (audioSource == null)
                {
                    Debug.LogWarning("Sequencer Command FadeAudio(): GameObject '" + GetParameter(0) + "' doesn't have an Audio Source");
                }
                else
                {
                    var startingValue = audioSource.volume;
                    float elapsed = 0;
                    while (elapsed < fadeDuration)
                    {
                        float t = Mathf.Clamp01(elapsed / fadeDuration);
                        audioSource.volume = Mathf.Lerp(startingValue, endValue, t);
                        yield return null;
                        elapsed += DialogueTime.deltaTime;
                    }
                }
            }
            Stop();
        }

        void OnDestroy()
        {
            if (audioSource != null)
            {
                audioSource.volume = endValue;
            }
        }
    }
}
Keiji
Posts: 17
Joined: Tue Feb 18, 2020 2:29 pm

Re: Fade Audio Custom Sequencer Command

Post by Keiji »

Wow, this works perfectly! Awesome, thank you so much for the quick help, Tony!
User avatar
Tony Li
Posts: 21636
Joined: Thu Jul 18, 2013 1:27 pm

Re: Fade Audio Custom Sequencer Command

Post by Tony Li »

Glad to help!
Post Reply