UI Toolkit UI Document not refreshing it's runtimePanel after being disabled

There might be a bug in UI Tooklit’s UI Document. UI Document caches it’s runtimePanel after obtaining a reference from its panelSetting. This code is from a decompiled version of UI Document:

    private void ResolveRuntimePanel()
    {
        if (m_RuntimePanel == null)
        {
            m_RuntimePanel = rootVisualElement?.panel as RuntimePanel;
        }
    }

However, after the UI Document’s game object is disabled and re-enabled, we see a new runtime panel was created on the PanelSettings, but the UI Document is not picking up this new panel.

Interesting. There shouldn’t be two panels existing for a single panelSettings, and if the previous panel is destroyed then I would have expected the m_RuntimePanel == null check to fail.

Have you changed what panelSettings was used in the UI documents while it was disabled?

If you have some steps to reproduce can you submit them in a bug report, I will check that theses cases are fixed and covered by unit tests.

We’re not changing PanelSettings.

Here’s a rough outline of a reproduction. Scenario is rendering document to texture

  1. Setup PanelSettings to render to a texture
  2. Apply PanelSettings to UIDocument
  3. See UIDocument.runtimePanel == UIDocument.panelSettings.panel
  4. Apply some UXML to UIDocument’s source asset
  5. Apply AppUI’s WorldSpaceUIDocument component to UIDocument’s gameobject
  6. Set WorldSpaceUIDocument’s target camera to main camera
  7. Within the UIDocument’s game object hierarchy, setup mesh render for quad with material whose main texture is texture in PanelSettings
  8. Disable UIDocuments game object
  9. Enable UIDocument’s game object
  10. See UIDocument.runtimePanel != UIDocument.panelSettings.panel