Class BaseEventTrigger
Abstract base class providing the shared infrastructure for invoking UnityEvents when an object enter or exit a trigger collider.
public abstract class BaseEventTrigger : MonoBehaviour, IDataLoggable
Inheritance
Derived
Implements
Examples
Override OnTriggerEnter / OnTriggerExit in a subclass to apply filtering, then
delegate to Collider) / Collider) so that the
base class handles the enabled-check, one-shot guard, and logging.
using UnityEngine;
using mobisim.Event;
public class TagFilteredEventTrigger : BaseEventTrigger
{
[SerializeField] private string _requiredTag = "Player";
private void OnTriggerEnter(Collider other)
{
// Resolve the actor root: child colliders on compound Rigidbodies report themselves,
// not the root GameObject that carries the tag.
var root = other.attachedRigidbody
? other.attachedRigidbody.gameObject
: other.gameObject;
if (root.CompareTag(_requiredTag))
TriggerEnterEvent(other); // base handles enabled-check and _onlyTriggerOnce
}
private void OnTriggerExit(Collider other)
{
var root = other.attachedRigidbody
? other.attachedRigidbody.gameObject
: other.gameObject;
if (root.CompareTag(_requiredTag))
TriggerExitEvent(other);
}
}
Remarks
This class has two distinct responsibilities:
- Event dispatch — exposes Collider) and Collider) as virtual entry points that subclasses call after applying their own filtering logic (tag checks, layer masks, distance tests, etc.).
- Data logging — implements IDataLoggable so that every trigger interaction is automatically timestamped and made available to the project's analysis pipeline via Collect.
This class deliberately does not implement Unity's OnTriggerEnter or
OnTriggerExit. Each subclass is responsible for defining those callbacks according to
its own requirements — whether that means applying filter logic (tag checks, layer masks,
distance tests, etc.) before forwarding to Collider) /
Collider), or bypassing physics callbacks entirely and invoking those
methods manually from external code or a UnityEvent.
Both trigger methods check enabled before firing, meaning the trigger
can be enabled/disabled during runtime.
Implementing IDataLoggable opts this component into automatic discovery by the
nearest ancestor DataCollector in the scene hierarchy. The collector calls
Collect at regular analysis intervals; the method returns a snapshot copy of all
events recorded since the last collection and resets the internal cache, so each analysis
window receives exactly the events that occurred within it.
Fields
_onEnterTriggered
The UnityEvent invoked when a collider enters the trigger zone and passes all filter conditions defined by the concrete subclass.
[Header("EVENTS")]
[Tooltip("Called when a collider enters and passes the filter conditions.")]
[SerializeField]
protected UnityEvent<Collider> _onEnterTriggered
Field Value
_onExitTriggered
The UnityEvent invoked when a collider exits the trigger zone and passes all filter conditions defined by the concrete subclass.
[Tooltip("Called when a collider exits and passes the filter conditions.")]
[SerializeField]
protected UnityEvent<Collider> _onExitTriggered
Field Value
_onlyTriggerOnce
Whether the enter and exit events should each fire at most once over the lifetime of this component.
[Header("TRIGGER SETTINGS")]
[SerializeField]
[Tooltip("Whether the enter and exit events should each fire at most once.")]
protected bool _onlyTriggerOnce
Field Value
bool
Remarks
When true, Collider) nulls out _onEnterTriggered
after the first successful invocation, and Collider) does the same
to _onExitTriggered.
logCache
Runtime cache that accumulates TriggerRecord entries between successive Collect calls.
protected EventTriggerDataLog logCache
Field Value
Remarks
Initialised lazily on the first logged event (see String)), so
no allocation is made for trigger zones that never fire. When Collect is
called, the cache is deep-copied into a new EventTriggerDataLog that is
handed off to the analysis pipeline, and this field is reset to null so the next
collection window starts empty.
Methods
Collect()
Returns a snapshot of all trigger interactions logged since the last collection, then resets the internal cache so the next collection window starts empty.
public virtual DataLog Collect()
Returns
DataLog — A deep copy of the accumulated EventTriggerDataLog, or null if no
trigger events occurred since the last call to this method.
Remarks
The snapshot is produced via the copy constructor of EventTriggerDataLog, giving the caller a stable view of the data even if new trigger events are logged on the main thread after this method returns.
Returning null when logCache is null signals to the
DataCollector that this component produced no events during the last analysis
window. The collector is designed to skip null returns, so no placeholder
allocation is ever needed.
Start()
Intentionally empty Unity lifecycle callback required to expose the component's enabled toggle in the Unity Inspector.
protected virtual void Start()
Remarks
Unity only renders the enabled checkbox in the Inspector header for components that
define at least one of the recognised message methods (Start,
Update, FixedUpdate, etc.). Because BaseEventTrigger acts as
a purely passive responder — no per-frame work is required — this override exists solely
to satisfy that Unity requirement.
TimestampEventLog(string, string)
Appends a new timestamped record to the runtime log cache, capturing the name of the triggering object and a human-readable event-type label.
protected void TimestampEventLog(string eventType, string objName)
Parameters
| Name | Type | Description |
|---|---|---|
eventType | string | A short human-readable label for the event type (e.g., "Enter UnityEvent Invoked"). Stored verbatim in the TriggerRecord and appears in analysis exports. |
objName | string | The resolved name of the game object whose collider triggered the event. For compound-Rigidbody actors this is the Rigidbody root's name, not the child collider's name. |
Remarks
The cache is allocated lazily on the first call using the null-coalescing assignment
operator (??=), so no heap allocation occurs for trigger zones that never fire.
The EventTriggerDataLog is identified by this component's
GameObject name.
TriggerEnterEvent(Collider)
Core dispatch method called by subclasses when an entering collider has passed all filter conditions. Logs the interaction and invokes _onEnterTriggered provided the component is enabled and the event has not already been consumed.
public virtual void TriggerEnterEvent(Collider collider)
Parameters
| Name | Type | Description |
|---|---|---|
collider | Collider | The collider that entered the trigger zone, as provided by Unity's OnTriggerEnter callback. May be null when the method is invoked programmatically (e.g., from a test harness or editor tool) rather than by a physics event. |
Remarks
Subclasses call this method from within UnityEvent such as OnTriggerEnter, which Unity fires
regardless of the component's enabled state. Checking enabled here means it
can be deactivated mid-session (via a UnityEvent, or script).
If _onlyTriggerOnce is true, _onEnterTriggered is set to
null after invocation. Subsequent calls are silently ignored because the
null guard fails.
TriggerExitEvent(Collider)
Core dispatch method called by subclasses when an exiting collider has passed all filter conditions. Logs the interaction and invokes _onExitTriggered provided the component is enabled and the event has not already been consumed.
public virtual void TriggerExitEvent(Collider collider)
Parameters
| Name | Type | Description |
|---|---|---|
collider | Collider | The collider that exited the trigger zone, as provided by Unity's OnTriggerExit callback. May be null when invoked programmatically. |
Remarks
Mirrors the behaviour of Collider): the enabled
check allows the zone to be deactivated mid-session and the event is prevented from firing more
than once when _onlyTriggerOnce is true.