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. Art Of Bytes

    Art Of Bytes

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

    We have published new framework for NGUI, that allows to significantly improve ui development efficiency thanks to independent logic implementation and automatic data binding.

    NData in AssetStore

    General approach explanation, overviews, feature list and other useful info can be found on our website

    Simple tutorial represents step-by-step NData usage instructions. You can see that it is easy to integrate and use, but allows to create UI for projects of any types and complexity.

    Documentation describes possible data binding, gives some hints and recommendations.

    We have been using MVVM pattern in our project from the very beginning and like it very much. Hopefully, you'll enjoy it too :)

    If something is not clear in pattern, framework, approach, documentation, you have questions or comments, please, do not hesitate to ask. Your feedback is the most important source of understanding if we are doing the right things. Post it here, in this post, or via website contact form

    Looking forward to your first reports!

    Yours,
    Art Of Bytes Team
     
    Last edited: May 23, 2012
  2. jeffweber

    jeffweber

    Joined:
    Dec 17, 2009
    Posts:
    616
    Hmm, looks interesting. I use MVVM quite a bit for my "real" job.
     
  3. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Pretty cool approach, isn't it? ;)
     
  4. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
    Hi,
    I just bought this as I'm looking at creating a cleaner data/view separation in an app (so I can move code to external assemblies) I have in development.

    I have some initial feedback while I go through it.

    1. I realise you ported this over from your EZData solution (I have EZGui, but I'm using NGui atm) but it is confusing to See EZData sprinkled throughout the sample, as in public class HighScoreTableItem : EZData.Context.etc
    I understand data bindings / beans etc from my java days, but I'm still finding your documentation heavy going to understand.

    2. I think your example scene would be better if it was a more dynamic example of MVVC applied to games. The High scores table is really just akin to the functionality of a business app and does not really demonstrate whether MVVC is a solution for game design at all.
    The reason why is discussed here in detail and at the moment I still can't see how it would be applied to a complex dynamic gui (such as the one I am currently working on).

    ty!
     
  5. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Hi sonicviz!

    Than you very much for purchase and your feedback! Points are reasonable, let us try to highlight them.

    1. NData was ported from EZData, indeed. That's why EZData.dll and namespace have that name, while in fact they are independent of any UI library. We're considering renaming the core namespace to something more generic, once we find a way to do it in a most seamless way for the people who have already got some code based on previous versions of our libraries.

    2. That's a good point, we believe that design patterns like MVC and MVVM usually used in applications, if used correctly, can be very useful in games of any complexity as well. That's why we decided to publish this tool in the first place, initially for EZGui and now for NGUI.
    We have been implementing game with complex data and interface structure and MVVM turned out to be absolutely 100% what we need in our project.

    It became clear that using some kind of 'enterprise' pattern is must for the large games. If you have anything like loot / inventory panels, character customization, trees of skills, etc., trying to manage it with a regular "let's-get-all-code-into-a-single-huge-ball-of-everything" pattern will get you to the troubles even before release date of your project, not to speak of the further support and extensions.

    Regarding complexity of documentation, treat it more like a reference. Probably make sense to get started with simple tutorial first (http://tools.artofbytes.com/ndata/simple-tutorial). Once you get the pattern, it'll be easy to extend your model and bind UI elements to the fields of your model.

    We would totally like to make something like a small but completely finished game as a demo. Meanwhile, we're working on more tutorials that cover more use-cases, and eventually one of them can be this game.

    If you have any other questions or comments about NData usage, integration or implementation, we'll be happy to help!
     
  6. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
    kk, I'll work through your simple tutorial and see where it goes.

    ty!
     
  7. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Cool, let us know how helpful and useful it is.
     
  8. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
    ok, the tutorial is a much better introduction. The tips on monodevelop templates are great too.

    Now to take the basic tutorial one step further I want to implement a model that modulates the slider volume, but I'm not seeing how to access the TutorialUI.volume property from the ViewModel.

    Here's my sample model:
    using UnityEngine;
    using System.Collections;

    public class TutorialModel : MonoBehaviour {

    public ViewModel view;
    // Use this for initialization
    void Start () {
    view = GameObject.Find( "TutorialViewModel" ).GetComponent<ViewModel>();
    }

    // Update is called once per frame
    void Update () {
    float sliderValue = (Mathf.Sin(Time.time) * 50) + 50f;

    [insert update to vewmodel tutorialui.volume here..how?]
    }
    }

    How do I do that?

    Cheers,
    sv
     
  9. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
    Also, I don't see if NData supports NGui popup list? How do I do that? (I have a number of popup lists that need to be populated at scene initialisation)

    ty!
     
  10. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Good point, thank you for noticing this important place for tutorial improvement!
    Use the following snippet for ViewModel creation, and use Context to access all properties you need:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class $game$Ui : EZData.Context
    5. {
    6.  //TODO: add your dependency properties and collections here
    7. }
    8.  
    9. public class ViewModel : MonoBehaviour
    10. {
    11.  public NguiRootContext View;
    12.  public $game$Ui Context;
    13.  
    14.  void Awake()
    15.  {
    16.   Context = new $game$Ui();
    17.   View.SetContext(Context);
    18.  }
    19. }
    20. $end$
    21.  
    We've updated Tutorial as well - http://tools.artofbytes.com/ndata/simple-tutorial
     
  11. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
    ok, that works fine. I suggest you expand the tutorial to include a full example of bi-directional control using this.
    For example, the checkbox could hide the slider, the button could stop the model cycling the slider update and let the user play with it.
    Developers need to see a full working round trip example to easily grasp the concepts and I think this would be a good basis.

    The demo included also looks a little more complex in how the model accesses the viewmodel, but I guess it has to be if the viewmodel also needs access to the model in order too update model properties based on user interaction.

    ty.
     
  12. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Thank you, we'll definitely take your suggestions into account in our next tutorial, that is coming soon
     
    Last edited: Mar 18, 2012
  13. duke

    duke

    Joined:
    Jan 10, 2007
    Posts:
    763
    An inventory example wouldn't be a bad idea. Also, I'm totally buying this when I get home - I loves me some mvvm wpf'y separation of concerns goodness!
     
  14. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Yes, inventory would be good Item Source Binding usage example. We'll try to add it next week.

    We also loved MVVM after some experience with WPF, and decided to bring it to our Unity project as well :)

    Looking forward to your feedback!
     
  15. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
    *bump* Any word on if this works with NGui popup list and/or how to do it? ty!
     
  16. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    sonicviz, we are investigating this question, and will answer here as soon as possible
     
  17. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
    That would be great, ty!
     
  18. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    sonicviz, we'll include NGUI popup list binding to the next NGUI update that is going to be published this week. I'll notify here, when it is accepted by Asset Store Team
     
  19. Art Of Bytes

    Art Of Bytes

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

    We've just added new tutorial, that based on NGUI Tutorial and shows how to get the same UI behavior without writing a single line of code for events handling – everything is done automatically thanks to Visibility and Checked bindings!

    http://tools.artofbytes.com/ndata/tutorials/checkbox-tutorial

    If you didn't got through our Basic Tutorial, probably that makes sense to look at it first

    We hope that our tutorials are helpful and allow to show benefits of NData with really easy usage.

    Looking forward to your feedback!
     
  20. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
  21. Lord_Pall

    Lord_Pall

    Joined:
    Sep 25, 2009
    Posts:
    52
    Does this work in flash?
     
  22. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Hi,
    Sorry to say that, but no, NData doesn't support Flash currently. Probably, Flash support will be added later
     
  23. matix

    matix

    Joined:
    May 4, 2012
    Posts:
    4
    Hallo,
    I need a NguiUISpriteBinding. to Set or Change a Sprite Texture from a Atlas.
    are you planning on implementing this feature in the near future?
     
  24. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Hi matix,

    Currently you can only bind UITexture to Texture2D property.
    Do you need to have some, let's say, string property, where you put a name of a sprite and all UISprite components bound to this property get updated automatically? Have we understood you right?
     
  25. matix

    matix

    Joined:
    May 4, 2012
    Posts:
    4
    i have a NguiAtlas(UIAtlas) with some Textures. I created some Items with UITable, NguiListItemTemplate and NguiItemsSourceBinding at runtime. My Tempate has a Label and a Sprite (not UITexture) and i will set the UISprite.Sprite from the Atlas.
    someting like in Ngui Example 3 - Character.
    http://www.tasharen.com/ngui/example3.html
     
  26. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    matix, we've added this feature in our task list, it will be available in the next update, quite soon.
    We'll notify here when it's ready.
     
  27. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    NData new update is available in AssetStore - http://u3d.as/content/art-of-bytes/ndata-for-ngui/2KC
    The following changes and features are included:
    • IEnumerable support for collections to allow foreach for items
    • Binding to collection items by index (allowing binding paths like: HighScores.Table.0.Name)
    • Slight context access level modification for possible iOS issues
    • Better handling of unresolved bindings
    • Indexed bindings validation
    • Sprite binding support
    • Minor code cleanup

    @matix, here we go, SpriteBinding is supported
     
  28. matix

    matix

    Joined:
    May 4, 2012
    Posts:
    4
  29. matix

    matix

    Joined:
    May 4, 2012
    Posts:
    4
    Last edited: May 30, 2012
  30. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    @matix, well done! thank you for your contribution!
    Next NData versions will include updates that make own bindings writing even easier.
    As for vector bindings, we've added this request to our backlog, so they are being available in one of the next updates
     
  31. bryanleister

    bryanleister

    Joined:
    Apr 28, 2009
    Posts:
    130
    Hi just bought your tool and thought I would mention that it's a bit hard to find the Code Templates on the mac from your first tutorial. On a mac it's located at Preferences>Code Templates> Add.

    Bryan
     
  32. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Hi Bryan,
    Thanks for this helpful information!
    I've updated tutorial text with it, so mac users won't have problems with that anymore
     
    Last edited: Jun 6, 2012
  33. bryanleister

    bryanleister

    Joined:
    Apr 28, 2009
    Posts:
    130
    Cool. I really like this concept of keeping the logic separate from the UI. It makes sense and seems very logical. OTOH, I have not programmed this way before and it is still not totally clicking for me. I've completed both the Slider tutorial and the checkbox tutorial and they both work, but they are also not applying changes to a GameLogic script like in your demo file.

    Looking at your NDataDemo scene, you are updating to a GameLogic script but I can't figure out how to make it work for my own script.

    So, just to keep it simple, how do I get the Slider value into a new GameLogic.cs script based on your Basic tutorial? And, to expand, can you explain adding something simple like a button that when clicked loads a new level in the GameLogic.cs?

    My goal (let me know if this is correct way to think) is to create a single UI framework, and a corresponding GameLogic.cs that I can use in multiple games. After all, most games all have the same kinds of variables, Player Health, Score, Awards, etc. so I want to create a nice polished interface UI that I can then import as a package and adjust for the specific game.

    I'm finding that my current UI implementation is confusing me, having to keep track of what buttons go where and what damages get updated on what script. I think the MVVM concept makes the most sense, especially when a game gets more complex and layered.
     
  34. bryanleister

    bryanleister

    Joined:
    Apr 28, 2009
    Posts:
    130
    OK, I think I'm getting it. So, within the Contexts set up on the ViewModel.cs, I can create functions as well as Properties. So, if all I want to do is load a level from a button click I could do this:

    Code (csharp):
    1.  
    2. public class StartScreenUi : EZData.Context
    3. {
    4.        //TODO: add your dependency properties and collections here
    5.  
    6.     public void Submit()
    7.     {
    8.         Application.LoadLevel(1);
    9.     }
    10.    
    11. }
    12.  
    13. public class ViewModel : MonoBehaviour
    14. {
    15.     public NguiRootContext View;
    16.     public StartScreenUi Context;
    17.     public GameLogic Model;
    18.  
    19.  void Awake()
    20.  {
    21.     Context = new StartScreenUi();
    22.     View.SetContext(Context);
    23.        
    24.  }
    25.    
    26. }
    27.  
    This seems like I'm starting to put Game Logic into the ViewModel and as I understand the concept is to keep 'em separated.

    In your demo, you are doing it a bit differently, calling this function

    Code (csharp):
    1.  
    2.     public void Submit()
    3.     {
    4.         _root.Model.SubmitScore(PlayerName, Score, DateTime.Now);
    5.     }
    6.  
    Can you explain what you are doing with all the "private Root _root;" and the class Root in DemoViewModel? Can there be more than one Root object in the scene?

    At first glance, this seems to be adding a lot of code and until I understand what the advantage is of doing it this way I think I'll remain confused...

    Thanks!
     
    Last edited: Jun 5, 2012
  35. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Hi Bryan,

    Good questions, thank you for understanding the principles and asking the right things!

    We've been using MVVM in our projects for a while, and noticed that there are a couple of possible pattern adjustments, depending on the project complexity.

    When application is not very large, like a typical casual game, and ViewModel almost directly reflects Model, it could be additional overhead on transferring data and triggering commands between Model and ViewModel and then Viewmodel and View.
    In such cases ViewModel can be redundant. So, if you feel that separating Model from ViewModel gives more overhead than benefits, it's totally fine to merge them together.
    It is not a classical MVVM, but main purpose is still achieved, View is separated from logic, no matter if it is split to two layers or not.

    In other case when project is more complex and not all things that happen in the application are directly reflected in View. If you are scaling up from simpler solution described above, at some moment you notice that only a subset of your properties and functions is exposed to View, commands from View deal with data from this subset only. When you see that ViewModel is no longer just a proxy, but a separate entity and a subject for possible decoupling from the rest of the Model, it would be logical to isolate that entity, so that it doesn't know about the model and cannot accidentally do something that it not supposed to. Model knows everything about Viewmodel and can use its properties.

    However, in many cases ViewModel can not be completely isolated, it still has to know a little bit on how to affect the Model, trying to keep this interface as small as possible. It can be done in different ways, what you prefer. For example, Model can provide some callbacks that ViewModel is allowed to invoke, or it can provide itself as an implementation of interface that ViewModel is allowed to use.

    In our demo, ViewModel containing "public GameLogic Model;" - is not very clean way to isolate ViewModel from Model, giving access to all public part of GameLogic. It's ok for demo, since SubmitScore method is the only public thing there, but for more advanced scenarios it's safer to narrow access down to some interface implementation or callbacks explicitly provided to ViewModel.

    _root variable is just a way to provide access to Model in the whole hierarchy of ViewModel classes. You don't have to do it in the same way, it's just an example, end even not the best one, because this way parts of ViewModel can disregard their hierarchy and call each other through the _root that is available all over the ViewModel. So, it's completely up to you how to arrange access to model if you need it at all.

    Meanwhile, we'll try to come up with more examples and demos covering cases of different complexities and demonstrate different approaches.
     
  36. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    If you look at previous post and think "No way, it's too complicated", don't worry, all that stuff only relates to the projects with complex architecture, containing many thousands line of code. If you're working on a smaller project, it still gives you a boost in development speed and stability if you just separate view from logic.

    Among new bindings, upcoming NData update will also contain quick start script that gears up your already existing UI with some basic classes and components, so that you can start using features provided by NData after a couple of clicks.
     
  37. bryanleister

    bryanleister

    Joined:
    Apr 28, 2009
    Posts:
    130
    I can see that was what _root is doing, but my question remains- What is the 'clean' way to click a button and run a function on the model or update a variable on the model?

    Since the EZData.Context is outside of the ViewModel class it's not easy for me to see how to reference/linkup to Model.
     
  38. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    There are few ways: for example, if some class in your viewmodel needs to trigger specific model callback, viewmodel can provide this very callback to that class. Or in case of providing model as an interface implementation, it can be done by inheriting that common interface that model interface from multiple smaller ones, and provide that specific interfaces to your viewmodel inhabitants

    Here's some code as an illustration.

    Case 1 (combined Model and ViewModel, in this case there's no need to access the model in some special way, you're already there, when command is triggered):

    Code (csharp):
    1.  
    2. ///////////////////////////////////////
    3. // Model and ViewModel combined      //
    4. ///////////////////////////////////////
    5.  
    6. class Game : EZData.Context
    7. {
    8.     private void LoadLevel(int level)
    9.     {
    10.         Application.LoadLevel(level);
    11.     }
    12.  
    13.     public void Submit() // called from View via command binding
    14.     {
    15.         LoadLevel(1);
    16.     }
    17. }
    18.  
    19. public class ViewModel : MonoBehaviour
    20. {
    21.     public NguiRootContext View;
    22.     public Game Context;
    23.    
    24.     void Awake()
    25.     {
    26.         Context = new Game();
    27.         View.SetContext(Context);
    28.     }
    29. }
    30.  
    31.  

    Case 2 (separate Model with interface for usage in ViewModel, if you're planning something big, it's a good solution to start with):
    Code (csharp):
    1.  
    2.  
    3. ////////////////
    4. // Model      //
    5. ////////////////
    6.  
    7. interface IModel
    8. {
    9.     void LoadLevel(int level);
    10. }
    11.  
    12. class Game : IModel
    13. {
    14.     // IModel implementation
    15.     public void LoadLevel(int level)
    16.     {
    17.         Application.LoadLevel(level);
    18.     }
    19.  
    20.     // rest of the game that should not be accessible from ViewModel
    21.     // ...
    22. }
    23.  
    24.  
    25. ////////////////
    26. // ViewModel  //
    27. ////////////////
    28.  
    29. public class Ui : EZData.Context
    30. {
    31.     private IModel _model;
    32.     public Ui(IModel model)
    33.     {
    34.         model;
    35.     }
    36.  
    37.     public void Submit() // called from View via command binding
    38.     {
    39.         _model.LoadLevel(1);
    40.     }
    41. }
    42.  
    43. public class ViewModel : MonoBehaviour
    44. {
    45.     public NguiRootContext View;
    46.     public Ui Context;
    47.     public IModel Model;
    48.  
    49.     void Awake()
    50.     {
    51.         Context = new Ui(Model);
    52.         View.SetContext(Context);
    53.     }
    54. }
    55.  
    Changing model properties directly from ViewModel is generally not recommended (although you can define properties in model interface too), if you have to do that a lot, and most of your game will be "proxied" through the ViewModel, it's really better to follow case number one.

    We're working on new examples and tutorials for NData, so if you're interested in how full game structure could look like when following both approaches from this post, we'll try to make it as soon as possible.
     
  39. bryanleister

    bryanleister

    Joined:
    Apr 28, 2009
    Posts:
    130
    I think I will wait for the example scripts and take it from there. I couldn't get these scripts to work, not deriving from MonoBehavior errors and such.

    If I can make a couple of suggestions for the new examples - Please comment the code that is outside the MonoBehavior class. I'm used to starting scripts from Unity and everything is almost always contained inside the class I've created, so it's unfamiliar to me to work with these other classes within the same script.

    A Start Game button, score a few points that get counted in a main Game Setting script along with a health bar would make me a happy camper!

    And, since you are introducing MVVM, and since this is tied to NGUI please set it up in a way that demonstrates best practices for both. I decided that since I'm already using NGUI's UI Play Animation on the Start button, and my goal is to have cleaner code it makes sense to simply tell the Start Game button to load the function after the animation has played.

    This keeps my game logic in the Model and I think is cleaner code for simple function that is not called often. Of course, health bars, sliders and scores make more sense to have the data binding using NData. It would be nice to see an example that takes this into account and sets up according to what makes the most sense.

    Thanks!

    Bryan
     
  40. JDonavan

    JDonavan

    Joined:
    Oct 3, 2009
    Posts:
    105
    Does this support iOS?
     
  41. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Yes, NData supports iOS
     
  42. soulis6

    soulis6

    Joined:
    Apr 21, 2011
    Posts:
    22
    Just started using NData, and although it took me a little time to wrap my head around it (the tutorials helped a lot), i'm finding it very useful and neat so far.

    I have some questions about the SelectedItem property of a collection. There's a lot of associated functions with that, and and i'm trying to get some more information on how it works in general, but I couldn't find any additional info on the site, and since the source code is in a .dll file, I can't look at that to see how it's used.

    Does the selected item get changed other than manually ever? Are there any bindings for a buttons that would allow you to change it to a specific index value? If I wanted to change the SelectedItem based on the user pressing a button, acorrding to which button was pressed (pressing a level button for example, if there's 10 levels, i want it to change the SelectedItem based on which button was pressed).

    Any tips for this?
     
  43. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Hi Soulis6,

    There are several ways of SelectedItem change other than manually.
    The first one is to use collection in items source binding, when you have a list of items in your UI that represent a content of your collection. Items source binding keeps UI in sync with collection and selected item - with it's representation, whether it's changed from code or from UI.

    In your case buttons corresponding to levels could be organized as a button list bound to levels collection, so that selected item is updated automatically.

    We'll try to prepare tutorial about item collection and selected item bindings soon.

    Here are some other useful tips abut collections:
    • If you want to bind to some property or command of specific collection item and you know it's index, you can use binding like this: MyGame.Levels.3.SomeLevelMember, where 3 is the index of an item and what comes after index is the path within collection item.
    • Collections have properties HasItems, HasSelection, ItemsCount and SelectedIndex that you can also bind to
    • There's an auto-select argument in collection constructor, you can set it to true, it'll make first item added to collection selected.
     
  44. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
  45. soulis6

    soulis6

    Joined:
    Apr 21, 2011
    Posts:
    22
    Thanks for the great response! Some very useful stuff there, and the inventory example looks like it will be very helpful as well.

    When you say organized as a button list, what do you mean exactly? I don't see any sort of button list binding, or anything that fits that description.

    Essentially what I have is a list with about 6-7 levels in a panel, with a separate single text element in another panel, that is bound to Level.SelectedItem.TotalStuff. So Right now i've got just a set of individual functions that change the SelectedItem for each one, (ex. Level.SelectItem(1), Level.SelectItem(2), etc) which works fine, but will get messy quickly if we end up adding more. So what i'm wondering is if there's a way to just somehow pass an int as an arugment for a buttonclick binding, or change the SelectedItem from a binding.

    I'll be using your tips though, and thanks again for the quick response!



    Edit: After playing with the inventory tutorial a bit, I have one other question; Is there any way to use ItemsSourceBinding without the List Item template? Can you stick it on a parent, set the collection name, and have all the children under it use that named collection? I tried to use it by itself in our current setup, and it didn't seem to work, all the text bindings that were just using the collection's properties (name, number, etc) were just blank.

    I was also going to say that I couldn't get the Inventory tutorial to work, but then I realized that in that tutorial there's a newer version of ItemsSourceBinding, which it says you need if you have 1.0.5 or earlier, which is confusing, as that's the current version in the asset store. After replacing that it worked great though!
     
    Last edited: Jun 27, 2012
  46. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    1.0.6 is on its way, it usually takes couple of days till new version is available. Regarding your questions, first of all, command bindings don't support arguments, and we're not planning to add this support, because solutions like that increase coupling between UI and underlying data. However, fortunately, it seems like in your case you don't need any code at all to select levels by clicking controls.

    You mentioned "a list with about 6-7 levels" if that levels have the same look and positioned like they are an actual list, you could make it in similar way it's done in tutorial, leaving only one of the items as a template and bind the whole list to levels collection. Using ItemsSourceBinding without an item template is not possible at the moment, but here's the idea:

    Instead of having a template and instantiating items at run-time, ItemsSourceBinding could reuse existing static items that you already have in the list as if they were instanced from a template. This approach seems to solve your situation completely, levels will look exactly the same way as they are right now, and will actually be bound to the corresponding items in collection, enabling automatic SelectedItem updates when you select levels in UI. How do you think, will it be useful in your case? Also, it looks like a simple, but very useful feature in general, so it could be added quite soon.
     
  47. soulis6

    soulis6

    Joined:
    Apr 21, 2011
    Posts:
    22
    Thanks for the detailed reply!

    I ended up doing what you suggested for the levels, used item source binding and item template like in the inventory demo for them. After a little bit of tweaking it works perfectly.

    As for the ItemsSourceBinding without the template, I think that could be very useful, but for my particular case, i'm not sure it would work.

    The reason I asked about it is because I have one other main UI scene, with around 12 or so items that aren't clicked or selected, all on screen at once, which are set up identically in terms of data binding and information, but the scale and position and look of each is different, so it wouldn't be feasible to use the ItemTemplate with them like before.
    So for now I've just been manually setting the bindings (ex. for the name "level.1.name", "level.2.name" and so on), which works, but it's a huge pain when adding new elements or changing stuff around, because I have to go through all of them again and set the specific item number. What would be incredibly useful, is if there were a way to just set ItemSourceBinding on each parent item, and just set it to "level.1", "level.2" etc, so the numerous individual objects could just be "name", "number" and so on.

    I don't know if that's possible though, so no worries either way, NData has already been extremely useful so far.
     
    Last edited: Jun 28, 2012
  48. PSFAX

    PSFAX

    Joined:
    Jul 15, 2012
    Posts:
    2
    Thanks for the tutorial, but I have a doubt yet. When I click in a item that I want it be selected, How to get the Index from the item clicked to pass as argument to the method SelectItem(int Index)?

    I resolved this problem creating a property int Id in my item class to store the index when I create the items at the first time, but when I remove some item from the collection this values will be out of sync.
     
  49. Art Of Bytes

    Art Of Bytes

    Joined:
    Jun 8, 2011
    Posts:
    198
    Hi PSFAX,

    You don't have to manually call SelectItem. When item is clicked (it has to contain a BoxCollider for that) SelectedItem as well as SelectedIndex and other properties will be updated in collection object automatically. Also OnSelectionChange event will be triggered. NData comes with a demo project, and there you can find an example of the list that works exactly like that, with UI selection and SelectedItem/SelectedIndex synchronized automatically.

    Don't forget that you can use SelectedItem as a part of binding path, for example, path like "MyCollection.SelectedItem.Name" will point to the Name property of the current selection.
     
  50. PSFAX

    PSFAX

    Joined:
    Jul 15, 2012
    Posts:
    2
    I didn't realize that in the "High Score Table" in the NDataDemo it's possible to select a row.
    Thanks.