Hello,
Could you please advise on how to correctly set up data binding for the following situation? I have a custom UI element called Header
, which contains a Label
named sensorSpeedLabel
. Additionally, there is a SensorManager
on the scene that has a float
property called sensorSpeed
, which collects the average speed of all sensors. How can I bind the sensorSpeed
value to sensorSpeedLabel
in Header
using DataBinding so that the value is linked only once? Furthermore, Header
has several other fields that similarly need to receive data from various sources (objects or classes).
Thank you!
I feel like this is pretty reasonably covered in the manual: Unity - Manual: Create a runtime binding in C# scripts
What part are you having difficulty with in particular?
I followed the documentation, but I’m encountering an issue. The data binding works when I modify the field in the Inspector or in the Update
method, for example, but it doesn’t work with a get
property, which I need to use to gather data from multiple sensors.
Here’s the code I’m working with:
SensorManager.cs:
public float SensorSpeed => sensors.Count > 0 ? (float)sensors.Average(sensor => sensor.Sent_FPS) : 0;
UIManager.cs:
screens.ForEach(screen => screen.header.SetWifiDataBinding(new DataBinding
{
dataSource = sensorManager,
dataSourcePath = new PropertyPath(nameof(SensorManager.SensorSpeed)),
bindingMode = BindingMode.ToTarget
}));
Header.cs:
public void SetWifiDataBinding(DataBinding _dataBinding)
{
wifiSpeedLabel.SetBinding("text", _dataBinding);
}
The problem is that the binding doesn’t seem to update when the value is retrieved through the get
property. I need it to collect data from several sensors. How can I resolve this issue?
Or is it impossible to bind something like this, and it’s necessary to keep the object in the SensorManager
, updating it from each sensor?
Probably comes down to the update trigger? Though the default should be every frame, but you could start with explicitly defining that as the update trigger, which makes the most sense for sensors.
Nonetheless I’ve not had issues with Get-only properties myself, and have used them fairly frequently.
I assume you’re not forgetting the [CreateProperty(ReadOnly = true)]
attribute?
Great! I’m glad that adding [CreateProperty(ReadOnly = true)] fixed the issue.
1 Like
Regarding the question of binding multiple data sources to different VisualElements within a custom element, what would you recommend? At the moment, I see that the best approach would be to create several properties, each of which would receive different DataBindings for each internal field, and then bind them externally.
Good question. Not a situation I’ve been in… but if I had to be clever about it, I might make a single object that consolidates all these these different data sources. Then that single object can be used as the data source for the top most object, and probably means the visual elements can bind themselves to said object as well.
Ah, I see what you mean, and I was considering a similar approach. The idea is to create a data object for the entire Header, then distribute this data within the Header, while filling this object from various external data sources. Thank you.
1 Like