What is a Binder?
A Binder is a component of the Bindy system that allows two or more objects to be bound together to synchronize their values. It can be used to connect UI elements with game logic scripts, or to synchronize data between different parts of a system.
How it works
A Binder component can be added to any GameObject in the Unity Editor. It contains one or more Bindable components that represent the objects to be bound together. Each Bindable has a unique identifier, which is used to identify it within the Bind.
When a Binder component is enabled, it automatically connects all of its Bindables to the Bind, which is responsible for storing and synchronizing their values. The Bindables can be connected in one of two ways:
Direct Connection Approach
In this approach, the Bindables are directly connected to each other with a Bidirectional connection type.
For example, if we have a volume slider UI element and a Sound Manager script, we can create a Binder component and add two Bindables to it.
One Bindable is connected to the volume slider with a Bidirectional connection type that gets the value when connected, and the other Bindable is connected to the volume float variable in the Sound Manager script with a Bidirectional connection type that sets the value when connected.
At runtime, when these Bindables get connected to the Bind, the Set Value will send the value to the Bind and the Get Value will get the value from the Bind.
Set Value happens at the beginning of the frame, while Get Value happens at the end of the frame, to avoid race conditions.
In this approach, the Bindables are not directly connected to each other, but are connected to the same Bind using a shared identifier.
For example, if we have an InputField UI element for username and a Player Manager script, we can create two Binders.
One Binder is created on the InputField with a BindId set to (‘PlayerInfo’, ‘Username’) and a Bidirectional connection type that gets the value when connected, and the other Binder is created on the Player Manager GameObject with the same BindId and a Bidirectional connection type that sets the value when connected.
When these Binders are enabled, they connect to the Bind and start synchronizing their values.
This approach allows for more flexibility, as Bindables can be added or removed from the Bind at any time, and they can be connected to different objects without having to modify their connections.
In both cases, the Binder component allows for easy and flexible synchronization of values between objects, making it a powerful tool for building complex systems in Unity.