Hi! I’m working on inventory system using localization and new UIToolkit binding system. Unfortunately i can’t make it work, when I change locale through dropdown in game view or via code only first item in inventory is being updated with localized text. If I change locale and then add new item it will contain correct values but still wont change after that(except first one).
Here’s my item class:
public class ItemStack : INotifyBindablePropertyChanged
{
private int _quantity;
public event EventHandler<BindablePropertyChangedEventArgs> propertyChanged;
[CreateProperty]
public ItemConfig Config { get; set; }
[CreateProperty]
public int Quantity
{
get => _quantity;
set
{
if (_quantity == value)
return;
_quantity = value;
OnPropertyChanged();
}
}
private void OnPropertyChanged([CallerMemberName] string property = "") =>
propertyChanged?.Invoke(this, new BindablePropertyChangedEventArgs(property));
public ItemStack(ItemConfig config, int quantity)
{
Config = config;
Quantity = quantity;
}
public override string ToString() =>
$"Name: {Config.Name.GetLocalizedString()} Quantity: {Quantity}";
}
Which holds link to simple scriptable object
[CreateAssetMenu(fileName = "New Item Config", menuName = "Inventory/Item Config", order = 0)]
public class ItemConfig : ScriptableObject
{
[field: SerializeField, DontCreateProperty]
[CreateProperty]
public string Id { get; set; }
[field: SerializeField, DontCreateProperty]
public LocalizedString Name { get; set; }
[field: SerializeField, DontCreateProperty]
public LocalizedString Description { get; set; }
}
This is how I bind item to view
public void AddItem(ItemStack item)
{
Items.Add(item);
AddItemView(item);
}
public void AddItemView(ItemStack item)
{
VisualElement root = _canvas.rootVisualElement;
VisualElement itemView = _itemTemplate.Instantiate();
Label nameLabel = itemView.Q<Label>(itemNameLabel);
Label descriptionLabel = itemView.Q<Label>(itemDescriptionLabel);
Label quantityLabel = itemView.Q<Label>(itemQuantityLabel);
itemView.dataSource = item;
nameLabel.SetBinding("text", item.Config.Name);
descriptionLabel.SetBinding("text", item.Config.Description);
quantityLabel.SetBinding("text", new DataBinding
{
dataSourcePath = new PropertyPath("Quantity"),
bindingMode = BindingMode.ToTarget
});
root.Q("inventory__container").Add(itemView);
}
Any ideas why it working only on first item?