- Home /
Cleanest way to decouple Manager class with events
So here's my dilemma. I've been kicking around online many hours the last few weeks trying to figure out a solution to my problem.
I have an animation system and sound system in use throughout multiple scenes in my game which I instantiate when I need them. I'm naming the sound system class AudioManager in this instance. The AudioManager is one of the few singletons I have in my game.
I have scripts attached to different prefabbed game objects that I instantiate that make noise. These objects come and go so it's not like I can just click and drag these onto the AudioManager. I want to be able to just essentially broadcast an audio play event like PlayNoise("coinDrop") in an ignorant way from anywhere in these scripts and have AudioManager listen for it and handle the fadein/fadeout and intricacies. I could do that easily by just referencing the audiomanager as a singleton and calling AudioManager.Instance.PlayNoise("blah") but the one improvement I'd like to make is that I'd like to be able to totally remove the AudioManager from the entire project and still have the game work without having to change any code or recompile anything. I don't really want to have a bunch of delegate subscribers and list a bunch of functions either. Just a simple event broadcast out.
The purpose for this is that I'm looking for more modularity. I've done the drag and link with OnClick and unity events and it works great for in game scenes but works like garbage when you have an intricate UI system and you need to be able to prefab everything.
TL:DR; I want to announce an event from multiple classes/gameobjs without having to know anything at all about the listener (e.g. no delegate subscribers in every class I need to make a sound from). The listener knows nothing about the announcers; it just knows to do something when it hears an event. I've heard of SendMessage but I've heard negative things about it with regard to efficiency and it sounds like some protocode from when Unity was new.
Thanks!!
Answer by juicyz · Jan 19, 2017 at 12:52 AM
Something like https://unity3d.com/learn/tutorials/topics/scripting/events-creating-simple-messaging-system?playlist=17117 ?
So this isn't exactly what I was wanting but I pretty much realized the only way without listeners and subscribers in the manager class was to have it search the entire scene like Send$$anonymous$$essage does (inefficient) so I went ahead and implemented what the tutorial described. I made the Event$$anonymous$$anager a true singleton and wrote a generic function that accepts multiple things and turns them into strings. IT seems to work really well.
I saw that tutorial before but it wasn't exactly what I was looking for so I kind of skimmed the code and never took the time to understand it and watch the vid. The Unity3d tut vids are actually quite good these days.
Your answer
Follow this Question
Related Questions
is there any easier way to do this ? 2 Answers
Delegate an event 2 Answers
What part of the delegate gets passed back?The return type? 1 Answer