Comments (7)
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.
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.
Thanks, that would be very kind of you.
from unity-weld.
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.
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.
The ServerProjectList
holds the view model for my list:
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:
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:
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.
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.
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)
- TwoWayBinding on a Slider with Min Value not set to 0 fails HOT 4
- Binding to game object visibility HOT 2
- InputField binding binds to wrong event HOT 1
- Problem of TypeResolver for Interface HOT 2
- How to approach drag-drop behavior? HOT 6
- Binding through custom string instead of function name HOT 3
- Bugged images after binding a Sprite in Image Component using OneWayPropertyBinding HOT 2
- Access object of collection HOT 1
- Implement Clean Mvvm Architecture HOT 2
- Binding to parent viewmodel afte Collection Binding doesn't work HOT 1
- Ahead of time Compilation Problem with Binding on iOS HOT 3
- Why custom ObservableList? HOT 5
- CollectionBinding to unassigned property
- How to properly bind to ViewModel data that is avaialbe after Awake? HOT 1
- (Fix Included) Does not update all bindings in VM when passing Null or String.Empty HOT 2
- FloatToStringAdapterOptions removed at runtime
- Event Binding Execution Order Issue causing Race Condition
- Is this project dead? HOT 4
- can I use unity weld on IL2CPP ? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from unity-weld.