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
Documentand, using anAutomatedSubscriber, automatically updates the position of a Unity-internal UI Text component.This script listens to the specified
Pathon theDocumentand 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,
xandycoordinates inside theDocumentare set. -
Handle Unity UI events
This scene uses an
EventGeneratorwhich makes interactive UI elements (likeUnityEngine.UI.Buttons) automatically send their events to theDocumentfor others to subscribe to.This component scans its
GameObjectfor anyComponentthat can generate events. In this scene, each GameObject with aButtonalso has anEventGenerator, so that there'sLeft,Right,Up,Downobjects withdownandclickedeventsThis 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
Documentand manually sets and subscribes to changes inside aDocumentusing C#.This script listens to the specified
Pathand manually forwards them to the UI element to update it's position.This script listens to arrow key presses. Based on those,
xandycoordinates inside theDocumentare set. -
Automatically generate and update Prefab instances using arrays
This scene contains a
Documentand aPrefabGenerator.
Based on the array at the specifiedPath, thePrefabGeneratorautomatically generates, updates or deletes instances of aPrefab.This script subscribes to a path inside the
Documentcontaining an array.- If array elements are added, a new instance of the
Prefabis generated- All
ReflectedSubscribers 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
Documentand a debugger which dumps the entire JSONDocumentto an on-screen label.This script fills a UI element with the current entire
Documentand allows it to be copied to the clipboard.
Toggle it's visibility withCtrl+Shift+D.This script sets example data on the
Document.
- A
Documentis a Unity component containing aJSON object:{ "key1": "value1", "key2": [ "value2", 3 ] } - A
pathor "documentpath" is a route to take inside theDocumentto find a value (i.e.key2[1]would be3in the example above)
- Add a
DataBinding.Documentto any GameObject to your scene - Subscribe to a path inside that
DataBinding.Documentusing either useDataBinding.Document.Subscribe()or aAutomatedSubscriber - Set values on that path either using
DataBinding.Document.Set()or using anEventGeneratorpointed to thatDocument - Notice the subscriber being informed about changes
