A lightweight decoupled messaging system for Unity toolsets using the Observer Pattern.
In Unity development, "Spaghetti Code" occurs when systems are too tightly coupled (e.g., a UserCredits script manually searching for a UI script via GameObject.Find or GetComponent).
This project provides a Global Event Bus that acts as a central "Radio Station." It facilitates the transfer of data and signals across separate Unity toolsets, allowing them to communicate without ever needing a direct reference to one another.
In this scenario, the UserCredits script is "married" to the UIManager. To update the UI, the script needs a hard reference and specific knowledge of how the UI works. If you delete the UI, the logic breaks.
// UserCredits.cs
public class UserCredits : MonoBehaviour {
public UIManager ui; // Hard reference
void SpendCredits(int amount) {
// Logic is stuck: it HAS to know about the UI to work
ui.UpdateDisplay(amount);
}
}The UserCredits script is now "independent." It simply broadcasts a CreditEvent package. It doesn't care if a UI exists, if a Save System is listening, or if an Analytics script is tracking it.
// UserCredits.cs
public struct CreditEvent {
public int amount;
}
public class UserCredits : MonoBehaviour {
void SpendCredits(int amount) {
// Broadcast the data package to any system that cares
EventBus<CreditEvent>.Publish("CreditSpent", new CreditEvent { amount = amount });
}
}graph TD
A[Broadcaster: UserCredits.cs] -->|Publish 'CreditEvent'| B(<b>Global Event Bus <T></b>)
B -->|Notify with Data| C[Listener: UIManager.cs]
B -->|Notify with Data| D[Listener: SaveSystem.cs]
B -->|Notify with Data| E[Listener: Analytics.cs]