Binding System 2
  • Welcome
  • Product
    • Binding Values
    • Minimal UI
    • Pinning Fields
    • Extending the System
      • Bind Class
      • Converters
      • Modifiers
        • List of Available Modifiers (WIP)
      • Bind Rerouting
      • Accessor Providers
    • Demo
    • Settings
    • Troubleshooting
      • Errors Visualization
      • Live Debug
      • Path Value Preview
      • Refactoring
      • Bindings Dependencies
    • Reserializer
    • Performance
    • FAQ
    • External Extensions
      • Odin Inspector
    • ‼️Upgrading from Version 1
  • Change log
    • Changes from version 1
  • Third Party Licenses
Powered by GitBook
On this page

Was this helpful?

  1. Product
  2. Extending the System

Bind Class

PreviousExtending the SystemNextConverters

Last updated 6 months ago

Was this helpful?

One of the most important classes in BindingSystem is the Bind<T> class. This class wraps the T type with the binding mechanism and provides the necessary functionality for serializing binding information. Additionally, it includes the BindData structure for storing the binding information. The Bind<T> class allows for binding paths with different depths. For example, a float field can be bound to the path "position.x" of a Transform so that at runtime, it will have the value of x component of transform's position.

Using this class gives slightly more performance at cost of flexibility, since it requires coding, and thus cannot be applied to third party scripts.

The Bind<T> has an internal data pipeline when reading the data from the bound source or writing it to the source:

There are several variations of the Bind<T> class available:

  • ReadOnlyBind<T>: Allows read-only access to the bound value.

  • WriteOnlyBind<T>: Allows write-only access to the bound value.

  • BindDataFor<T>: Doesn't have a direct value field, must only bind to other sources.

These classes can be implicitly converted to the inner T type when needed.

To define a new Bind<T> simply add a field or property as follows:

class MyMonoBehaviour : MonoBehaviour
{
    public Bind<float> myBoundFloat;
    public Bind<float> myBoundAndInitializedFloat = 3.14f.Bind();
}

The above code will let users bind the properties directly in Unity's Inspector.

The power of Bind<T> type is in its drawer, which allows for a quick and easy binding process with an intuitive UI.

Binding with code

To create a new bound value in code simply call the constructor with specified target and path:

    void Start()
    {
        Object target = GameObject.Find("Sphere").transform;
        myBoundFloat = new Bind<float>(target, "position.x");
    }

When accessing Bind<T>'s value, simply use the .Value property or, in case of reading the value, ommit it altogether:

    void Update()
    {
        float value = myBoundFloat; // Here the implicit conversion is applied
        value *= 2;
        myBoundAndInitializedFloat.Value = value;
    }

The Bind<T> class and its variations (except WriteOnlyBind<T>) can notify users when its value changes through its ValueChanged event:

    void Start()
    {
        myBoundFloat.ValueChanged += (oldValue, newValue) => Debug.Log("New Value is: " + newValue);
    }

Please Note: ValueChanged is not called immediately when the underlying value is changed, due to security limitations. All these events are processed at multiple points within the same frame.

For more expert users, the inner BindData structure can be used to create custom Bind classes. BindData stores all required information to access a bound value, as well as parameters for converters and modifiers.

Page cover image
Default Bind Pipeline
Inspector View of MyMonoBehaviour