Custom UI Event System revamped

Hi everyone, there is a really good thread about Unity Events and the guys there were discussing about the current event Unity v5.x system limitations.

Current Unity Event System limitations:
-Nonexistent way for re-arrange events execution order.
-Methods with multiple parameter values are not supported.
-Methods with default parameter values are not supported.
-VectorX as a parameter is not supported.
-Color as a parameter is not supported.

I think UnityEvents are on the right path but the way they are now are currently too simple, at least inspector wise, and for me this is a big thing.

This is why I decided to start making my own custom version of a UI event system based on previous implementations and I wanted to share it with the community. You can download the latest version from the Asset Store here: Event Delegate System.

There is also a GitHub repo for the Event Delegate System if you want to dive in the code and help improving this out.

Event Delegate System features:
-Easy way for reordering events, like a drag and drop kind style!
-Unlimited parameters.
-Support for properties and fields.
-Supporting usage of methods from Components and GameObjects.
-Support for static methods and methods with return values.
-Support for methods with default parameters values.
-Support for Enum, Enum Flags, Vector2, Vector3, Vector4 and Color as parameters.
-Parameter mode, you can setup either a value or a reference.
-Allow folding the entire event list for visualization clarity.

Easy to use!:

using UIEventDelegate;

public class EventTest : MonoBehaviour
{
    public ReorderableEventList OnEnableEvents;

    void OnEnable()
    {
        if (OnEnableEvents.List.Count > 0)
            EventDelegate.Execute(OnEnableEvents.List);
    }
}

Event Display:

Method menu selection:

Current Version

v12.3
-Refresh rate optimization added for delegates visualization. (thanks to Froghuto)

v12.2
-Added option to visualize enums as flags.
-Bug fixes.

v12.1
-Adding the ability to refresh a target component’s functions manually. (request from QuantumCalzone)
-Using toggle on label click for easier fold-out (request from Jay-Pavlina)
-Allow folding the entire event list for visualization clarity.
-Improving check for Unity version. Adding support for Unity 2017 or newer versions.

Changelog

v12
-Support for properties and fields.
-Supporting usage of methods from Components and GameObjects.
-Improved list height calculation.
-Private methods are not being showed anymore.
-Removing indent level of parameters below method.
-Avoiding the use of foreach instructions to avoid heap allocations that will eventually trigger the garbage collector. Also loops micro improvements implemented.
-Minor code cleanup.

v11
-Allow the use of flags with enum types.
-Added ability to change a method while in Play mode and thus enabling tests without stopping gameplay. Previously it was using the first executed method saved in cache.
-EventDelegate drawer optimizations. Just asking once for the use of Parameter mode (Value or Reference) per parameter.
-Parameters are not requested again only if in editor mode (for testing purpose) or if the parameter is a reference. We assume value parameters will never change in the realtime deployed app.
-Added missing error log in case a method invoke failed when no arguments expected.
-Some minor code cleanup.

v10.1
-Issue affecting methods using void template as return type is now solved.

v10
-Reoderable feature implemented. Easy way for reordering events, like a drag and drop kind style!
-Enum support implemented.
-Parameter mode, you can setup either a value or a reference. You can now select any property to be a reference from a GameObject or using a manual value in the inspector.
-Issues calculating height fixed. Added offset option for overlap issue in reorderable event list.
-Fixed fold group indent level.

v9
-Support for Vector2, Vector3, Vector4 and Color as parameters.
-EventDelegate added to UIEventDelegate namespace to avoid name collision.
-Adding a “<Missing - ‘previous method name’>” when method was not found.
-Improved memory usage and performance when drawing events by avoiding the creation of a new EventDelegate object per draw, only for confirming existence. Now only one is created at startup.
-Fixed issue for components with some specific return value types.

Known issue:
-When Inspector width is below a very small threshold the Vector2 and Vector3 attributes will get drawn below their respective lines.

v8
-Support for non-public and static methods.
-Overloaded methods are now correctly filtered.

v7
-Support for methods with return values.
-Showing previous name method when used method has gone missing, like: <Missing - MethodName>.
-Parameters removed for selected method. We only show the name of the selected method since parameters are already been shown, and it clears the event display quite a bit.

v6
-Support for methods with multiple and default parameters values.
-Methods are displayed sorted and showing parameter type and name.

Future improvements:
-Add an easy built in method to add or remove events via scripts. Helpful for building EventDelegates during runtime.
-Add the ability to refresh a target component’s functions manually.
-Allow folding the entire event list for visualization clarity.
-Create “Examples” scene.
-Support for overloaded methods.
-Support for generic methods. (Beware that currently generics methods are not being filtered)
-Memory footprint optimization using lordofduct ‘parameter class’ implementation as reference.
-Allow to distinguish between different components in the same GameObject.

I deeply hope that this goes as a starting point in the interest to point out Unity how important would be to support this in the UI system, having this features shipping with Unity would be the ideal case!

