Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

NData - MVVM framework for NGUI

Discussion in 'Assets and Asset Store' started by Art Of Bytes, Mar 15, 2012.

  1. duke

    duke

    Joined:
    Jan 10, 2007
    Posts:
    763
    I've been doing a bit of this for other purposes. What you'd want to do is use a code generation tool like T4 templates or Razor to create a "ViewModelNameBinding.cs" file, that directly references and links the viewmodel to its bindings, using NData's own binding resolution, but at build time rather than runtime.

    Making use of the Editor vs Runtime divide is a greatly underutilized advantage, IMO.
     
  2. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,459
    Duke,

    Very nice idea. can you elaborate a bit more.
     
  3. duke

    duke

    Joined:
    Jan 10, 2007
    Posts:
    763
    Actually it would be better to do it the other way around. Create a class that you put on your UI root, with a reference to your root viewmodel. Store references to all the bindings in the UI tree, and manually plug them into the properties OnChange handler. This would be a one off throw away class that would be overridden as you change bindings and properties.

    You'd also have to monkey patch the NData stuff with some compiler directive so that it a) disables its binding components on build, and b) doesn't attempt to do any other reflective business. Basically see what breaks and patch it ;)
     
  4. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,459
    @Duke: Interesting approach.
    @ArtOfBytes: Do you think that this approach could work?
     
  5. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Well, the current approach that we're trying is to have an alternative way of resolving the bindings path. Instead of finding properties via reflection, they are looked up from the dictionary, that is filled in the context constructor. So far it seems to work and allows to keep the scenes untouched. The only change is that you have to register your properties, variable contexts and commands in the constructor of your context. Change is minor, but that's where some code generation done by editor script could be useful. Anyways, despite we already have some samples working as swf-s, there are problems. Mostly because c# types get converted to a smaller set of action script equivalents and some bindings get messed up. So, you can expect for experimental flash support in some not very far future. But flash is ... well, it'll take some time till its perfect.
     
  6. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,459
    This is very promising news. Great work. I was helping unity LeanTween's author debuging the flash output from that project. It's quite interesting that the javascript version exported quite nicely whereas the C# version had problems. Perhaps Unity's Flash export compiler takes care of more details when it's Javascript. This might be a possible solution when exporting to flash, make the classes that are giving problems in JS and see if Unity's compiler works better.
     
  7. Steve-Tack

    Steve-Tack

    Joined:
    Mar 12, 2013
    Posts:
    1,240
    Can you elaborate on what doesn't work in iOS with NData? I'm in the process of learning Unity and it didn't take me long to figure out how awesome NGUI was, so I bought that and shortly after, I bought NData. I haven't done much with NData yet, but I may want to target iOS at some point for my games, so it'd be good to know about any gotchas ahead of time. Thanks!
     
  8. duke

    duke

    Joined:
    Jan 10, 2007
    Posts:
    763
    The bridge between your view (bindings) and view model (properties) is done via reflection. So the bridge itself either doesn't work at all or is missing functionality.
     
  9. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,459
    We did a few more tests with making LeanTween package work with Flash and found something very interesting that might be useful in making nData work for flash.

    Please have a look at these two files, one is JS and the other is CS equivalent.

    1. LeanTween.js https://dl.dropbox.com/u/48378123/LeanTween.js
    2. LeanTween.cs https://dl.dropbox.com/u/48378123/LeanTween.cs

    LeanTween.js successfully compiled for Flash whereas the .cs version does not because it uses reflection.

    The example project is here: https://www.dropbox.com/s/bh4qgbzo0w...TweenFlash.zip

    The forum thread for this topic is :
    http://forum.unity3d.com/threads/16...ompeting-engines!/page4?p=1211764#post1211764

    The CS version has to directly call the Reflection package whereas for some reason the exported flash code generated from the JS version escaped the Reflection problem. A possible workaround the reflection problem could be to use JS code when exporting for flash and CS code for other platforms. There is definitely some magic that the unity's compiler is doing behind the scenes.

    Also NGUI author is an expert on Flash export. http://forum.unity3d.com/threads/114...request/page17
    Perhaps you can ask him directly.
     
    Last edited: Apr 6, 2013
  10. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,459
    Can you send me a link to a working version of NDATA for unity 4.1.2f , I'm still getting the same errors for version 1.0.11

    I started a fresh project, imported in NGUI and then NDATA. Just did this 10 minutes ago.

    Here's the screen shot.
    https://dl.dropbox.com/u/48378123/NdataProblem.tiff
     
  11. Deleted User

    Deleted User

    Guest

    Hi @AOB.

    Having an issue with selected item binding to a label on a different panel than the Ndata Root object.

    For instance I have a scroll items list (sits ontop of a panel using a draggable camera like in the example) being populated fine by a collection, when i click a button in the items list id like it to change the label on the panel that the scroll view sits on top of. This panel doesnt currently have any other bindings. I have added the following NDATA text binding to the label but it wont update the label

    myCollectionName.SelectedItem.itemName

    Am I doing something wrong?
     
  12. duke

    duke

    Joined:
    Jan 10, 2007
    Posts:
    763
    That seems right. It doesn't help that th validate bindings tools doesn't seem to work. I assume itemName is an actual property on the item, and that myCollectionName has a selected item? It's possible that your collection doesn't have or isn't registering that the collection has a selection. Try binding a label to myCollectionName.HasSelection to debug it.
     
  13. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Version 1.0.12 is on the way, we've already uploaded it to asset store (on Tuesday to be exact) so it will be available shortly via AssetStore. Should be a matter of day or two.
     
  14. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    NData if fully functional on iOS. There are some specific features in reflection library that are not supported on iOS (like Eval, or any other run-time code generation). But we don't use any of those unsupported features. So, no worries. iOS builds will have a complete NData functionality available.
     
  15. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Current solution we're working on doesn't use reflection at all, so not likely we'll need a workaround like that. But anyways, that possible ways through java-script could be useful when there's no other way around. Thanks for the hint! Right now, we need to fix couple of other side effects caused by translation to action script. Then the whole work about supporting new stuff in bindings validation utility and maybe code generation for flash-specific auxiliary code.
     
  16. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Hi Tommy. As duke correctly suggest, you should check two things:
    First is if binding path is correct. For that bindings validation tool can be used. Select your label in the hierarchy and hit Alt-V it'll print any detected errors to the console. Also, for a quick check you can replace the binding path to myCollectionName.0.itemName - it'll bind label text to the name of first item in collection.
    Second thing is that maybe item selection doesn't happen. To check that you can try to bind something to myCollectionName.HasSelection or call myCollectionName.SelectItem(0) after adding items to it to force selection of the first item and see if it helps.
    Note, that for items to be selectable, they have to contain a collider in the root of an item template.
     
  17. Deleted User

    Deleted User

    Guest

    When I tried the alt V i get the following error in the console

    Failed to resolve node in binding myCollectionName.SelectedItem.itemName in object UIRoot (2d).Camera.Anchor.Panel.lblSelectedIemName
    [context type is root], error at myCollectionName
     
  18. Nezabyte

    Nezabyte

    Joined:
    Aug 21, 2012
    Posts:
    110
    I've been following the tutorials for NData and set up the template for ViewModel. I noticed the demo includes a game object for Model and a reference to it from the ViewModel game object. However, I didn't see any tutorials to set up Model, and the provided ViewModel template snippet didn't have anything for it. Is Model optional? Are there any tutorials for that? I wanted to understand how everything goes together.
     
  19. soulis6

    soulis6

    Joined:
    Apr 21, 2011
    Posts:
    22
    That would be great thanks, spent a while trying to figure out the problem but no luck. All the bindings are right and they're definitely not invisible, they're not being created. It appears that OnItemInsert in ItemSourceBinding isn't being called on the prefab object (the Headline Item in this case) after it's been instantiated, but I couldn't quite figure out why that was.
     
  20. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    It means that class root doesn't contain collection with name myCollectionName. Could you show the code of your ViewModel?
     
  21. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Hi, Model layer can me omitted if your game logic is relatively simple (it's more like it's being dissolved in ViewModel in such a case). It is up to you whether to add Model or not and how ViewModel and Model communicate, we don't enforce any particular architecture. The new update (version 1.0.12) contains an example of a small game without a model. We'll add more examples like that with a possible architectures of a game.
     
  22. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    We're preparing the next batch of improvements for submission to asset store right now. There will be changes in tables refresh, but if you say that sub-items are not added to the hierarchy at all, it might be something different. Maybe you can PM some test scene, so we can look into it?
     
  23. Deleted User

    Deleted User

    Guest

    I know the name and spelling is correct as it populates the scroll list just fine within its own UI is correct. Perhaps I can explain my setup better. Im thinking it has something to do with a missed reference. Im still learning unity coming from Objective C so I thought that my collection would be a sort of global scene variable.

    what I have are two UI roots just liek in the NGUI example scroll view

    the first one is my scroll view root, which has all the Ndata root objects and bindings and my data model script attached. works great

    the second one is a larger UI that the first UI camera is drawn inside to make the scroll view just like in the NGUI example (using drag camera). The second UI root has NO other NDATA references or components, scripts or otherwise, other than the one label text binding at which im attempting. Im thinking that there needs to be a reference here to this collection since it sits outside of the root where all the NDATA root bindings are. Im just not sure how to do that. Or if that is the case but i'm guessing that is it.

    It would be the equivalent of the example on your website for items when you click on an item in the scroll with to changing the label of the main UI root outside the UIroot that contains all the data bindings.

    Pardon my lake of knowledge on this im going by how I understand things in objective c. Unity is quite a culture shock coming from that. I hope this explains better what my setup is and what im trying to do.
     
  24. Deleted User

    Deleted User

    Guest

    Interestingly enough I just got this to work by dragging the camera from my main top level UI (the one not containing the scroll view) and putting it under the UIRoot where the scroll view exists. It has seem to create some other issues now with my scroll view not lining up properly any more but object clicking does work. Im kind of confused now. I build my gui using NGU's example 8 as a guide which has 2 separate UIroots, one for the scroll view and one for the main view.

    Im at a loss at this point how I should continue. Should I try and fix the positioning of my scroll view and have everything under the one UI root or should I try to get this to work like in the NGUI example using 2 UI roots and keeping these separate?
     
  25. duke

    duke

    Joined:
    Jan 10, 2007
    Posts:
    763
    I swear this worked before I updated NData this morning. Visibility binding on a panel doesn't work.
     
  26. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051

    I just had this happen after I updated HarpNinja to the latest NGui and NData. One of my panels that has 2 popul lists and a label with a Vis binding on the panel kept showing one of the popuplists on scene startup, but the others were properly hidden.
    This worked fine prior to update.

    I don't have time to fully debug it so I just duplicated the visibility binding component from the parent panel onto the Popuplist that had the issue and it works. Quick fix but there must be a bug somewhere methinks.
     
  27. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Hi, problem is most likely is that only one of the UI roots has the "NguiRootContext" component. First of all your viewmodel should look like this, notice that array of views is present here instead of a single view. And all of that views get the same context:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class Ui : EZData.Context
    5. {
    6.     // ...
    7. }
    8.  
    9. public class ViewModel : MonoBehaviour
    10. {
    11.     public NguiRootContext[] Views;
    12.     public Ui Context;
    13.  
    14.     void Awake ()
    15.     {
    16.         Context = new Ui ();
    17.         foreach (var view in Views)
    18.             view.SetContext (Context);
    19.     }
    20. }
    21.  
    When viewmodel is ready, make sure all of your views that you want to bind to this viewModel have NguiRootContext component in them and are added to the views array in viewmodel game object. Example setup, if you are using Ngui - Example 8, would be like that:

    $MultipleViewsSetup.png
     
  28. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Working on this, it must be some side-effect of the latest visibility re-factoring. So, it happened only to visibility bindings added to the UIPanel-s and visibility of all/some contents of this panel is not affected. Is that correct?
     
  29. Deleted User

    Deleted User

    Guest

    Thanks AOB! That did the trick. I even got a pop up list binding working.
     
    Last edited by a moderator: Apr 12, 2013
  30. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
    So far I only picked up the visibility binding issue
     
  31. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Hi all :)

    Please, welcome our latest NData updates. Big improvements pack in 1.0.12 and couple of smaller features and fixes in 1.0.13. Details are here: http://tools.artofbytes.com/ndata/release-notes. Besides the original high scores screen demo, there are 3 new examples demonstrating separate features (NestedTables, PersistentProperties and VariableContexts). And also simple game example demonstrating a possible structure of a full project.

    2 Nyxo : 1.0.13 version has support for self-referenced item templates and better items repositioning code (also comes with an example scene NestedTables)
    2 duke and sonicvis : visibility issues should be also gone with 1.0.13 version
     
  32. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
    A1. Thanks for the great support and product.
     
  33. Nezabyte

    Nezabyte

    Joined:
    Aug 21, 2012
    Posts:
    110
    Thanks for the updates and new examples (really awesome reference material). Keep up the great work!
     
  34. Chargo

    Chargo

    Joined:
    Apr 16, 2013
    Posts:
    2
    About EZData.Collection

    Hi,

    Just new to this plug-in and have some questions about EZData.Collection:

    1. What is the best way to implement sorting to this kind of container?
    The trivial way first came to my mind is to use my own container to sort all the data, and copy the content to a EZData.Collection with NguiItemsSourceBinding iteratively to make the UI reflects the changes. However, this also makes all the gameObjects in UITable being destroyed and re-instantiated, which is very very slow.....Is there a better way, or did I miss something?

    2. public void Remove(T item) seems not to work
    I have tried implementing interfaces in my own class, like IEquatable, IEqualityComparer, IComparable, or overloading == and != operators, but none of them makes Remove(T item) in EZData.Collection to compare and all the item in it with the comparison methods I defined.
    For example, I have a class named "HeroInformation" and I'd like them to compare the "Name" property while using ==. The desired behavior is when I pass a "HeroInformation hero" with hero.Name = "ToBeKilled" to myCollection.Remove(hero), the item in the container with Name "ToBeKilled"will be removed, but nothing is removed actually...

    Thanks in advance :D
     
    Last edited: Apr 16, 2013
  35. Chargo

    Chargo

    Joined:
    Apr 16, 2013
    Posts:
    2
    Anybody help? :D

     
  36. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Hi, Chargo, sorry for late reply, notification about your first message must have been lost somehow.
    Anyways, regarding to your questions:
    1. At the moment, solution you described is not optimal, but OK. Right now observable collections don't support notifications about items re-ordering. That's scheduled in our to-do list, will be available in one of the next updates.
    2. Remove definitely works if the argument is reference equals to one of the items, so that something like this will work:
    Code (csharp):
    1.  
    2. var myItem = new Item();
    3. myCollection.Add(myItem);
    4. // and then later
    5. myCollection.Remove(myItem);
    6.  
    However, we'll check why overloading == and != doesn't work as expected, at first look it should. Also, in terms of extending collection interfaces, we're planning to add the Remove function that takes a predicate and can remove multiple items at once. Quick solution may look like this added to Collection<T> class in NData/Core/Collection.cs file:
    Code (csharp):
    1.  
    2.         public void Remove(System.Func<T, bool> predicate)
    3.         {
    4.             var toDelete = new List<T>();
    5.             foreach(var item in Items)
    6.             {
    7.                 if (predicate(item))
    8.                     toDelete.Add(item);
    9.             }
    10.             foreach(var item in toDelete)
    11.             {
    12.                 Remove(item);
    13.             }
    14.         }
    15.  
    then you'll be able to remove items by their content like this: myCollection.Remove((hero) => hero.Name == "ToBeKilled");
     
  37. Nezabyte

    Nezabyte

    Joined:
    Aug 21, 2012
    Posts:
    110
    What would be the best way to approach an inventory system? I want to do something similar to NGUI Example X - Character, which has a 3D character and items (clothing) to add to the model. For example, I have a EZData.Collection of ClothingItem (each ClothingItem represents an instance of class Clothing, exposing just the Name and Image variables), and I want to be able to call command PutOnClothing(). Depending on the clothing item selected (either using OnClick or OnDrop binding ... I wanted to start with OnClick for quick testing but eventually be able to drag items to the 3D model), it would pass information for the particular instance of class Clothing (includes Name, Image, and everything else in the class).
     
  38. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    The straightforward approach you described would work just fine. If you break down the NGUI Example X - Character, you will notice that the data that describes the whole scene is the container of items in a backpack and three slots that can contain same items as backpack does. So the context of a character would look like:
    Code (csharp):
    1.  
    2. class ClothingItem
    3. {
    4.   Property Name;
    5.   Property Slot;
    6.   Property Icon;
    7. }
    8.  
    9. class Character : Context
    10. {
    11.   Collection<ClothingItem> Backpack;
    12.   VariableContext<ClothingItem> Shoulders;
    13.   VariableContext<ClothingItem> Bracers;
    14.   VariableContext<ClothingItem> Boots;
    15. }
    16.  
    Then, you just add items to the character and move them around between backpack and slots as you wish. This requirements add some action and a bit of game logic to the whole setup, so it evolves to something like this:
    Code (csharp):
    1.  
    2. class ClothingItem
    3. {
    4.   Property Name;
    5.   Property Slot;
    6.   Property Icon;
    7.   event Action<ClothingItem> OnToggle;
    8.   void Toggle() { OnToggle(this); }
    9. }
    10.  
    11. class Character : Context
    12. {
    13.   Collection<ClothingItem> Backpack;
    14.   VariableContext<ClothingItem> Shoulders;
    15.   VariableContext<ClothingItem> Bracers;
    16.   VariableContext<ClothingItem> Boots;
    17.  
    18.   void Add(ClothingItem item)
    19.   {
    20.     Backpack.Add(item);
    21.     item.OnToggle += Toggle;
    22.   }
    23.   void Remove(ClothingItem item)
    24.   {
    25.     Unequip(item);
    26.     Backpack.Remove(item);
    27.     item.OnToggle -= Toggle;
    28.   }
    29.   void Toggle(ClothingItem item)
    30.   {
    31.     if (IsEquipped(item))
    32.       Unequip(item);
    33.     else
    34.       Equip(item);
    35.   }
    36.   bool IsEquipped(ClothingItem item)
    37.   {
    38.     // TODO: return true if item is a value of any slot
    39.   }
    40.   void Equip(ClothingItem item)
    41.   {
    42.     // TODO: remove item from backpack and set as a value of appropriate slot
    43.   }
    44.   void Unequip(ClothingItem item)
    45.   {
    46.     // TODO: clear slot taken by the item and put item to backpack
    47.   }
    48. }
    49.  
    You can go even further and think of your character as a view, add NguiRootContext component to its root game object. Then extend data contexts with references to an actual clothes' prefabs and bind to them in a custom ClothesModelBinding, so that clothes will be instantiated and added to the character automatically as you move your ClothingItem's around between collections and variable contexts. It maybe a little bit too advanced, so this approach will be covered in an example, meanwhile, here's the preview of this example with clothing items in UI: http://tools.artofbytes.com/wp-content/uploads/2013/04/NDataInventoryExample.unitypackage
     
  39. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,459
    Thanks so much for the Inventory Example.

    This is really the Best way to Learn and Show Off NData's powerful features.

    Please add more examples. Fantastic.
     
  40. chuk2bp

    chuk2bp

    Joined:
    Jan 30, 2013
    Posts:
    5
    Hey, loving the plugin so far, solid work. However, I'm having a bit of difficulty with the Ngui Local Scale Binding. The idea is this:

    Say we have a Player Health Bar (which is a simple tileable sprite) that has an x value between 0 to 100 (for the sake of simplicity). I had assumed that I could use the Local Scale Binding on the tileable sprite to a Vector 3 property that would adjust the X value as necessary... but it doesn't seem to do anything at all?

    Would you mind giving me a brief explanation of what is necessary in order to get this binding to scale the sprite? Thanks a lot!
     
  41. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Hi, that unpleasant issue occurs because fields in Vector3 do not notify about their change, but only the whole property itself triggers a UI update when changes. Code sample:
    Code (csharp):
    1.  
    2.     #region Property MyVector
    3.     private readonly EZData.Property<Vector3> _privateMyVectorProperty = new EZData.Property<Vector3>();
    4.     public EZData.Property<Vector3> MyVectorProperty { get { return _privateMyVectorProperty; } }
    5.     public Vector3 MyVector
    6.     {
    7.     get    { return MyVectorProperty.GetValue();    }
    8.     set    { MyVectorProperty.SetValue(value); }
    9.     }
    10.     #endregion
    11. ...
    12.     MyVector.x = 10;  // This will do nothing, because MyVector setter will not be invoked
    13. ...
    14.     var v = MyVector;
    15.     v.x = 10;
    16.     MyVector = v; // This will trigger an UI update
    17.  
    Also, if you want to change only one component in transform and don't want to have the whole Vector3 for that, this new binding in attachment might be useful.
     

    Attached Files:

  42. chuk2bp

    chuk2bp

    Joined:
    Jan 30, 2013
    Posts:
    5
    Thanks alot, that new binding did the trick quite nicely.

    If it has not already been done so, I'd like to submit a formal request that you guys update your Documentation to reflect at least some of the more integral bindings that you've added over recent updates. Additionally, it would be VERY useful if you guys provided a sort of glossary to all the bindings you've created up to date and provide write ups about the general functionality of each binding.

    I think this would really help new users understand the full power of this asset. It would also be useful for reference for developers, as they could have that glossary up for reference while working in Unity.

    All suggestions aside though, thanks a lot for the help. Keep up the good work guys.
     
  43. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,459
    Keep up the Excellent work. Very nice.

    Also would like to +1 on the Glossary or a single source of Updated Docs would be very Helpful.

    Cheers.
     
  44. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,459
    Question: Is it possible to somehow create databindings of objects in-game ?

    For example: I have an XML that describes all of the NGUI elements for a scene. My code then instantiates all of the NGUI elements dynamically. I would like to use Ndata to create the bindings for these NGUI elements.

    Can Ndata support this type of dynamic data binding setup in-game ?


    Thanks.
     
    Last edited: Apr 28, 2013
  45. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Documentation is on the way, its updates indeed are lagging behind the actual feature releases. We're going to make it more compact and more informative.

    Regarding this issue:

    That's totally possible. The only tricky note about that approach is the bindings' paths resolution moment. It happens in the Start method, so if you instantiate an object with binding components from a prefab and then assign its transform parent, bindings' path resolution will fail, because the newly created bindings do not have a parent data context yet. To handle that situation, you'll need to call UpdateBinding method in all newly instantiated bindings after assigning a parent.

    If you are planning to instantiate binding components by calling AddComponent on the object that already exist on the right place in the hierarchy, then it should work without any additional actions from your side.

    Also note that NguiRootContext should exist on the root of your UI hierarchy before you start spawning bindings.
     
  46. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,459
    If I understand correctly, it's possible to hook up the bindings with prefab objects that already have bindings.

    Is it possible to Define the bindings in the XML. This means to define and create the bindings objects at runtime and not in the editor.
     
  47. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Well, prefabs with bindings can be seen in action when items source bindings fills the table or grid with items created from item templates' prefabs. So, once you get your UI objects into the scene to the intended place in the hierarchy, and they resolve bindings path successfully - they will work, no matter how exactly you create them (by instantiating a prefab, creating components from code or having everything predefined in a scene).

    Regarding to the XML defined bindings - in our projects we usually have UI with all data bindings as a part of a scene, it is an appropriate place for it. For a typical game project having an extra data format for UI definition and instantiating it dynamically would be redundant. And if you have UI structure defined in a scene and bindings defined elsewhere - then it's not a good idea, we really wouldn't recommend it, maintenance cost of such UI may be too high.

    However if you already have some format that describes your UI in XML form and have tools that instantiate it on the fly, then you can extend it to support bindings too, all bindings are the regular components, so if you can create UILabel dynamically, you will be able to create NguiTextBinding next to it too.
     
  48. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,459
    If I can rephrase my question, it would be: Is there a way to Dynamically create or define the ViewController using XML instead of hardcoding the binding paths ? For example, Adobe Flex uses databinding heavily and there are projects that create the entire GUI scene and the bindings from XML. Ndata is still relatively new and I wanted to request to the developers the option of being able to dynamically create the ViewController.
     
    Last edited: May 1, 2013
  49. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Right now, we're not planning to support alternative way of defining the UI. It'll require supporting the whole NGUI feature set + NData feature set in some sort of XAML-like format - quite a work to do. So, that feature is, for sure, not for the nearest future. Also, native Unity UI will be released eventually, it may affect the plans too.
     
  50. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,459
    Double Binding to NGUI alone would probably not be a good idea as you describe, however a more generalized data binding framework that is independent could be used for any sort of game object. In the latest section of unity store is a new asset "Get Set Value" that seems to do data binding to any object and possible dynamically.