Network Trigger System
1. Overview
The Network Trigger System is a fundamental component of the HoloMIT networking architecture. It allows developers to invoke events across all connected clients (RPC-like behavior) by using the EventManagerController.
Each trigger is identified by a unique NetworkId (inherited from NetworkIdBehaviour), ensuring that when a trigger is activated on one client, the correct object on all other clients reacts accordingly.
2. NetworkTrigger (Base Component)
The NetworkTrigger is the simplest form of networked event. It fires a UnityEvent with no parameters across the network.
2.1. Inspector Reference
Figure 1: The NetworkTrigger component. Use the "OnTrigger" UnityEvent to link local logic.
Properties
- OnTrigger (UnityEvent): The event that will be executed locally and replicated on all other clients.
2.2. Public API
public virtual void Trigger(): The main method to activate the trigger. Call this from code or UnityEvents to broadcast the action.
3. Typed Triggers (Data Propagation)
For cases where you need to send specific data along with the trigger (e.g., a switch state, a score, or a message), HoloMIT provides several specialized triggers:
3.1. NetworkBoolTrigger
Used for binary states (On/Off, True/False).
- Event:
UnityEvent<bool> OnTrigger - Method:
Trigger(bool _data)
3.2. NetworkIntTrigger
Used for numeric values (IDs, counters, levels).
- Event:
UnityEvent<int> OnTrigger - Method:
Trigger(int _data)orTrigger()(uses theDatafield defined in inspector).
3.3. NetworkStringTrigger
Used for text-based data.
- Event:
UnityEvent<string> OnTrigger - Method:
Trigger(string _data)orTrigger().
3.4. NetworkByteTrigger
Used for raw data transmission (byte arrays).
- Event:
UnityEvent<byte[]> OnTrigger - Method:
Trigger(byte[] _data)orTrigger().
4. How to use (Step-by-Step)
- Attach the desired trigger component (e.g.,
NetworkBoolTrigger) to a GameObject. - Ensure the GameObject has a unique Network ID (managed by
NetworkIdBehaviour). - In the Inspector, add a listener to the
OnTriggerevent. - To fire the trigger from another script:
public NetworkBoolTrigger myTrigger;
void ToggleState(bool state) {
myTrigger.Trigger(state); // This will execute on ALL clients
}
5. Editor Utilities
To facilitate testing without a full network session, all triggers include Context Menu options:
- Force Trigger (Editor-only): Executes the full networking flow (calling
Trigger()). - Force OnTrigger (Editor-only): Only executes the local
UnityEventwithout sending network messages.
6. Common Pitfalls
- Missing NetworkId: Triggers rely on
NetworkIdto find their counterpart on other clients. If two objects have the same ID or no ID, the trigger will fail or target the wrong object. - Registration: Triggers automatically register their message types in
Awake(). If you instantiate a trigger and try to use it immediately, ensureAwakehas finished.