Class EventBus
A global container that maps Event names and Component references to actions for registered listeners.
Assembly: Unity.VisualScripting.Core.dll
Syntax
public static class EventBus
Examples
The following example shows how to use the EventBus to send a custom event from a script to a node in
a graph. It also shows how to use the EventBus as a global event manager by executing a callback in a
script, not just a node.
For more information on how to create custom event nodes refer to the
User Manual.
In this example we've added some code to a GameObject. This code checks for when the user presses a sequence of
keys to enable a cheat code, then triggers the CheatCodeActivated event. We register the
CheatCodeActivated event in the Start method. The Update method triggers the event twice
with 2 different targets: one for the CheatCodeActivated callback and the other to trigger the
CheatCodeEnabled Node.
public class CheatCodeController : MonoBehaviour
{
public const string CheatCodeActivated = "CheatCodeActivated";
static readonly KeyCode[] famousCheatCode = {
KeyCode.UpArrow,
KeyCode.UpArrow,
KeyCode.DownArrow,
KeyCode.DownArrow,
KeyCode.LeftArrow,
KeyCode.RightArrow,
KeyCode.LeftArrow,
KeyCode.RightArrow,
KeyCode.B,
KeyCode.A
};
int index;
EventHook cheatCodeHook;
Action<EmptyEventArgs> godModeDelegate;
// A GameObject with a ScriptMachine that holds a graph with a
// CheatCodeEnabled Event Node.
public GameObject player;
void Start()
{
// Hold a reference to the EventHook and the Delegate for the
// EventBus.Unregister call in the OnDestroy method.
cheatCodeHook = new EventHook(CheatCodeActivated);
godModeDelegate = _ => EnableGodMode();
EventBus.Register(cheatCodeHook, godModeDelegate);
}
void Update()
{
if (Input.anyKeyDown)
{
if (Input.GetKeyDown(famousCheatCode[index]))
{
index++;
}
else
{
index = 0;
}
if (index >= famousCheatCode.Length)
{
// Triggers the EnableGodMode delegate
EventBus.Trigger(CheatCodeActivated);
// Triggers the CheatCodeEnabled Visual Scripting Node
EventBus.Trigger(new EventHook(
CheatCodeActivated,
player.GetComponent<ScriptMachine>()));
index = 0;
}
}
}
void OnDestroy()
{
EventBus.Unregister(cheatCodeHook, godModeDelegate);
}
void EnableGodMode()
{
Debug.Log("Cheat code has been entered. Enabling god mode.");
}
}
The CheatCodeEnabled Node:
[UnitTitle("On Cheat Code Enabled")]
public sealed class CheatCodeEnabled : MachineEventUnit<EmptyEventArgs>
{
protected override string hookName => CheatCodeController.CheatCodeActivated;
}
Methods
Register<TArgs>(EventHook, Action<TArgs>)
Declaration
public static void Register<TArgs>(EventHook hook, Action<TArgs> handler)
Parameters
Type Parameters
Trigger(string, GameObject)
Declaration
public static void Trigger(string name, GameObject target)
Parameters
Trigger(EventHook)
Declaration
public static void Trigger(EventHook hook)
Parameters
Trigger<TArgs>(string, GameObject, TArgs)
Declaration
public static void Trigger<TArgs>(string name, GameObject target, TArgs args)
Parameters
Type Parameters
Trigger<TArgs>(EventHook, TArgs)
Declaration
public static void Trigger<TArgs>(EventHook hook, TArgs args)
Parameters
| Type |
Name |
Description |
| EventHook |
hook |
|
| TArgs |
args |
|
Type Parameters
Unregister(EventHook, Delegate)
Declaration
public static void Unregister(EventHook hook, Delegate handler)
Parameters