Documentation index for AI agents (llms.txt) A Markdown version of every page is available: request the page's source under /docs/, follow the "alternate" link in this page's head, or start from /llms.txt.
Visual Script logo Manual

Set Audio Channel Volume#

Audio / Volume / Set Audio Channel Volume

Description#

Changes the Volume of the specified channel

Parameters#

Name Description
Channel The Audio Channel to change the volume of, or Master for the global volume
Volume The new volume value

Code#

using System;
using UnityEngine;
using VisualScript.Icons;

namespace VisualScript.Runtime
{
    [Title("Set Audio Channel Volume")]
    [Image(typeof(IconVolume), ColorTheme.Type.Yellow)]

    [Description("Changes the Volume of the specified channel")]
    [Category("Audio/Volume/Channel")]

    [Parameter("Channel", "The Audio Channel to change the volume of, or Master for the global volume")]
    [Parameter("Volume", "The new volume value")]

    [Keywords("Set", "Change", "Modify")]

    [Serializable]
    public class InstructionAudioUnityVolume : Instruction
    {
        [SerializeField] private AudioVolume m_Channel = AudioVolume.Music;

        [SerializeField] private GetNumber m_Volume = new GetNumber(1f);
        [SerializeField] private TweenTransition m_Transition = new TweenTransition();

        [NonSerialized] private bool m_IsComplete;

        [NonSerialized] private Action<bool, float> m_CallOnUpdate;
        [NonSerialized] private Action m_CallOnFinish;
        [NonSerialized] private Func<bool> m_CallIsComplete;

        // PROPERTIES: ----------------------------------------------------------------------------

        private string TweenKey => this.m_Channel switch
        {
            AudioVolume.SoundEffect => "volume-sound-effect",
            AudioVolume.Ambient => "volume-ambient",
            AudioVolume.Music => "volume-music",
            AudioVolume.Voice => "volume-voice",
            AudioVolume.UI => "volume-ui",
            _ => "volume-master"
        };

        // METHODS: -------------------------------------------------------------------------------

        protected override string GetTitle(GameObject source)
        {
            string channel = TextUtils.Humanize(this.m_Channel);
            string result = $"{this.m_Volume}{this.m_Transition}";

            return $"Volume of {TextUtils.ThemeAsKeyword(channel)} = {result}";
        }

        protected override async Awaitable Run(Args args)
        {
            bool wait = this.m_Transition.Await;
            this.m_IsComplete = false;

            this.m_CallOnUpdate ??= this.OnUpdate;
            this.m_CallOnFinish ??= this.OnFinish;
            this.m_CallIsComplete ??= this.IsComplete;

            TweenManager.Run(new TweenStaticInput<float>(
                this.TweenKey,
                this.GetVolume(),
                this.m_Volume.GetFloat(args),
                this.m_CallOnUpdate,
                wait ? this.m_CallOnFinish : null,
                this.m_Transition.GetDuration(args),
                this.m_Transition.EaseMode,
                this.m_Transition.TimeMode
            ));

            if (wait) await this.Until(this.m_CallIsComplete);
        }

        private void OnUpdate(bool isAlive, float volume)
        {
            if (isAlive) this.SetVolume(volume);
        }

        private void OnFinish() => this.m_IsComplete = true;

        private bool IsComplete() => this.m_IsComplete;

        private float GetVolume()
        {
            return this.m_Channel == AudioVolume.Master
                ? VolumeManager.Master.Value
                : VolumeManager.Get((AudioChannel) this.m_Channel);
        }

        private void SetVolume(float volume)
        {
            if (this.m_Channel == AudioVolume.Master)
            {
                VolumeManager.Master.Value = volume;
                return;
            }

            VolumeManager.Set((AudioChannel) this.m_Channel, volume);
        }
    }
}
Visual Script logoVisual Script © Catsoft Works 2026. All rights reserved.