Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Solved] Find all AudioMixerGroups

Discussion in 'Scripting' started by Son_Of_Diablo, Apr 23, 2015.

  1. Son_Of_Diablo

    Son_Of_Diablo

    Joined:
    Feb 22, 2013
    Posts:
    3
    Hello I'm having some problems with finding all the AudioMixerGroups in my unity project.

    I want to add a AudioMixerGroup to a AudioSource with my scripts.

    Am I wrong in assuming this would be the correct way of finding my desired AudioMixerGroup:

    Code (csharp):
    1. foreach (AudioMixerGroup AMG in FindObjectsOfType<AudioMixerGroup>())
    2.         {
    3.             if (AMG.name == "Sound")
    4.             {
    5.                 audioSource.outputAudioMixerGroup = AMG;
    6.                 break;
    7.             }
    8.         }  

    But this just results in nothing as it can't find any AudioMixerGroups :(
     
  2. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    I have some experience with Mixer Groups give me like 5-10 minutes I'll have a script together for you that shows how you can get what you are looking for.
     
    HenryChinaski likes this.
  3. Son_Of_Diablo

    Son_Of_Diablo

    Joined:
    Feb 22, 2013
    Posts:
    3
    Many thanks! I will wait in anticipation! :D
     
  4. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    Here's a sample script that will do what you are looking for. I had a similar problem to solve when I was working on my object pooling asset; it's available from the asset store link in my signature(shameless plug).

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Audio;
    4.  
    5. public class AudioExtension : MonoBehaviour {
    6.  
    7.     public AudioMixer audioMixer;
    8.  
    9.     public AudioMixerGroup[] AllMixerGroups {
    10.         get{
    11.             return audioMixer.FindMatchingGroups(string.Empty);
    12.         ;}
    13.     }
    14. }
    15.  
    Essentially what you want to do is create a public variable for the audio mixer you want to get groups from. Drag your AudioMixer onto that variable in the inspector and use the extension method to return all the mixer groups associated with that mixer. Simple as that! Good luck and I hope this helps you out!
     
  5. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    Also, if you are looking for a specific mixer group you can just replace the string.Empty with the string that represents the path to your mixer group. eg "Sound" ;)

    That's as easy as this:
    Code (CSharp):
    1.  audioSource.outputAudioMixerGroup = audioMixer.FindMatchingGroups("Sound")[0];
     
    HOPEGIVER and Son_Of_Diablo like this.
  6. Son_Of_Diablo

    Son_Of_Diablo

    Joined:
    Feb 22, 2013
    Posts:
    3
    Works perfectly! Many thanks!
     
  7. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    No worries. If you have any more questions you can get at me on Twitter or via my website. Links are in my signature. Best of luck in all your endeavours!
     
  8. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    I was stuck on this for a few minutes only to realize I was using AudioMixer.outputAudioMixerGroup instead of AudioSource.outputAudioMixerGroup.
     
  9. MrFuzzzy

    MrFuzzzy

    Joined:
    Feb 27, 2019
    Posts:
    13
    is there a way to find out what AudioMixerGroup is feeding into what other AudioMixerGroup?
     
  10. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,931
    I don't think it is possible in a C# script. I suspect what you see in the editor window is just a representation from the C++ side (FMod).
     
  11. MrFuzzzy

    MrFuzzzy

    Joined:
    Feb 27, 2019
    Posts:
    13