dimanche 17 avril 2016

How do I add a method to ANY event's invocation list at runtime (C# + Unity)

I'm trying to create an event-based sound triggering system for Unity (5.x) built on top of a project where existing code should be modified as little as possible.

The existing project has all kinds of events with different delegate signatures, like so:

public delegate void CustomDelegateType1();
public event CustomDelegateType1 OnCustomEvent1;

public delegate void CustomDelegateType2(CustomClass param);
public event CustomDelegateType2 OnCustomEvent2;

I want to use these events to trigger sounds by adding a method to the invocation list at runtime, treating the new method just like any other subscriber. Basically, start by using reflection to get a list of events on the current GameObject, display those events in an editor dropdown on my Component, and then bind a method to that event on Awake()

The problem is: how do you add a generic method to ANY event type (as long as it returns void), given the different number and types of params used in the different delegates?





Aucun commentaire:

Enregistrer un commentaire