Author Topic: Property Binding (data binding)  (Read 19920 times)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Property Binding (data binding)
« on: March 27, 2014, 07:05:39 AM »
Overview

Property Binding component lets you do basic data binding. Data binding is when you have one value automatically update some other value without having to write code.

NOTE: This feature was added in NGUI 3.5.6.



To use it, simply attach the Property Binding component to any game object. It's not UI-specific and will work just fine on any game object.

Once the script is attached, choose the Source object that contains the script with one of the values you want to bind, then the Target object that contains the script with the target value you wish to bind it to. Choose the appropriate property or field from the drop-down list that will appear underneath each object, and that's all it takes to bind two values.

You can change which value updates the other by modifying the Direction, and you can change when it gets updated using the Update field. When working with a script that should not be updated while outside of Play mode you might want to turn the Update in Edit Mode off.

Pro-Tip

Under the hood the Property Binding script is actually quite short. It simply has two public PropertyReference fields and it sets one's value based on the other's value. You can use PropertyReferences yourself freely in your own script. For example this small script will update one property using the value of another:
  1. using UnityEngine;
  2.  
  3. [ExecuteInEditMode]
  4. public class Test : MonoBehaviour
  5. {
  6.         public PropertyReference myProperty;
  7.         public PropertyReference secondProperty;
  8.  
  9.         void Update ()
  10.         {
  11.                 myProperty.Set(secondProperty.Get());
  12.         }
  13. }

Video

http://www.youtube.com/watch?v=4ufvpR6HHp4

Class Documentation

http://tasharen.com/ngui/docs/class_property_binding.html
http://tasharen.com/ngui/docs/class_property_reference.html

If you have a question regarding this component or would like me to clarify something, just post a reply here.
« Last Edit: March 27, 2014, 07:13:55 AM by ArenMook »

coeing

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 11
    • View Profile
    • slash games
Re: Property Binding (data binding)
« Reply #1 on: March 31, 2014, 10:14:49 AM »
Great new feature, thanks! :) Looking forward how it will evolve!

Currently I am using NData (http://forum.unity3d.com/threads/127918-NData-MVVM-framework-for-NGUI) to do this job, you probably already know it. But an integrated solution sounds much better.

In the end I hope this will properly divide the view itself from the view logic as in the MVVM pattern. Do you have already any plans where the feature should go?
Visit slash games at http://www.slashgames.org

Indie Game Developer from Hamburg, Germany.

vikti

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 25
    • View Profile
Re: Property Binding (data binding)
« Reply #2 on: January 21, 2015, 05:30:50 PM »
Is there a way to bind to/from a non monobehaviour model property ?


coeing

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 11
    • View Profile
    • slash games
Re: Property Binding (data binding)
« Reply #3 on: January 22, 2015, 03:16:24 AM »
Hi @vikti,

I just developed a little asset for the Unity Asset Store to handle the data binding between the (game) logic and the presentation (e.g. the UI) side. It should be available in the Store soon, here's a bit of documentation: http://slashgames.org:8090/display/DBFU It works with NGUI and Unity UI as well. Here's the not yet updated Unity forum thread: http://forum.unity3d.com/threads/data-bind-for-unity.283252/

If it looks interesting for you, let me know, I could send you a free version to test with. I'm currently looking for feedback from other developers to check if my tool contains everything it needs.

Cheers
Christian
Visit slash games at http://www.slashgames.org

Indie Game Developer from Hamburg, Germany.

vikti

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 25
    • View Profile
Re: Property Binding (data binding)
« Reply #4 on: January 27, 2015, 05:14:54 PM »
Thx Coeing, looks like interesting, but I prefer to keep focus on my actual framework (StrangeIoC).

I have question for ArenMook : is it worth to worry about performance impact of PropertyReference since it uses reflection ? How many propertyref could we use at once ?

Thx,

coeing

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 11
    • View Profile
    • slash games
Re: Property Binding (data binding)
« Reply #5 on: January 27, 2015, 05:52:12 PM »
Thx Coeing, looks like interesting, but I prefer to keep focus on my actual framework (StrangeIoC).

Alright, no problem :) If you have some hints what definitively shouldn't miss in a data binding asset (i.e. what you really like about StrangeIoC), I'm always open for feedback.
Visit slash games at http://www.slashgames.org

Indie Game Developer from Hamburg, Germany.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Property Binding (data binding)
« Reply #6 on: January 28, 2015, 01:46:55 AM »
I have question for ArenMook : is it worth to worry about performance impact of PropertyReference since it uses reflection ? How many propertyref could we use at once ?
Well, reflection-based approach is always going to be slower than accessing the value directly, but I highly doubt that you will notice any slowdown coming from extensive use of reflection.