- Home /
Question regarding events and event managers.
Hi,
First of all, I've done a good deal of Googling and Youtubing before asking this.
I know how to use delegate to define a signature for my events and how to define an event with said delegate and how to make other scripts to subscribe to these events.
But in all of the videos or blog posts that I found teaching this, they all were firing the event from the event manager but I want it for some other object to fire that event.
For example, say you have an SFX manager. It knows how to playback some sound effects based on some events. Let's imagine that there is a particle manager that does particle on certain events. Both these managers have interest in "GotCorrectObject" event.
So I need to raise that "GotCorrectObject" event for both of those managers so they can act accordingly. How can I do that? I do not want to raise event from inside of those event managers but raise them from outside.
And please do not forward me to one of those fat event managers, I'm aiming to learn and something simple.
Thanks.
This is not one of those fat event managers, more a GetComponent/Event comparative with just few lines. $$anonymous$$aybe that will help:
Answer by Ashkan_gc · Apr 17, 2014 at 09:21 PM
You should simply have a higher level event manager which raises events which are high level game specific events. Let's say you have fire shot event for the enemy. SFX system and particle system are both interested in the event. You should register the SFX.PlayShotEffect event and particleSystem.CreateShotParticles events to be called when FireShot is raised. This is called event chaining. Messaging systems are ideal for this and similar situations.
Answer by matheuslr · Jun 07, 2014 at 01:46 PM
Hey, I too had some troubles trying to find the best solution for event management in Unity. So far, I wasn't able to find a proper one that satisfied my needs. So I decided to roll my own EventManager. It's a simple but effective solution to events in Unity.
I hope you don't get it as another Fat Event Manager. All I want is some feedback on the usage :)
Since it's open source, you can learn the things under the hood!
Please, check the code out at https://bitbucket.org/matheuslessarodrigues/connectr/ I'm looking for feedbacks :)
I was looking for a way to get rid of that nasty "SendMessage()". Also, I tried to keep it really simple and unobtrusive as you don't require to define classes or implement some IEvent interface. Just mark your methods as an EventHandler (with C# attributes) and trigger events with a static method call. Like this:
// Define the event with the method signature delegate void MyEvent( int, string, float );
// Listens to "MyEvent" [Handlr( typeof( MyEvent ) )] void MyEventHandlerMethod( int some, string random, float args ) { // Do your magic here :) }
// Then rais events with Connectr.Trigger( 42, "The event params", 0.5f );
Also, the subscription to events is handled by unity components automatically!
What do you think?
Your answer
![](https://koobas.hobune.stream/wayback/20220613142059im_/https://answers.unity.com/themes/thub/images/avi.jpg)