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>();
}
}
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