Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Attach Mixer to Audio Source via script

using System;
using UnityEngine.Audio;
using UnityEngine;

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

    public static AudioManager instance;

    // use for initialization
    private void Awake()
    {
        // to avoid multiple audio manager in some scene
        // and we don't want music to restart every time we change scene.
        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.outputAudioMixerGroup = s.mixer;

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

    private void Start()
    {
        Play("background");
    }

    public void Play (string name)
    {
        //FindObjectOfType<AudioManager>().Play("name");

        Sound s = Array.Find(sounds, sound => sound.name == name);

        if (s == null)
        {
            Debug.LogError("Sound: " + name + "not found!");
            return;
        }
        s.source.Play();
    }
}


// heres the other public Sound code
using UnityEngine.Audio;
using UnityEngine;

[System.Serializable]
public class Sound
{
    public string name;

    public AudioClip clip;

    public AudioMixerGroup mixer;

    [Range(0f, 1f)]
    public float volume;
    [Range(.1f, 3f)]
    public float pitch;

    public bool loop;

    [HideInInspector]
    public AudioSource source;
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: remove focus from button unity 
Csharp :: unity how to end a game with esc 
Csharp :: remove whitespace between string c# 
Csharp :: how to change scenes in unity 
Csharp :: c# unity camera follow 
Csharp :: new parameterized thread c# 
Csharp :: bluestacks unity black screen 
Csharp :: c# checksum 
Csharp :: how to convert a number to 2 decimal places in c# 
Csharp :: if string contains number c# 
Csharp :: c sharp int to string 
Csharp :: regex replace all special characters 
Csharp :: Create gaps / headers between variables in the unity inspector 
Csharp :: c# dynamic object get value 
Csharp :: making a list of chars in c# 
Csharp :: unity c# set object tag 
Csharp :: c# datetimepicker set weeks before today 
Csharp :: Unity asset storre download forlder 
Csharp :: c# string to enum 
Csharp :: c# create file 
Csharp :: how to stop rigidbody2d from falling in unity 
Csharp :: c# delay 
Csharp :: small modal popup bootstrap 
Csharp :: c# func with no return 
Csharp :: c# list sort by property string 
Csharp :: how to reference a child object unity 
Csharp :: c# string default value 
Csharp :: how do I print something in the console at the start of the game unity 
Csharp :: c# create dynamic object 
Csharp :: c# filter list 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =