Hello!
I’m working with the MultiColumnListView in Unity and looking for the best approach to streamline my workflow. Specifically, I often prefer to see live updates when modifying elements like cell styling or layout, without needing to enter Play mode.
So far, I haven’t found a way to populate a MultiColumnListView with mock data in Edit mode. Is there a method or best practice for working efficiently with mock data in a MultiColumnListView while staying in Edit mode?
Note: I’m not doing editor tools, it’s to have the MultiColumnListView within the game UI.
Thanks in advance!
I use the constructor of custom visual elements to insert dummy data when outside of play mode.
For example:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
[UxmlElement]
public partial class ExampleVisualElement : VisualElement
{
#region Constructors
public ExampleVisualElement() : base()
{
_multiColumnListView = new MultiColumnListView();
var colums = _multiColumnListView.columns;
colums.Add(new Column()
{
title = "NAME",
makeCell = MakeCellLabel,
bindCell = BindNameToCell,
stretchable = true,
});
colums.Add(new Column()
{
title = "AGE",
makeCell = MakeCellLabel,
bindCell = BindAgeToCell,
stretchable = true,
});
Add(_multiColumnListView);
#if UNITY_EDITOR
if (Application.isPlaying == false)
{
BuildMockUpDisplay();
}
#endif
}
#endregion
#region Internal Members
private readonly MultiColumnListView _multiColumnListView;
#endregion
#region Methods
public void ApplyPersons(List<Person> persons)
{
_multiColumnListView.itemsSource = persons;
}
#endregion
#region Internal Methods
private Label MakeCellLabel() => new();
private void BindNameToCell(VisualElement element, int index)
{
var label = (Label)element;
var person = (Person)_multiColumnListView.viewController.GetItemForIndex(index);
label.text = person.Name;
}
private void BindAgeToCell(VisualElement element, int index)
{
var label = (Label)element;
var person = (Person)_multiColumnListView.viewController.GetItemForIndex(index);
label.text = person.Age.ToString();
}
#endregion
#region EDITOR
#if UNITY_EDITOR
private void BuildMockUpDisplay()
{
var persons = new List<Person>()
{
new() { Name = "John", Age = 20 },
new() { Name = "Jane", Age = 24 }
};
ApplyPersons(persons);
}
#endif
#endregion
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
I’m sure there’s other ways.
thanks for such a quick reply sir, It’s indeed an useful approach and I’ll use it until I find more answers. I was trying to find a solution that does not imply messing the code, it seems to be such an uncommon way of working out there.