Guide for using View Binding components in .prefabxml markup.
- ViewContext — container with variables and events, added to root GameObject.
- Applicators — reactively update UI components when a variable changes.
- Binders — listen to UI events and fire ViewContext events.
- Adapters — transform variable values (bool→string, float ratio, etc).
Variables and events are [SerializeReference] lists on ViewContext. Define them using <Ref> inside the ViewContext tag and reference via <Item rid="...">:
<ViewContext renderOnStart="true">
<Ref id="myVar" type="CodeWriter.ViewBinding CodeWriter.ViewBinding.ViewVariableString"
name="myVar" context="#Root" />
<Ref id="myEvent" type="CodeWriter.ViewBinding CodeWriter.ViewBinding.ViewEventVoid"
name="myEvent" context="#Root" />
<Field name="vars">
<Item rid="myVar" />
</Field>
<Field name="evts">
<Item rid="myEvent" />
</Field>
<Field name="listeners">
<Item ref="#SomeApplicator" />
<Item ref="#SomeBinder" />
</Field>
</ViewContext>| Type attribute | Value type |
|---|---|
CodeWriter.ViewBinding CodeWriter.ViewBinding.ViewVariableBool |
bool |
CodeWriter.ViewBinding CodeWriter.ViewBinding.ViewVariableInt |
int |
CodeWriter.ViewBinding CodeWriter.ViewBinding.ViewVariableFloat |
float |
CodeWriter.ViewBinding CodeWriter.ViewBinding.ViewVariableString |
string |
| Type attribute | Parameter |
|---|---|
CodeWriter.ViewBinding CodeWriter.ViewBinding.ViewEventVoid |
none |
CodeWriter.ViewBinding CodeWriter.ViewBinding.ViewEventBool |
bool |
CodeWriter.ViewBinding CodeWriter.ViewBinding.ViewEventInt |
int |
CodeWriter.ViewBinding CodeWriter.ViewBinding.ViewEventFloat |
float |
CodeWriter.ViewBinding CodeWriter.ViewBinding.ViewEventString |
string |
Applicators, binders, and controllers have inline ViewVariable* / ViewEvent* fields with two sub-fields:
context— reference toViewContextBasecomponent (use#id)name— string name matching a variable/event in that context
Use dot notation in XML attributes:
<TMPTextApplicator target="#Display"
source.context="#Root" source.name="myVariable" />
<ButtonClickBinder button="#Btn"
onClick.context="#Root" onClick.name="myEvent" />- Add
<ViewContext />on the root GameObject (give it anid) - Define variables and events via
<Ref>, wire intovars/evtsvia<Item rid="..."> - Add
listenerswith<Item ref="#id">pointing to all applicators/binders - Add applicators/binders on child GameObjects, wiring fields via dot notation
<UnityPrefab>
<GameObject name="Counter" id="Root">
<RectTransform m_SizeDelta="200, 80" />
<Image m_Color="#1E1E2E" />
<ViewContext renderOnStart="true">
<Ref id="counter" type="CodeWriter.ViewBinding CodeWriter.ViewBinding.ViewVariableInt"
name="counter" context="#Root" />
<Ref id="increment" type="CodeWriter.ViewBinding CodeWriter.ViewBinding.ViewEventVoid"
name="increment" context="#Root" />
<Ref id="decrement" type="CodeWriter.ViewBinding CodeWriter.ViewBinding.ViewEventVoid"
name="decrement" context="#Root" />
<Field name="vars">
<Item rid="counter" />
</Field>
<Field name="evts">
<Item rid="increment" />
<Item rid="decrement" />
</Field>
<Field name="listeners">
<Item ref="#DecrementBtn" />
<Item ref="#Display" />
<Item ref="#IncrementBtn" />
</Field>
</ViewContext>
<CounterController
counter.context="#Root" counter.name="counter"
increment.context="#Root" increment.name="increment"
decrement.context="#Root" decrement.name="decrement" />
<GameObject name="DecrementBtn" id="DecrementBtn">
<Image m_Color="#FF5577" />
<Button />
<ButtonClickBinder button="#DecrementBtn"
onClick.context="#Root" onClick.name="decrement" />
</GameObject>
<GameObject name="Display" id="Display">
<TextMeshProUGUI m_text="0" m_fontSize="36" m_fontColor="#FFFFFF"
m_HorizontalAlignment="Center" m_VerticalAlignment="Middle" />
<FormattedTMPTextApplicator target="#Display" format="<counter>">
<Field name="extraContexts">
<Item ref="#Root" />
</Field>
</FormattedTMPTextApplicator>
</GameObject>
<GameObject name="IncrementBtn" id="IncrementBtn">
<Image m_Color="#44D962" />
<Button />
<ButtonClickBinder button="#IncrementBtn"
onClick.context="#Root" onClick.name="increment" />
</GameObject>
</GameObject>
</UnityPrefab>Container for variables and events. Add to root GameObject.
| Field | Type | Description |
|---|---|---|
renderOnStart |
bool | Call Render() on Start |
Variables (vars) and events (evts) are configured in Inspector.
Fires a void event on button click. Requires Button on same GameObject.
| Field | Type | Description |
|---|---|---|
button |
Button | Target button (#id reference) |
onClick |
ViewEventVoid | .context = ViewContext ref, .name = event name |
Fires a bool event when toggle value changes. Requires Toggle on same GameObject.
| Field | Type | Description |
|---|---|---|
toggle |
Toggle | Target toggle (#id reference) |
onToggle |
ViewEventBool | .context = ViewContext ref, .name = event name |
Fires a float event when slider value changes. Requires Slider on same GameObject.
| Field | Type | Description |
|---|---|---|
slider |
Slider | Target slider (#id reference) |
onValueChanged |
ViewEventFloat | .context = ViewContext ref, .name = event name |
Fires a string event when input field text changes. Requires InputField on same GameObject.
| Field | Type | Description |
|---|---|---|
inputField |
InputField | Target input field (#id reference) |
onTextChanged |
ViewEventString | .context = ViewContext ref, .name = event name |
Fires a string event when input field editing ends. Requires InputField on same GameObject.
| Field | Type | Description |
|---|---|---|
inputField |
InputField | Target input field (#id reference) |
onEndEdit |
ViewEventString | .context = ViewContext ref, .name = event name |
All applicators based on ComponentApplicatorBase<TTarget, TVariable> share these fields:
| Field | Type | Description |
|---|---|---|
target |
TTarget | Target component (#id reference) |
source |
TVariable | .context = ViewContext ref, .name = variable name |
Sets TMP_Text.text from a string variable. Requires TMP_Text on same GameObject.
target: TMP_Textsource: ViewVariableString
Sets Text.text from a string variable. Requires Text on same GameObject.
target: Textsource: ViewVariableString
Sets Toggle.isOn from a bool variable. Requires Toggle on same GameObject.
target: Togglesource: ViewVariableBool
Sets Slider.value from a float variable. Requires Slider on same GameObject.
target: Slidersource: ViewVariableFloat
Sets InputField.text from a string variable. Requires InputField on same GameObject.
target: InputFieldsource: ViewVariableString
Sets Image.fillAmount from a float variable.
target: Imagesource: ViewVariableFloat
Sets Image.enabled from a bool variable.
target: Imagesource: ViewVariableBool
Sets Button.interactable from a bool variable.
target: Buttonsource: ViewVariableBool
Sets CanvasGroup.alpha from a float variable.
target: CanvasGroupsource: ViewVariableFloat
Sets CanvasGroup.interactable from a bool variable.
target: CanvasGroupsource: ViewVariableBool
Sets CanvasGroup.blocksRaycasts from a bool variable.
target: CanvasGroupsource: ViewVariableBool
Sets CanvasGroup.alpha to 0/1 and blocksRaycasts from a bool variable.
target: CanvasGroupsource: ViewVariableBool
Sets GameObject.SetActive from a bool variable.
| Field | Type | Description |
|---|---|---|
target |
Transform | Target transform (#id reference) |
source |
ViewVariableBool | .context = ViewContext ref, .name = variable name |
inverse |
bool | Invert the value |
These applicators do NOT use source. They format text from variables referenced in a format string.
Formats text using <variable_name> placeholders. Requires TMP_Text on same GameObject.
extraContexts is required — without it the applicator cannot resolve variable placeholders. Use <Field> to set the array with references to ViewContext(s):
<FormattedTMPTextApplicator target="#Display" format="Score: <score>">
<Field name="extraContexts">
<Item ref="#Root" />
</Field>
</FormattedTMPTextApplicator>| Field | Type | Description |
|---|---|---|
target |
TMP_Text | Target text component (#id reference) |
format |
string | Format string, e.g. "Score: <score>" |
extraContexts |
ViewContextBase[] | Required. Contexts to resolve variables from (use Field + Item ref) |
Same as FormattedTMPTextApplicator but localizes the format string first. extraContexts is required.
<LocalizedTMPTextApplicator target="#Display" format="ui_score">
<Field name="extraContexts">
<Item ref="#Root" />
</Field>
</LocalizedTMPTextApplicator>| Field | Type | Description |
|---|---|---|
target |
TMP_Text | Target text component (#id reference) |
format |
string | Localization key / format string |
extraContexts |
ViewContextBase[] | Required. Contexts to resolve variables from (use Field + Item ref) |
Invoke a UnityEvent<T> when a variable changes. Useful for custom reactions.
All share:
| Field | Type | Description |
|---|---|---|
source |
TVariable | .context = ViewContext ref, .name = variable name |
callback |
UnityEvent<T> | Unity event to invoke |
- UnityEventBoolApplicator — source: ViewVariableBool
- UnityEventIntApplicator — source: ViewVariableInt
- UnityEventFloatApplicator — source: ViewVariableFloat
- UnityEventStringApplicator — source: ViewVariableString
Adapters transform variable values. They act as mini-contexts producing a result variable. All adapters based on SingleResultAdapterBase have a result field (the output variable with .context and .name).
Inverts a bool variable.
| Field | Type | Description |
|---|---|---|
result |
ViewVariableBool | Output: .context and .name |
source |
ViewVariableBool | Input: .context and .name |
Converts bool to string.
| Field | Type | Description |
|---|---|---|
result |
ViewVariableString | Output |
source |
ViewVariableBool | Input |
trueString |
string | String when true (default: "TRUE") |
falseString |
string | String when false (default: "FALSE") |
Converts bool to formatted string with variable placeholders.
| Field | Type | Description |
|---|---|---|
result |
ViewVariableString | Output |
source |
ViewVariableBool | Input |
trueFormat |
string | Format when true |
falseFormat |
string | Format when false |
extraContexts |
ViewContextBase[] | Additional contexts |
Compares a string variable to a constant, outputs bool.
| Field | Type | Description |
|---|---|---|
result |
ViewVariableBool | Output |
source |
ViewVariableString | Input |
comparer |
enum | Equals or NotEquals |
other |
string | String to compare against |
Passes float through with precision formatting.
| Field | Type | Description |
|---|---|---|
result |
ViewVariableFloatFormatted | Output (has precision and fixedPrecision) |
source |
ViewVariableFloat | Input |
Computes numerator / denominator.
| Field | Type | Description |
|---|---|---|
result |
ViewVariableFloat | Output |
numerator |
ViewVariableFloat | Numerator: .context and .name |
denominator |
ViewVariableFloat | Denominator: .context and .name |
Formats a string with <variable> placeholders.
| Field | Type | Description |
|---|---|---|
result |
ViewVariableString | Output |
format |
string | Format string |
Localizes and formats a string.
| Field | Type | Description |
|---|---|---|
result |
ViewVariableStringLocalized | Output |
format |
string | Localization key / format |
All have sub-fields: context (ViewContextBase, #id) and name (string).
| Type | Value Type |
|---|---|
ViewVariableBool |
bool |
ViewVariableInt |
int |
ViewVariableFloat |
float |
ViewVariableString |
string |
All have sub-fields: context (ViewContextBase, #id) and name (string).
| Type | Parameter |
|---|---|
ViewEventVoid |
none |
ViewEventBool |
bool |
ViewEventInt |
int |
ViewEventFloat |
float |
ViewEventString |
string |