0
\$\begingroup\$

I am trying to make a audio manager. I want this audio manager to setup itself and be accessible from everywhere. So I made a static class that creates the audio sources when you need them. Is this bad design?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NodeEditorFramework.Utilities;

public static class AudioManager {

    private static bool musicIsSetup;
    private static AudioSource musicSource;
    private static bool sfxIsSetup;
    private static AudioSource sfxSource;

    public static void ManualSetup()
    {
        SetupSFXSystem();
        SetupMusicSystem();
    }

    public static void PlaySFX(AudioClip clip)
    {
        if (!sfxIsSetup) SetupSFXSystem();
        sfxSource.PlayOneShot(clip);
    }

    public static void PlayMusic(AudioClip clip)
    {
        if (!musicIsSetup) SetupMusicSystem();
        musicSource.clip = clip;
        if (!musicSource.isPlaying) musicSource.Play();
    }

    private static void SetupMusicSystem()
    {
        musicIsSetup = true;
        GameObject MusicHandler = new GameObject("MusicHandler");
        musicSource = MusicHandler.AddComponent<AudioSource>();
    }

    private static void SetupSFXSystem()
    {
        sfxIsSetup = true;
        GameObject SFXHandler = new GameObject("SFXHandler");
        sfxSource = SFXHandler.AddComponent<AudioSource>();
    }
}
\$\endgroup\$
2
  • 4
    \$\begingroup\$ If you have code that works correctly for your needs, and would like open-ended feedback on architecture decisions and coding style, the Code Review StackExchange may be a more appropriate place to ask. Here on GameDev, we're more focused on problem-solving, so we'd want to know: have you identified a specific issue with this approach? Eg. is it behaving badly, exhibiting undesired performance effects, cumbersome to use, or failing to scale to features you want to add? Zeroing in on these issues helps get constructive answers. Else, "if it ain't broke…" \$\endgroup\$ – DMGregory Oct 3 '18 at 14:11
  • \$\begingroup\$ To put it simply, yes, it's a bad design in a way that you can't change the values/settings for AudioSource in inspector. Also, at some point, if the game is complicated enough, you will need different settings for different scenes/actions in game which are easier to change in inspector and also doesn't require game designer to go to code to change some of the settings. If you want to access it anywhere in your scripts, consider using Singleton pattern. There are other factors and reasons of course. \$\endgroup\$ – Candid Moon _Max_ Oct 3 '18 at 19:45
2
\$\begingroup\$

The main limitation I see with this class is that it can not handle positional audio. It creates two audio sources at unspecified coordinates, which means they will get spawned at 0:0:0. The audio listener in the scene, however (unless you change that) is attached to the main camera. So depending on the position of the camera, sound effects will become louder, quieter and change their stereo panning.

If all the sound effects you play through this class are meant to be non-diegetic (the sound is made by the user interface, not a particular object in the game world), then you should make the audio handlers children of the main camera (or whichever objects has your audio listener). If you want a sound effect to be diegetic, then they should be played through an audio source attached to the object which makes the sound.

If your main intention of the class is that you don't want to deal with audio sources, then it might be useful to add a method PlaySFX(AudioClip clip, GameObject source). The method should check if the source already has an AudioSource component, and when it has not, it adds one to it. It then plays the clip through the AudioSource of the game object. Your standard PlaySFX(AudioClip clip) method could then just call PlaySFX(clip, Camera.current.gameObject); to play a non-diegetic sound effect.

Another useful overload could be PlaySFX(AudioClip clip, Vector3 position) which creates an ephemeral game object with an AudioSource component at the given world-position which plays the clip and then self-destructs.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.