Please let me now any suggestion, feature request or pizza donations be my guest! :slight_smile:

You can also send some pizza donations here:

Best regards,
Lermy

10 Likes

Thank you for this, I’ll try your script and give you a feedback :slight_smile:

1 Like

Hi everyone, I just posted a EventDelegate v7! Here are the changes:

-Support for methods with return values.
-Showing previous name method when used method has gone missing, like: <Missing - MethodName>.
-Parameters removed for selected method. We only show the name of the selected method since parameters are already been shown, and it clears the event display quite a bit.

I updated the original post as well with screenshots and the package.

Hit me back for any issue or suggestions!

Have a nice one!
Lermy

Hi everyone, I just posted a EventDelegate v8! Here are the changes:

-Support for non-public and static methods.
-Overloaded methods are now correctly filtered.

Do not be shy people, you can hit me back for any issue or suggestions!

Have a nice one!
Lermy

I’ll have to try that. :stuck_out_tongue:

1 Like

You should throw this up in a version control repo somewhere. Bitbucket and GitHub are both good ones. This lets people both contribute and use the code more easily.

2 Likes

Thanks BoredMormon, great suggestion! I will definitely do it and post you guys back the link.

Yeah, I haven’t looked at it once yet just for that reason.

I just look at the ‘unitypackage’ and grumble… “ugh, I have to download, create an empty unity project, import, open in visual studio, and then look at this?”

1 Like

Hello eveyrone! I just created a repo in github so anyone willing to help or improve the Event System is more than welcome!!

Will this work on all of Unity’s platforms? I’m specifically wondering about iOS and WebGL.

I’m also wondering if this will work with any serializable type.

1 Like

Hi Jay, as far as I am concerned the reflection namespace is supported across all platforms that uses Mono, leaving Window Store and Windows Phone unsupported for the moment since they use their own .Net classes and I haven’t had the chance to test them on those just yet, :face_with_spiral_eyes::slight_smile:

Regarding your concern about serializable types, the answer is yes it will work.

Make as many tests as you wish and If you have any issues please let us know!

You have a name collision with NetworkBehaviour.EventDelegate. You should either change the name or put it in a namespace.

Also I tested on iOS and it worked fine.

Edit: Also, when you use EditorGUI.FoldOut, it’s easier to click if you set “toggleOnLabelClick” to true (line 126 of EventDelegateDrawer.cs)

1 Like

I see, I was not including NetworkBehaviour on the development project, thanks a lot for the tip. Great it worked like a charm for you, if you have any suggestion or issue do not hesitate to post.

Have a nice one.

Hi guys I just updated the post with a new version bringing some new parameter support and some cool optimizations and fixes. The master branch on the github repo is also updated for those who are interested in the code or making a contribution.

Here are the new features:

-Support for Vector2, Vector3, Vector4 and Color as parameters.
-EventDelegate added to UIEventDelegate namespace to avoid name collision.
-Adding a “<Missing - ‘previous method name’>” when method was not found.
-Improved memory usage and performance when drawing events by avoiding the creation of a new EventDelegate object per draw, only for confirming existence. Now only one is created at startup.
-Fixed issue for components with some specific return value types.

Leave your comment, issue or suggestion!
Have a nice one.
Lermy

@lermy3d I really like your Event System. The only issue I have with it is that I wish somehow visually the events stood out more in the inspector. When I’m looking down the inspector of a component with multiple variables sometimes it’s easy to get lost in the variables, where with Unity’s event system they have that separate design to events which immediately draws my eye for me to realize they are events. If you could somehow mirror that or have an inspector UI interface that stood out more I think that would really help. Keep up the great work.

1 Like

Hello BearFish, thank you so much for leaving a comment on the thread. I totally think this should be done, I was actually working on getting the re-orderable feature to be ready since this will solve both issues in a row. I have tried using rotorz reorderable list due to all the advantages it provides but I haven’t been able to make it work just yet.

So do not worry, is in the ToDo list and this is definitely a must have, I just haven’t been able to make it work.

For those who want to help on these feature there is a develop branch on the repo if anyone would like to give a hand with that too.

Thanks again, and I’ll keep you posted.

I liked before that I could pass a Vector2 parameter as a property of an object. Can you make it so that you can either enter a value manually or send a property of an object (like it was before)? This would be extremely powerful.

1 Like

I see what you mean, let’s see if I can add an option for drawing a manual value or setting up a reference. I will add it in the ToDo list. Thanks for the suggestion.

1 Like

If you want, check out my ‘VariantReference’ class:

It’s a bit like your ‘Parameter’ class, but it packs into a much smaller memory foot print, and has support for pulling properties from existing properties. As well as an ability to evaluate simple arithmetic operations that include properties of objects.

The property reading is done via my ‘dynamic’ namespace:

2 Likes

I did notice EventDelegate was serializing a lot of data. I like your solution.