This package allows data binding between components using a JSON blob called a Document
.
The Document
can be updated via C# or automatically generated UI events and subscribed to to either via C# or automatically update any Unity MonoBehavior
/Component
.
Clone this repository and open any of the scenes inside Assets/Examples
.
(Use Ctrl+Shift+D
to see a debug view of the Document
!)
-
Update any component automatically
This scene contains a
Document
and, using anAutomatedSubscriber
, automatically updates the position of a Unity-internal UI Text component.This script listens to the specified
Path
on theDocument
and automatically updates all properties and fields of theTargetComponent
, that are visible in the Unity Inspector. (In this case a RectTransform.)This script listens to arrow key presses. Based on those,
x
andy
coordinates inside theDocument
are set. -
Handle Unity UI events
This scene uses an
EventGenerator
which makes interactive UI elements (likeUnityEngine.UI.Button
s) automatically send their events to theDocument
for others to subscribe to.This component scans its
GameObject
for anyComponent
that can generate events. In this scene, each GameObject with aButton
also has anEventGenerator
, so that there'sLeft
,Right
,Up
,Down
objects withdown
andclicked
eventsThis script subscribes to the buttons' events inside the
Document
(Right.events.down
,Left.events.down
, etc.) and - when pressed - changes the text's position via data binding like inAutomatedSubscriber.scene
-
Subscribe and set values using C#
This scene contains a
Document
and manually sets and subscribes to changes inside aDocument
using C#.This script listens to the specified
Path
and manually forwards them to the UI element to update it's position.This script listens to arrow key presses. Based on those,
x
andy
coordinates inside theDocument
are set. -
Automatically generate and update Prefab instances using arrays
This scene contains a
Document
and aPrefabGenerator
.
Based on the array at the specifiedPath
, thePrefabGenerator
automatically generates, updates or deletes instances of aPrefab
.This script subscribes to a path inside the
Document
containing an array.- If array elements are added, a new instance of the
Prefab
is generated- All
ReflectedSubscriber
s are updated to point to their relativePath
(i.e.UIElements[0]
,UIElements[1]
,UIElements[2]
, etc.)
- All
- If array elements are removed, the associated prefab instance is deleted
- If an array element's data changes, the associated prefab instance is updated too
Using the up and down arrow keys, new elements can be added or removed.
Using the left and right arrow keys, the position of the last element can be changed. - If array elements are added, a new instance of the
-
Debugging
This scene contains a
Document
and a debugger which dumps the entire JSONDocument
to an on-screen label.This script fills a UI element with the current entire
Document
and allows it to be copied to the clipboard.
Toggle it's visibility withCtrl
+Shift
+D
.This script sets example data on the
Document
.
- A
Document
is a Unity component containing aJSON object
:{ "key1": "value1", "key2": [ "value2", 3 ] }
- A
path
or "documentpath
" is a route to take inside theDocument
to find a value (i.e.key2[1]
would be3
in the example above)
- Add a
DataBinding.Document
to any GameObject to your scene - Subscribe to a path inside that
DataBinding.Document
using either useDataBinding.Document.Subscribe()
or aAutomatedSubscriber
- Set values on that path either using
DataBinding.Document.Set()
or using anEventGenerator
pointed to thatDocument
- Notice the subscriber being informed about changes