Skip to main content

UnityEvent Binders

Binders that forward ViewModel values into a UnityEvent, so changes can be subscribed to in the Inspector.


Overview

A UnityEvent binder receives a value from the ViewModel and invokes the matching UnityEvent<T>. Reactions to ViewModel changes are configured in the Inspector, without code.


Typed binders

BinderUnityEventData type
UnityEventBoolMonoBinderUnityEvent<bool>bool
UnityEventFloatMonoBinderUnityEvent<float>float
UnityEventIntMonoBinderUnityEvent<int>int
UnityEventLongMonoBinderUnityEvent<long>long
UnityEventDoubleMonoBinderUnityEvent<double>double
UnityEventStringMonoBinderUnityEvent<string>string
UnityEventColorMonoBinderUnityEvent<Color>Color
UnityEventVector2MonoBinderUnityEvent<Vector2>Vector2
UnityEventVector3MonoBinderUnityEvent<Vector3>Vector3
UnityEventQuaternionMonoBinderUnityEvent<Quaternion>Quaternion
UnityEventEnumMonoBinderUnityEvent<int>enum (as int)

Special binders

UnityEventBoolByBindMonoBinder

Invokes UnityEvent<bool> on bind/unbind of the binder rather than on value change:

  • OnBoundUnityEvent<bool>(true)
  • OnUnboundUnityEvent<bool>(false)

Implements IAnyBinder: accepts any data type (the value type is ignored).

Properties:

  • _isInvert: inverted, true on unbind and false on bind

When to use: show or hide a UI element depending on whether the binder is bound.

UnityEventSwitcherMonoBinder

bool → one of two values → UnityEvent<T>.

UnityEventNumberConditionMonoBinder

A numeric condition → UnityEvent<bool>. Compares the number with a threshold.

UnityEventNumberConditionSwitcherMonoBinder

A numeric condition → one of two values.


Supported modes

Every UnityEvent binder supports OneWay and OneTime.


Example

[ViewModel]
public partial class NotificationViewModel
{
[OneWayBind] private bool _hasNewMessages;
[OneWayBind] private string _message;
}

In the Inspector:

  1. Add UnityEventBoolMonoBinder → bind to HasNewMessages
  2. In the UnityEvent subscribe a method, for example NotificationPanel.SetActive(bool)
  3. Add UnityEventStringMonoBinder → bind to Message
  4. In the UnityEvent subscribe the method that shows the message

See also