3

im making a game and want to code a event function. Example:

public void OnEvent(string args[]) {}

This function should be accessable through every script, like the Start() or Update() 'event' that you just need to call as function to be registered.

Any ideas?

1 Answer 1

3

Unity Events

Unity has a set of delegates, UnityAction, that have generic versions that accept up to 16 params.
Unity also has a set of events, UnityEvent, that also have generic versions with up to 16 params.

Unity events are used in the UI system for linking events (such as On Click of Button) to an action (a function in a script). These can be saved with the scene and are editable from the inspector, unlike the vanilla C# events and delegates.

These are both found in the UnityEngine.Events namespace.

Event Class example

private UnityEvent<string[]> myEvent = new UnityEvent<string[]>();

public void RegisterForEvent(UnityAction<string[]> action)
{
    myEvent.AddListener(action); // register action to receive the event callback
}
public void UnregisterEvent(UnityAction<string[]> action)
{
    myEvent.RemoveListener(myAction); // unregister to stop receiving the event callback
}
private void RaiseEvent()
{
    myEvent.Invoke(new string[] { "Hello", "World" }); // raise the event for all listeners
}

Some Other Class example

private UnityAction<string[]> myAction

private void OnEnable()
{
    // Create the action if it doesn't already exist
    //
    if (myAction == null)
        myAction = new UnityAction<string[]>(EventCallback);

    // Register to the event
    //
    otherScript.RegisterForEvent(myAction);
}
private void OnDisable()
{
    // Unregister from the event when this object is disabled or destroyed
    //
    otherScript.UnregisterEvent(myAction);
}

private void EventCallback(string[] values)
{
    Debug.Log("My action was called!");
}

Alternatively..

This can be accomplished with the event and delegate keywords. You create a delegate which is the method signature for the callback, and define an event that uses this delegate. Methods register to the event using += and unregister from the event using -=. Events Programming Guide for C#

You want to make sure to utilize OnEnable and OnDisable for registering/unregistering events in Unity, regardless of which route you decide to take.

Sign up to request clarification or add additional context in comments.

6 Comments

Yuck, I just looked at the source code and both UnityAction (literally an alias for delegate) and UnityEvent use delegates. I was hoping that UnityEvent.AddListener would be using light-weight references that don't need unsubscribing as we did with certain WPF 3rd party toolkits.
@MickyD Yeah, they are essentially serializable wrappers. The only other option that comes to mind is the observable pattern. Do you know how any of those 3rd party toolkits are implemented?
No, but I'd love to know too. I think the library was Catel
...Source can be found here. Seems to be using WeakReference. We'll have to sit down and replicate that functionality in our Unity projects :)
After some research, I have found this is referred to as the "WeakEvent Pattern". The principal reason for following the WeakEvent pattern is when the event source has an object lifetime that is potentially independent of the event listeners. Thanks for finding that portion of the source.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.