Binding System
Get It Now⭐
  • Welcome
  • Product
    • Minimal UI
    • Bind Class
    • Binding Values
    • Extending the System
      • Converters
      • Modifiers
      • Accessor Providers
    • Demo
    • Settings
    • Troubleshooting
      • Errors Visualization
      • Live Debug
      • Path Value Preview
    • Reserializer
    • Performance
    • FAQ
    • External Extensions
      • Odin Inspector
Powered by GitBook
On this page

Was this helpful?

  1. Product

Bind Class

PreviousMinimal UINextBinding Values

Last updated 9 months ago

Was this helpful?

The central piece of the 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.

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 (an immediate version is available in experimental versions, to be released on demand). 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
Inspector View of MyMonoBehaviour