Forum rules - please read before posting.

Audio Master: Can't create variable

Goog afternoon everyone,

Sound settings is definitely not my cup of tea. My friend created a Master mixer and as children: music, SFX and Speech.
I followed this tutorial: https://adventurecreator.org/tutorials/working-audio-mixers
but the problem is that he wants 4 sliders: one for each type of sound. Now, for SFX, speech and music, all cool because they are default ones, but "how can I create one slider for the Master?".
I have created all 4 exposed parameters but I can't get to have the Master to appear under the Options settings in Menu.
What we are aiming for is volume control and the one missing would be the Master to show as a slider under options.

Please let me know if I'm not clear.
Unity 2020.3.27 and latest AC.
Thanks a lot

Comments

  • It'd be a case of linking the parameter - through script - to a Global Float Variable via Options Data, so that it can then be controlled through a Slider element.

    The process of linking custom options data to variables is covered in this tutorial.

    Attenuation parameters don't use the same scale as a 0 -> 1 volume slider, but you can run the conversion and update a given mixer automatically using the AdvGame.SetMixerVolume function.

  • Hello,
    Thnak you very much. After a few days of work I made it work but not the tutorial way. This is what I did:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Audio;
    using AC;
    using UnityEngine.UI;

    public class AudioMaster : MonoBehaviour
    {
    private GVar masterVolumeVar;
    public float masterVolume;
    //public string masterVolumeString;
    public AudioMixer mixer;
    //public Slider theSlider;

    void Start()
    {
        masterVolumeVar = GlobalVariables.GetVariable(28); //Variable 28: Audio Master Volume
        masterVolume = masterVolumeVar.FloatValue;
        //theSlider = GetComponent<Slider>();
    }
    
    void Update()
    {
        masterVolume = masterVolumeVar.FloatValue;
        //masterVolume = theSlider.value;
        mixer.SetFloat("masterVolume", masterVolume);
        //AdvGame.SetMixerVolume(masterVolumeString, masterVolume);
    }
    
    public void DefaultMaster()
    {
        masterVolumeVar.FloatValue = -10f;
    }
    

    So, this comes to a GameObject (prefab) on screen. The "mixer" parameter is the main mixer we have in Unity. As for the menus and Variable, our custom float variable is 28 as you can see. Then, linked it to options and linked the slider to the menu. Nothing else.
    I started doing the tutorial way but I found that it never ran from start. This is the script I did for this.

    using UnityEngine;
    using System.Collections.Generic;
    using UnityEngine.Audio;
    using UnityEditor;
    using AC;

    namespace AC
    {
    [System.Serializable]
    public class AudioMasterTemplate : Action
    {
    // Declare properties here
    public override ActionCategory Category { get { return ActionCategory.Engine; }}
    public override string Title { get { return "Master volume"; }}
    public override string Description { get { return "This is a Master volume."; }}

        // Declare variables here
        public AudioMixer audioMixer;
        public float masterVolume;
    
        public void Start()
        {
            float masterVolume = GlobalVariables.GetFloatValue(27);
        }
    
        public void Update()
        {
            Debug.Log(masterVolume);
        }
        public override float Run ()
        {
            audioMixer.SetFloat("masterVolume", masterVolume);
            return 0f;
        }
    
    
        public override void Skip ()
        {
            /*
             * This function is called when the Action is skipped, as a
             * result of the player invoking the "EndCutscene" input.
             * 
             * It should perform the instructions of the Action instantly -
             * regardless of whether or not the Action itself has been run
             * normally yet.  If this method is left blank, then skipping
             * the Action will have no effect.  If this method is removed,
             * or if the Run() method call is left below, then skipping the
             * Action will cause it to run itself as normal.
             */
            Run ();
        }
    
        #if UNITY_EDITOR
    
        public override void ShowGUI ()
        {
            audioMixer = (AudioMixer)EditorGUILayout.ObjectField("Master:", audioMixer, typeof(AudioMixer), false);
        }
    
        public override string SetLabel ()
        {
            // (Optional) Return a string used to describe the specific action's job.
            return string.Empty;
        }
    
        #endif
    }
    

    }

    I'm perry sure it's super wrong. I can see the Engine.Master Volume but does nothing when called. I has nothing but the option to choose the mixer. There has to be something wrong because the slider and variable work but it does nothing when calling it from start (sorry, touching new grounds with this topic).

    First I want to understand where I'm doing wrong. Maybe I can get rid of the gameObject on screen?

    Secondly: I honestly didn't understand this AC.AdvGame.SetMixerVolume. What should be the parameters and where should it go?

    And third: I found that AC default SFX, music and speech options go from 0 to 1 when "changing volume" but my friend is all about attenuation. Any way I can see what attenuation is eaqul to in terms of the 0 to 1 float? (I don't even know where I'm going with this question hahaha).

    Thanks a lot.

  • Action scripts don't derive from Unity's MonoBehaviour - Start and Update won't be called automatically in the same way. An Action is only run when called in an ActionList, and the code that affects the scene should be placed in the Run function:

    using UnityEngine;
    using UnityEngine.Audio;
    using AC;
    
    namespace AC
    {
    
        [System.Serializable]
        public class AudioMasterTemplate : Action
        {
    
            public override ActionCategory Category { get { return ActionCategory.Engine; }}
            public override string Title { get { return "Master volume"; }}
            public override string Description { get { return "This is a Master volume."; }}
    
            public AudioMixer audioMixer;
            public float masterVolume;
    
            public override float Run ()
            {
                float masterVolume = GlobalVariables.GetFloatValue(27);
                audioMixer.SetFloat ("masterVolume", masterVolume);
                return 0f;
            }
    
            #if UNITY_EDITOR
    
            public override void ShowGUI ()
            {
                audioMixer = (AudioMixer)UnityEditor.EditorGUILayout.ObjectField ("Master:", audioMixer, typeof(AudioMixer), false);
            }
    
            #endif
    
        }
    
    }
    

    To have an Action run when your game starts, place it in your "ActionList on start game" asset, assigned in the Settings Manager:

    I honestly didn't understand this AC.AdvGame.SetMixerVolume. What should be the parameters and where should it go?

    SetMixerVolume automatically converts a 0 -> 1 volume slider scale into an attenuation value. If your float variable uses this scale, replace your audioMixer.SetFloat with:

    AdvGame.SetMixerVolume (audioMixer, "masterVolume", masterVolume);
    

    Any way I can see what attenuation is eaqul to in terms of the 0 to 1 float?

    The conversion can be found inside the SetMixerVolume function:

    float attenuation = (volume > 0f) ? (Mathf.Log10 (volume) * 20f) : -80f;
    
  • Thank you very much. Now it's more clear. Just one thought: do you know our way of doing it can kill performance is not good even if it worked? It's just to read an opinion because for us, we did brightness exactly the same way and so far, it works. The only thing is: you need to have a prefab gameobject present in all scenes.

    Thanks a lot!

  • Forgot to ask: those last two lines: where should they go? In the AL script? In an object present in scenes? Update() or method?

    Thanks again.

  • The custom Action avoids the need for a prefab / script in the scene.

    Forgot to ask: those last two lines: where should they go?

    The SetMixerVolume call, if you want to use it, should replace the mixer's SetFloat call in the Action's Run function.

    The conversion line was to answer your question of how to get the attenuation from a 0 -> 1 scale - it's not necessary for the Action to work.

  • Thank you so much. Hope this script serves good for future adventurers :)

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.