How to implement simple Publisher/Subscriber pattern in Unity C#

Here is publisher

using System;

public event EventHandler RailsEnabled;

void EnableRails()
{
if (RailsEnabled != null)
RailsEnabled(this, EventArgs.Empty);
}

Here is subscriber

void Start () {
Launcher.RailsEnabled += OnRailsEnabled;
}

private void OnRailsEnabled(object sender, EventArgs e)
{
RailsEnabled = true;
}

Watch in-depth video.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *