Skip to content

Instantly share code, notes, and snippets.

Created June 3, 2023 23:17
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
An updated version of using standard Events subscriptions instead of Lists
using System;
using UnityEngine;
public class BaseGameEvent : ScriptableObject
[TextArea] public string developerDescription = "";
public event Action Raised;
public void Raise() => Raised?.Invoke();
public abstract class BaseGameEvent<T1> : BaseGameEvent
public new event Action<T1> Raised;
public void Raise(T1 t1) => Raised?.Invoke(t1);
public abstract class BaseGameEvent<T1,T2> : BaseGameEvent
public new event Action<T1, T2> Raised;
public void Raise(T1 t1, T2 t2) => Raised?.Invoke(t1, t2);
/* USE:
public class GameEvent : BaseGameEvent {}
public class IntGameEvent : BaseGameEvent<int> {}
public class IntStringGameEvent : BaseGameEvent<int,string> {}
/// examples of the three components required to utilize the system:
// the Scriptable Object Event definition
public class CoinGameEvent : BaseGameEvent<int>
// the MonoBehaviour on each Coin to collect
public class Coin : MonoBehaviour
// the Scriptable Object Event we created via the Create Asset menu
[SerializeField] private CoinGameEvent coinEvent;
// raise our coin event whenever the user picks up a coin
void OnTriggerEnter2D(Collider2D other)
// this can also be a ScriptableObject since they still have OnEnable() and OnDisable() methods
public class CoinManager : MonoBehaviour
int coins;
// register to Listen for the Coin Event
void OnEnable() => coinEvent.Raised += AddCoins;
void OnDisable() => coinEvent.Raised -= AddCoins;
// add the number of coins in the Event to our total
void AddCoins(int value) => coins += value;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment