Giter VIP home page Giter VIP logo

Comments (7)

NyxWallace avatar NyxWallace commented on September 26, 2024

I have the same exact problem, I need to get a collection of user from a database, but if I change the collection after initialization nothing change (I've tryied the above solutions as well before coming here), is there a way to do it?

from unity-weld.

marijneken avatar marijneken commented on September 26, 2024

Yes. There's definitely a way, because I figured it out. I'm not at my computer at the moment, but I'll look it up later and write it down.

from unity-weld.

NyxWallace avatar NyxWallace commented on September 26, 2024

Thanks, that would be very kind of you.

from unity-weld.

marijneken avatar marijneken commented on September 26, 2024

Sorry, I still intend to write it down, but I've suddenly become super busy. I'll try to find some time soon.

from unity-weld.

marijneken avatar marijneken commented on September 26, 2024

OK, so here's how to do it. My example is for a simple list that uses a Vertical Layout Group, but this can be easily adjusted for a Dropdown of course.
image

This is my Hierarchy:
image

The ServerProjectList holds the view model for my list:
image

The simplified code for that is:

using UnityEngine;
using UnityWeld.Binding;

namespace VDT.UnityPlugin.VR
{
    [Binding]
    public class ServerProjectListViewModel : MonoBehaviour
    {
        [Binding]
        public ObservableList<ProjectItemViewModel> ProjectItems { get; private set; }

        public ServerProjectListViewModel()
        {
            // We can't initialize the list directly with an auto property initializer in C# 4. We could do this in C# 6 and up.
            ProjectItems = new ObservableList<ProjectItemViewModel>();
        }

        [Binding]
        public void SelectProject(int projectId)
        {
            // TODO: Do something
        }

        private void OnProjectListResultHandler(object sender, ProjectListResultEventArgs e)
        {
            ProjectItems.Clear();
            foreach (var project in e.Projects)
            {
                ProjectItems.Add(new ProjectItemViewModel(project));
            }
        }

        private void OnEnable()
        {
            FrameworkSettings.Instance.RegisterProjectListHandler(OnProjectListResultHandler);
        }

        private void OnDisable()
        {
            FrameworkSettings.Instance.UnRegisterProjectListHandler(OnProjectListResultHandler);
        }
    }
}

This uses the ObservableList which automatically updates the UI when its contents change.
As you can see, I'm registering a Handler to be used as a callback for when new items are loaded. I then clear the old list and add the new items.

Then the ProjectList has a Vertical Layout Group and also the CollectionBinding component:
image
image

That's where you choose the property to bind to in your View Model. In our case ProjectItems. The Collection templates simply refers to the Project GameObject in our Hierarchy. It contains the following components:
image

The ProjectItemViewModel is pretty straightforward:

using System.ComponentModel;
using UnityWeld.Binding;

namespace VDT.UnityPlugin.VR
{
    [Binding]
    public class ProjectItemViewModel : INotifyPropertyChanged
    {
        [Binding]
        public int ProjectId { get; private set; }

        [Binding]
        public string ProjectName { get; private set; }

        public ProjectItemViewModel(Project project)
        {
            ProjectId = project.ProjectId;
            ProjectName = project.ProjectName;
        }

        /// <summary>
        /// Event to raise when a property's value has changed.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

You can bind the text on your button or dropdown list item to the ProjectName property and you're all set.

I hope this helps. Cheers, Marijn.

from unity-weld.

NyxWallace avatar NyxWallace commented on September 26, 2024

Hi Marijn, thank you for taking the time, but unfortunatly I'm still stuck because the drop down list, as you mention in your initial comment, does not bind to an ObservableList (or at least it doesn't seems to do so). It is also worth mentioning that every other script let me choose the property I'm willing to bind to, but here I have to write down the properties, so I'm not even sure I'm doing it correctly. I think I'll just find a work around using a list.

from unity-weld.

marijneken avatar marijneken commented on September 26, 2024

Hmm, I had a quick look at the DropDown. It's been a while since I dealt with it. I now notice that it seems a bit convoluted in its setup. It's not that different from a list, in terms of functionality, so I'm pretty sure you'd be better off building your own dropdown control, because then you have better control over things like data binding. Sorry I couldn't be of me help right now.

from unity-weld.

Related Issues (20)

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.