Bind Class
Last updated
Last updated
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:
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.
To create a new bound value in code simply call the constructor with specified target and path:
When accessing Bind<T>
's value, simply use the .Value
property or, in case of reading the value, ommit it altogether:
The Bind<T>
class and its variations (except WriteOnlyBind<T>
) can notify users when its value changes through its ValueChanged
event:
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.