Unity Community


Results 1 to 20 of 20

  1. Location
    Ukraine, Kiev
    Posts
    418

    Comparison of Unity(C#) Messaging / Events Systems

    Flash Like Event System
    https://www.assetstore.unity3d.com/#/content/7067

    How add listener
    Code:  
    1. MyButton btn = gameobject.addComponent<MyButton>();  // creating some simple button
    2. btn.addEventListner(BaseEvent.CLICK, onButtonClick);  // listening event

    How to dispatch event
    Code:  
    1. //MyButton.cs
    2.  
    3. void OnGUI() {
    4.     if(GUI.Button(buttonRect, "click me")) {
    5.         dispatch(BaseEvent.CLICK, "hello");  //second param is optional and can be any object.
    6.     }
    7. }

    Speed result on 100 000 events - 85ms



    Standar Unity Send Message
    http://docs.unity3d.com/Documentatio...ndMessage.html

    Speed result on 100 000 events - 100ms



    CSharp Event Manager
    http://wiki.unity3d.com/index.php/CSharpEventManager

    How add listener
    Code:  
    1. EventManager.instance.AddListener(this as IEventListener, "TestEvent");

    How to dispatch event
    Code:  
    1. EventManager.instance. TriggerEvent(new TestEvent());

    Speed result on 100 000 events - 145ms



    Advanced CSharp Messenger
    http://wiki.unity3d.com/index.php/Ad...harp_Messenger

    How add listener
    Code:  
    1. Messenger.AddListener("start game", StartGame);

    How to dispatch event
    Code:  
    1. Messenger.Broadcast("start game");

    Speed result on 100 000 events - 45ms



    CSharp Notification Center
    http://wiki.unity3d.com/index.php/CS...ficationCenter

    How add listener
    Code:  
    1. NotificationCenter.DefaultCenter.AddObserver(this, "OnBumperCollision");

    How to dispatch event
    Code:  
    1. NotificationCenter.DefaultCenter.PostNotification(this, "OnBumperCollision");

    Speed result on 100 000 events - 227ms



    Сonclusion: this is only my opinion, but I like AS3 event system, so "Flash Like Event System" is most comfortable to use and it's pretty fast.

    If You need free script, you should choose "Advanced CSharp Messenger", usage is almost like other's event systems, it steel static, but it's fastest.
    Last edited by lacost; 03-10-2013 at 11:01 PM.


  2. Location
    Germany
    Posts
    906
    if you like you can also add edriven (http://forum.unity3d.com/threads/768...Unity-released) in your comparison. its also free.


  3. Location
    Ukraine, Kiev
    Posts
    418
    Thx, I will add it.
    By the way if this post was helpful, let me know, and I will do much more detailed description with code examples.


  4. Location
    Germany
    Posts
    906
    i found it very helpfull as i always wondered about the runtime performance of each of this systems. but i was to lazy to test it and decided for advanced C# Messenger for convenience reasons. not the worst choice as it seems. but i have modified it alot and call it via enum instead of a string to allow obfuscating and prevent silly typo errors with strings.

    i wonder why your send message test is that fast. from what i read it should be far slowest solution (reflection). maybe you have only one script with one method attached? there had been a discussion about this some time ago but i have no link at hand.

    generally the measurements are only a rough indicator for execution speed as it depends on cicumstances (number of events, number of subscribers etc) and maybe even platform. retrieving an object from a generic dictionary is close to a O(1) operation relatively independant from the count (given the hash function matches the size and there are no/few collisions). so it is a good data structure for storing the events.

    i have never worked with actionscript and thus your flash like event system looks not very familiar to me. also it requires unity-components and is only available for gameobjects. most of my classes are non-monobehaviors and when i can't send them notifications the system as well as sendmessage is useless for me.

    but thanks for your efforts anyway as many people may wonder about performance and (dis)advantages of these systems in general. maybe it would be usefull when you create a wikipage and add your findings there and link to the systems. so people have a better overview than in a forum thread as it seems the forum search is not available to many people for some reason (or they are simply to lazy). who looks in the wiki is already willing to search what he/she needs.


  5. Location
    Ukraine, Kiev
    Posts
    418
    i wonder why your send message test is that fast. from what i read it should be far slowest solution (reflection). maybe you have only one script with one method attached?
    This was kind of a light testing. Every system was tested with only one listener and one dispatcher. So "Send Message" was tested with only one script with few methods.

    But you write, I will do new test and add result's with "loaded system", when we have a lot of listeners.

    i have never worked with actionscript and thus your flash like event system looks not very familiar to me.
    Yes it has total different events ideology, that's why it not static or siglethon like others.

    also it requires unity-components and is only available for gameobjects
    No, it's not. It can work in both ways. Point is, that it has two class (EventDispatcher -> MonoBehavior) - for gameobjects, and
    (EventDispatcherBase - > Object) - for others.

    maybe it would be usefull when you create a wikipage and add your findings there
    thx, this is good idea. I will do that as soon as I will finish "loaded" test's and add few more systems.


  6. Posts
    109
    Hey @lacost, thanks a lot for sharing your benchmarks - Have you considered this http://www.willrmiller.com/?p=87 ? - I wonder where it would sit in your scale - as I found it interesting.


  7. Posts
    1
    Here's a simple one that mimicks Unity's SendMessage but has Java-like events objects.

    http://blog.sebaslab.com/whats-wrong...o-do-about-it/


  8. Posts
    3,788
    Some of those results are pretty surprising, though it should be noted that the different systems may have different characteristics in different usage scenarios.

    Also, what about a delegate based system? You probably won't find a pre-written one because there's actually nothing to pre-write. You just define a public delegate somewhere as your event notification function, and anything that cares about that event adds a matching internal function to the delegate.

    You'll also want to look at things like whether firing messages causes allocation.

    But also also... it's worth pointing out that you really need to think about whether performance of your event system matters in your use case. How often are events fired? I have a game which has a crap event system and a lot of spaghetti-like usage but, due to when events are fired, it makes no difference to the game's performance. (In that system there is no filtering of events, and events are reference objects and therefore caused allocation. So a typical event caused some allocation and then called dozens of functions that weren't relevant to it. "Optimise event system" is something that's been on our to-do list for ages but we've never got to it because it literally makes zero practical difference in our case.)
    Last edited by angrypenguin; 12-15-2013 at 06:27 PM.
    Hobby project: Master Thief


  9. Location
    Fort Collins, Colorado
    Posts
    234
    I think the irate waterfowl has a point. I looked at these above (thanks to this very helpful thread) a few weeks ago, and ended up not using any of them. Instead I implemented my own which has a few features I really wanted: (1) broadcasters and receivers can come and go at any time, with no registration/unregistration step; (2) messages could be scoped either globally, or to the local GameObject, or to that GameObject and its contained objects (recursively); (3) messages could be referenced by name, making it easy to use them as triggers configured in the Unity editor; (4) no magic-named methods that's going to cause stuff to silently break just because of apparently-innocent refactorings; (5) no strong references causing objects to leak if you forget to clean them up.

    I achieved all these aims, but as a result, the performance on a per-message basis is a bit poor. Probably worse than any of the above, in fact. But the way we're using them, I really don't care; they don't have any noticeable effect on the framerate. I love how cleanly they separate concerns in the code in just the ways I need them separated, and I'm more than willing to pay a small performance penalty for that.

    Of course, different strokes for different folks, YMMV, never eat spinach with a stranger, etc.


    Strout & Sons
    creating Unity games with my boys since 2013


  10. Location
    Omaha, NE
    Posts
    1,127
    Interesting. My first Unity test project was a messaging system much like JoeStrout's above. It did use events but handled it in an interesting way. It was a generic system with a message manager. I created MonoBehaviors to act as subscribers to the messages. You could create your own classes and subscribe / handle messages... or you could use the Behavior helpers which would handle receiving messages for you. You could control the max number of messages, how many were processed at a time and how often it processed them (or process manually) and the messages were delivered and stored in a Queue<T>

    The entire system is generic and allows you to create subscriptions for different message types. If you subscribe like so:

    Code:  
    1. gameObject.SubscribeToMessage<Vector3>("ImHere");

    It's an extension method that sets up the proper subscribers for you and adds the subscription. Now whenever someone calls:

    Code:  
    1. MessageBusManager.SendMessage<Vector3>("ImHere", transform.position);

    That message gets sent out to all of the subscribers. You can handle the messages yourself OR you can setup events on your game objects and have the subscription process and send them for you. Also, the Subscriptions are stored as a WeakReference<T> and when they are garbage collected they are automatically purged from the subscription manager.

    Now this was my first Unity test so it needs a lot of work. I was leveraging some foreach loops because I have message subscriptions stored in a Dictionary and it generates garbage like mad every frame. It's something I'm going to go back and develop on eventually and implement my own custom collections and indexers so I don't deal with the per-frame garbage generation.

    Here's the original documentation for what I had started:
    http://www.parentelement.com/documentation/messaging/
    Last edited by Dustin Horne; 12-16-2013 at 10:18 AM.
    JSON .NET for Unity is now only $20! - I wanted to make sure it was more affordable to all. JSON .NET for Unity is now only $20.

    JSON .NET For Unity
    Tested on iOS, PC / Mac, Android, Web Player, Windows Phone and Windows Store Apps
    Includes IOS and WebPlayer Support

    My Blog
    Unity, MVC, C#

    My Unity Site


  11. Posts
    8
    In this thread they uploaded an upgraded version of Advanced C# Messanger:

    http://forum.unity3d.com/threads/112...essenger/page2


  12. Posts
    4
    Quote Originally Posted by exiguous View Post
    ...but i have modified it alot and call it via enum instead of a string to allow obfuscating and prevent silly typo errors with strings
    Such errors could be prevented by using constants, not string literals directly.
    Just testet the Advanced CSharp Messenger's performance with enum keys and results were not ones I expected (results per 100k broadcasts):
    - using string keys: 0.021 to 0.028 sec
    - using enum keys: 0.06 to 1.1 sec

    EDIT: Tested EventSystem from this thread http://forum.unity3d.com/threads/112...essenger/page2. Performance per 100k broadcasts is 0.002 to 0.003 sec.
    Last edited by demminik; 02-11-2014 at 02:23 AM.


  13. Location
    Germany
    Posts
    906
    Quote Originally Posted by demminik
    Such errors could be prevented by using constants, not string literals directly.
    sure it could. but none of the messenger/notification systems i'm aware of utilizes them. and those constants also have to remembered. when using an enum all possible values are "collected" at a central place. also you can iterate over the values of an enum, switch them etc.. so you gain alot functionality and "convenience" what you don't really have with strings or other "hacks". if you ask me i think thats what enums are made for so why intentionally ignore them?

    Quote Originally Posted by demminik
    Just testet the Advanced CSharp Messenger's performance with enum keys and results were not ones I expected
    as enums are value types there is a little annoying discrepancy when using them as dictionary keys as they are boxed. so make sure you use an appropriate equality comparer to unleash their performance power. in my tests they have been faster than int keys with a custom comparer (have not tested strings).

    edit: the manual comparer has been 10 to 15 times faster than the default comparer! the both suggested automatic/generic solutions have been half as fast.
    Last edited by exiguous; 02-11-2014 at 05:21 AM.


  14. Location
    Quebec, Canada
    Posts
    677
    I'm wondering about the exact step used for testing those systems.

    1 listener? What does the receiving method does? Since it's single-threaded, what the receiving method does has a huge impact on the performance.

    Also depends of the computer... At work - on my crappy computer - I get 24ms on our own event system for 100,000 dispatch over 1 listener with an empty callback method..
    Last edited by LightStriker; 02-11-2014 at 07:05 AM.


  15. Location
    Germany
    Posts
    906
    Quote Originally Posted by LightStriker
    I'm wondering about the exact step used for testing those systems.
    if you refer to my "tests" with this statement i was a bit unclear. i just wanted to verify the performance impact of the "crappy" default comparer described in the linked site. so my test was not the whole messaging system but simply different dictionary key setups. and i could confirm that enum keys with default comparer is a bad idea and that there should be a custom comparer specified to avoid this issue.
    when you use such a messaging system i think its not for performance reasons but for convenience or "necissity". and for usual usecases (below 1000 anything) i think there should not be a performance issue. so if it is too slow for your taste can you replace it at all?
    so to clarify again: demminik "complained" about enum dictionary keys beeing slow, i told him why and proposed an easy method to improve it. my statement was NOT regarding overall performance of such event systems. and i'm still convinced that enums as keys are the best you can get. better than strings in any case for the mentioned reasons.


  16. Location
    Quebec, Canada
    Posts
    677
    I meant the first post of this thread.


  17. Posts
    4
    Quote Originally Posted by exiguous View Post
    so make sure you use an appropriate equality comparer to unleash their performance power. in my tests they have been faster than int keys with a custom comparer (have not tested strings).
    Ahh, I see now. Thx for the link, was very useful to me!


  18. Posts
    910
    @Dustin,

    Your Event system looks great. I was searching for a flexible and dynamic event system.
    I would love to have a copy of the project.

    Cheers.


  19. Location
    Omaha, NE
    Posts
    1,127
    Sure! I'll see if I can get it packaged up for you. It needs a lot of work. Now that I've learned a lot more about Unity I realize it needs some optimization. The event dispatching works very well but it's enumerating a dictionary which allocates 30k every frame. I'm thinking about rewriting a good portion of it to implement custom collections and get rid of that allocation. Also, it auto manages the subscribers but only makes sure they're not null before dispatching events. I realize now that the game objects can be marked for destruction without being destroyed so I need to update the subscriber component so it unsubscribes itself automatically when OnDestroy is called. But I'll share the full source that's there now and you're welcome to play around with it.
    JSON .NET for Unity is now only $20! - I wanted to make sure it was more affordable to all. JSON .NET for Unity is now only $20.

    JSON .NET For Unity
    Tested on iOS, PC / Mac, Android, Web Player, Windows Phone and Windows Store Apps
    Includes IOS and WebPlayer Support

    My Blog
    Unity, MVC, C#

    My Unity Site


  20. Location
    Omaha, NE
    Posts
    1,127
    I had a copy still on my server that includes the full source (visual studio 2012 solution) and binaries if you want to start picking it apart.

    www.dustinhorne.com/files/messagebusv1.zip
    JSON .NET for Unity is now only $20! - I wanted to make sure it was more affordable to all. JSON .NET for Unity is now only $20.

    JSON .NET For Unity
    Tested on iOS, PC / Mac, Android, Web Player, Windows Phone and Windows Store Apps
    Includes IOS and WebPlayer Support

    My Blog
    Unity, MVC, C#

    My Unity Site

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •