Hi there,
I’m coming from this article
https://unity3d.com/how-to/architect-with-scriptable-objects
and would like to know how to raise an event using this architecture. I created a GameEvent class
[CreateAssetMenu(fileName = "New Game Event", menuName = "Game Event")]
public class GameEvent : ScriptableObject
{
private readonly List<GameEventSubscriber> subscriber = new List<GameEventSubscriber>();
public void Raise()
{
for (int i = subscriber.Count - 1; i >= 0; i--)
{
subscriber[i].OnEventRaised();
}
}
public void AddSubscriber(GameEventSubscriber subscriber)
{
this.subscriber.Add(subscriber);
}
public void RemoveSubscriber(GameEventSubscriber subscriber)
{
this.subscriber.Remove(subscriber);
}
}
and a OnEnterButtonDown scriptable object for testing purposes. Further I created a GameEventSubscriber
public class GameEventSubscriber : MonoBehaviour
{
[SerializeField]
private GameEvent eventSubscribingTo;
[SerializeField]
private UnityEvent actionsToExecuteOnEventRaised;
private void OnEnable()
{
eventSubscribingTo.AddSubscriber(this);
}
private void OnDisable()
{
eventSubscribingTo.RemoveSubscriber(this);
}
public void OnEventRaised()
{
actionsToExecuteOnEventRaised.Invoke();
}
}
and a Notifier script
public class Notifier : MonoBehaviour
{
public void Notify()
{
Debug.Log("Pressed!");
}
}
What I didn’t get so far is how to raise an event now? Yes, the GameEvent has a method called Raise() that I can call but what is the correct way to manage those calls?
Let’s assume I have a class taking those “EnterButtonInputs” and it should raise the OnEnterButtonDown event, how should I do that?