How to force UIDocument create its tree asset?

We have a custom “dependency injection” that initializes all registered systems from its Awake(), which has the highest script execution order.

Now the issue is, many of them need to access a visual element of UIDocument, ofc issue is, the UIDocument did not create its element yet, so the UIDocument.rootVisualElement is null during our system initialization.

Question here is:

is there any way how to either force UIDocument to create its UI, or to instantiate the visual asset tree and show it to UI from code outside of UIDocument?

I know I can brute force it via

document.GetType().GetMethod("RecreateUI", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).Invoke(document, null);

But there has to be some cleaner and better way

Thank you

There isn’t a cleaner way today. If you want to avoid this, it might be easier to create your own root element that gets injected later as a child of the UIDocument’s root.

1 Like

Thats a good idea… create own empty visual element, not parented it anywhere, put everything there on initialize, and then when UI Document gets awaken, just attach it to that visual element of the document.

Thank you will try that :slight_smile: