GithubHelp home page GithubHelp logo

vimaster / unity-databinding Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 1.0 1.45 MB

Bind UI elements using a global JSON blob as state

License: GNU General Public License v3.0

C# 100.00%
unity unity3d csharp

unity-databinding's Introduction

Unity Data Binding

This package allows data binding between components using a JSON blob called a Document.

alt text

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.

Examples

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

    AutomatedSubscriber.scene

    This scene contains a Document and, using an AutomatedSubscriber, automatically updates the position of a Unity-internal UI Text component.

    AutomatedSubscriber.cs

    This script listens to the specified Path on the Document and automatically updates all properties and fields of the TargetComponent, that are visible in the Unity Inspector. (In this case a RectTransform.)

    ArrowKeysHandler.cs

    This script listens to arrow key presses. Based on those, x and y coordinates inside the Document are set.

  • Handle Unity UI events

    EventGenerator.scene

    This scene uses an EventGenerator which makes interactive UI elements (like UnityEngine.UI.Buttons) automatically send their events to the Document for others to subscribe to.

    EventGenerator.cs

    This component scans its GameObject for any Component that can generate events. In this scene, each GameObject with a Button also has an EventGenerator, so that there's Left, Right, Up, Down objects with down and clicked events

    UIArrowEventSubscriber.cs

    This 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 in AutomatedSubscriber.scene

  • Subscribe and set values using C#

    ManualSubscriber.scene

    This scene contains a Document and manually sets and subscribes to changes inside a Document using C#.

    ManualSubscriber.cs

    This script listens to the specified Path and manually forwards them to the UI element to update it's position.

    ArrowKeysHandler.cs

    This script listens to arrow key presses. Based on those, x and y coordinates inside the Document are set.

  • Automatically generate and update Prefab instances using arrays

    Prefabs.scene

    This scene contains a Document and a PrefabGenerator.
    Based on the array at the specified Path, the PrefabGenerator automatically generates, updates or deletes instances of a Prefab.

    PrefabGenerator.cs

    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 ReflectedSubscribers are updated to point to their relative Path (i.e. UIElements[0], UIElements[1], UIElements[2], etc.)
    • 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

    UpdateCount.cs

    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.

  • Debugging

    Debugger.scene

    This scene contains a Document and a debugger which dumps the entire JSON Document to an on-screen label.

    DocumentDebugger.cs

    This script fills a UI element with the current entire Document and allows it to be copied to the clipboard.
    Toggle it's visibility with Ctrl+Shift+D.

    DebugDataGenerator.cs

    This script sets example data on the Document.

Vocabulary

  • A Document is a Unity component containing a JSON object:
    {
      "key1": "value1",
      "key2": [
                "value2",
                3
              ]
    }
  • A path or "document path" is a route to take inside the Document to find a value (i.e. key2[1] would be 3 in the example above)

Usage

  1. Add a DataBinding.Document to any GameObject to your scene
  2. Subscribe to a path inside that DataBinding.Document using either use DataBinding.Document.Subscribe() or a AutomatedSubscriber
  3. Set values on that path either using DataBinding.Document.Set() or using an EventGenerator pointed to that Document
  4. Notice the subscriber being informed about changes

unity-databinding's People

Contributors

vimaster avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

yumu908

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.