I need some help understanding the correct way to asynchronously load a VisualElement’s UXML file for my runtime UI.
I’m generally following the Element-first approach from the docs, but instead of Resources.Load, I’m using Addressables.LoadAssetAsync like so:
public class CustomElement : VisualElement
{
public CustomElement()
{
var handle = Addressables.LoadAssetAsync<VisualTreeAsset>("CustomElement.uxml");
handle.Completed += _ =>
{
var visualTreeAsset = handle.Result;
visualTreeAsset.CloneTree(this);
};
}
}
This mostly works as expected, except that I get this warning that only occurs in the editor:
SendMessage cannot be called during Awake, CheckConsistency, or OnValidate (New Game Object: OnDidAddComponent)
This seems to happen because the generated code for the CustomElement calls the element’s constructor during OnValidate, and in turn, Addressables.LoadAssetAsync calls SendMessage, which is not allowed during OnValidate.
I can’t use the synchronous version of Addressables.LoadAssetAsync because I’m making a WebGL app which does not support synchronous loading.
Is there some other pattern I should use to async load UXML files for custom VisualElements?