1
\$\begingroup\$

I'm making the main menu for a small game I'm making, and when I was learning how to make buttons etc.

In order to make the buttons have a sound effect when pressed and so on, I was told to add an audio source to each button and have a script on the button receive a reference to the audio file and play it. However, I am wondering what's the most efficient and "elegant" way to do it.

Is it ok to leave it in a "button manager" script that each button has, that also manages the appearance of the button, and have each button have an audio source, or should I make one object with an audio source, have a script for UI audio management and an event fired whenever player hovers or clicks any of the buttons and have that one audio source play the correct sound?

Will the latter create problems when a player hover quickly over several buttons?

Will it be better for when I eventually make a sound manager for sound preferences?

Also, will the same work for background music or will I need another audio source for that?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

AUDIO MANAGER

The best solution in my opinion would be to have an AudioManager in your scene. You can give it an AudioSource and and some sort of 'AudioManager' script.

The script can just contain a List of sound effects or something, and you can play each one using the AudioSource.PlayOneShot(audioClip) function. If you want a reference to make it easy to understand, heres my AudioManager that I use:

using UnityEngine;

public class AudioManager : MonoBehaviour
{
    // Reference to the AudioSource on the object.
    private AudioSource m_Source;

    // Reference to a SoundsAsset (a custom data type I made to store sounds for my game)
    public SoundsAsset soundsAsset;

    // Reference to the Instance of the Singleton -- https://stackoverflow.com/questions/2155688/what-is-a-singleton-in-c
    public static AudioManager Instance;

    void Awake() {
        // Singleton pattern.
        if (Instance != null && Instance == this) {
            Destroy(gameObject);
        }
        else Instance = this;
        m_Source = GetComponent<AudioSource>();
    }

    public void PlaySoundEffect(string soundName) {
        // If the sound name we entered is empty or null, return.
        if (soundName == null || soundName == string.Empty) return;
        switch (soundName)
        {
            // If we typed sound1, play the sound1 sound found in the soundsAsset, you can replace this with an audioclip and sound name of your choice.
            case "sound1":
                m_Source.PlayOneShot(soundsAsset.sound1);
                break;
            case "sound2":
                m_Source.PlayOneShot(soundsAsset.sound2);
                break;
        }
    }
}

This is a simplified version but hopefully it helps you understand, in the button's onClick function, you can select the audiomanager, and call the playsoundeffect() function along with the name of your sound. In this case I use a SoundsAsset to store all the sounds, but you could just use a list of AudioClips too.

MUSIC MANAGER

In my music manager, I create an audiosource for each audioclip put in, this is purely so that multiple music clips can fade and play at the same time. If you dont want that, you can just create a MusicManager, that simply has one audiosource, and create a MusicManager script that changes the music playing. Don't forget to use DontDestroyOnLoad, to make sure the Manager isn't destroyed when loading a new scene.

Hope this helps.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .