I was inspired by a great talk at Unite by Ryan Hipple.
https://youtu.be/raQ3iHhE_Kk?t=28m
His examples didn’t cover passing arguments with UnityEvents
Do I need to extend UnityEvent, GameEvent and GameEventListener from a generic base class for this to work properly?
If so do I need to make a new set of classes for each Type and combination of Types that I want to pass?
Game Event Scriptable Object
[CreateAssetMenu]
public class GameEvent : ScriptableObject
{
/// <summary>
/// The list of listeners that this event will notify if it is raised.
/// </summary>
private readonly List<GameEventListener> eventListeners =
new List<GameEventListener>();
public void Raise()
{
for(int i = eventListeners.Count -1; i >= 0; i--)
eventListeners[i].OnEventRaised();
}
public void RegisterListener(GameEventListener listener)
{
if (!eventListeners.Contains(listener))
eventListeners.Add(listener);
}
public void UnregisterListener(GameEventListener listener)
{
if (eventListeners.Contains(listener))
eventListeners.Remove(listener);
}
}
Listener Class
public class GameEventListener : MonoBehaviour
{
[Tooltip("Event to register with.")]
public GameEvent Event;
[Tooltip("Response to invoke when Event is raised.")]
public UnityEvent Response;
private void OnEnable()
{
Event.RegisterListener(this);
}
private void OnDisable()
{
Event.UnregisterListener(this);
}
public void OnEventRaised()
{
Response.Invoke();
}
}