I have added a data source to one of my template instance created in my UI builder.
However I have found the the dataSource property of the UI element is null no matter what. As per another forum post dataSource of child items data source should be inherited from parent until overridden, and should not be null.
namespace Cirrus.UI
{
[UxmlElement]
public partial class QuickMenuVisualElement : BindableElement
{
private QuickMenuDataSource _dataSource;
private VisualElement _entriesLayoutElement;
[UxmlAttribute]
[CreateProperty]
public VisualTreeAsset _entryTemplate;
private Dictionary<IAbilityInstance, VisualElement> _entries = new Dictionary<IAbilityInstance, VisualElement>();
public QuickMenuVisualElement()
{
_AsyncInit();
}
private void _OnAbilityAdded(IAbilityInstance ab)
{
if (!_entries.ContainsKey(ab))
{
_entries.Add(ab, _entryTemplate.Acquire(_entriesLayoutElement));
}
}
private void _OnAbilityRemoved(IAbilityInstance ab)
{
if (_entries.TryGetValue(ab, out VisualElement elem))
{
elem.Release();
_entries.Remove(ab);
}
}
async void _AsyncInit()
{
await UniTask.WaitForEndOfFrame();
// This is null no matter what.
if (dataSource != null)
{
_dataSource = (QuickMenuDataSource)dataSource;
_dataSource.OnAbilityAddedEvent += _OnAbilityAdded;
_dataSource.OnAbilityRemovedEvent += _OnAbilityRemoved;
}
_entriesLayoutElement = this.Query("Layout_Entries");
}
}
}