As per the title, how are people handling message pub/sub in unity for events? By that I mean a mediator pattern type setup?
I’ve looked into the Unity tutorial to create a simple messaging system, but when using the UnityEvent class, the Invoke() can’t take any parameters. This massively limits what you can do with the messages. I’ve read through dozens of threads of people trying to solve this, but it never seems anyone came up with a great solution. However most of these threads are 4+ years old so I thought I would ask if there’s any new development in this area.
The second part of my question is, if the built in UnityEvent system can’t handle parameters (and according to this thread is slower than just simply wiring up delegates manually), how is everyone else handling this? I myself have been using the CSharp messenger for about 6 years, but I doubt many people even know that exists. Considering it’s pretty damn difficult to build a half decent interface/game without using some kind of messaging system, what are you guys doing to solve this?
I would like to update my knowledge of the tools in this area, but the research has mostly just found outdated articles.
In the few more complicated cases I’ve just written my own quick and dirty pub/sub system. For example here’s one I wrote: https://hastebin.com/navahasuha.csharp
I’ve seen that before and it’s always seemed a bit strange to me. What seems odd you have to make an individual extensions of UnityEvent for every message type in your system, as well as specifying a possible class to encapsulate your command/event.
For example if you had an event you wanted to publish a change event which was a struct with a few properties like:
public struct PlayerScoreChanged {
public int PlayerNumber {get; set;}
public int PlayerScore {get; set; }
}
Plus the script which actually wants to publish the change needs to know that PlayerScoreChangedEvent has to only be used for the ‘PlayerScoreChanged’ message, so that you can do:
Maybe there’s an easier way than the docs imply, but it seems…clunky (at least compared to something like the CSharp messenger or just standard C# Actions).
Just for a bit of context around this question, the reason I want to figure this out is that a lot of the online projects I look at don’t use any event messaging. I’m trying to understand if there’s a really good flexible implementation that Unity has which people don’t know about, or if people know about it but just cant grok how to use it. So I’m trying to understand the Unity implementation fully.
That’s a nice implementation. I suspect most people are using their own little scripts like that. If I had to guess, a lot of new developers to Unity probably hear about pub/sub messaging, look into the Unity docs and never fully understand how to use it beyond simple triggers.
But yeah, it seems like something that should be a bit more standard ‘drop in boilderplate code’ for most projects (similar to how MediaR in the dotnet world has become the defacto standard for quick pub/sub).
Are most people using messaging in their projects, but everyone has their own homebrew setups? Or is there a massive collection of devs out there just not using pub/sub at all? There’s probably not enough data for this in a forum thread, but I do wonder.
The way I use it is a little more generic, I have events for single properties instead of entire objects. So i’ll usually define an IntEvent and a BoolEvent, and then use them as such:
public UnityIntEvent OnHealthChanged;
OnHealthChanged?.Invoke(health);
Also, I only use UnityEvents when I want to hook them up in the inspector. For other usecases Actions are my way to go