You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity新手求助:无法添加AudioMixer至AudioManager实现音量滑块控制

Unity音量控制问题:找不到AudioMixer赋值选项

我是Unity新手,还请多多包涵。我创建了名为MainMixer的AudioMixer和volumeSlider,希望通过拖动菜单中的滑块控制游戏所有音效的音量大小。我已编写脚本并挂载到对应的GameObject上,但找不到可添加该AudioMixer的“Mixer”选项位置。

代码部分

AudioManager.cs

using UnityEngine;
using System;
using UnityEngine.Audio;

public class AudioManager : MonoBehaviour
{
    public Sound[] sounds;

    public static AudioManager Instance;

    public void Awake()
    {
        if(Instance == null)
        {
            Instance = this;
        }
        else
        {
            Destroy(gameObject);
            return;
        }


        DontDestroyOnLoad(gameObject);
        foreach (Sound s in sounds)
        {
            s.source =  gameObject.AddComponent<AudioSource>();
            s.source.clip = s.clip;


            s.source.volume = s.volume;
            s.source.pitch = s.pitch;
            s.source.loop = s.loop;
        }
    }

    private void Start()
    {
        //Play("Theme");
        Play("WindTheme");

    }


    public void Play(string name)
    {
        Sound s =  Array.Find(sounds, Sound => Sound.name == name);

        if (s == null)
        {
            Debug.Log("Sorry sound with name not found");
            return;
        }

        s.source.Play();
    }

    public void Stop(string name)
    {
        Sound s = Array.Find(sounds, Sound => Sound.name == name);

        if (s == null)
        {
            Debug.Log("Sorry sound with name not found");
            return;
        }

        s.source.Stop();

        // we are here to do a
    }

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

public class MenuEvents : MonoBehaviour
{
    public Slider volumeSlider;
    public AudioMixer mixer;

    public void SetVolume()
    {

        mixer.SetFloat("volume", volumeSlider.value);


    }

}

相关截图

  • 音量滑块组件:
    音量滑块
  • Canvas对象结构:
    Canvas
  • AudioManager挂载面板:
    AudioManager

解决方法

  1. 定位MenuEvents挂载对象:找到你挂载MenuEvents脚本的GameObject(比如截图里的Canvas),选中该对象后在Inspector面板里找到这个脚本组件。
  2. 赋值AudioMixer:在MenuEvents组件的Inspector界面中,找到Mixer公开字段,直接从Project面板把MainMixer拖入该字段的赋值框即可。
  3. 关联音频源到Mixer(关键):当前AudioManager创建的AudioSource未关联Mixer,滑块调节不会生效,需修改代码:
    • 在AudioManager类中添加public AudioMixer mixer;字段。
    • 在Awake方法的循环内,给每个AudioSource添加输出到Mixer的代码:
      s.source.outputAudioMixerGroup = mixer.FindMatchingGroups("Master")[0];
      
      之后同样给AudioManager的mixer字段赋值MainMixer。
  4. 检查Mixer暴露参数:打开MainMixer,右键目标音量参数,选择「Expose 'volume' to script」,确保参数名称与代码中的"volume"完全一致,否则SetFloat会执行失败。

内容的提问来源于stack exchange,提问作者Davinci

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 20:08:32