Using a template from a custom package sometimes throws errors

Because we have multiple Unity projects that we want to use the same GUI style for I created a package which contains our default controls, such as buttons, with their default styling. This seems to work fine in the UI Builder, I can add them to my GUI and they show up in runtime.

Occasionally though when I hit save in the UI Builder I get a set of errors “Template declaration requires a path or src attribute referencing another UXML file” and then “Unknown template name Button”. Full errors below:

full errors

Assets/Scripts/AnimationSelectionTree.uxml (1,2): Semantic - 'Template' declaration requires a 'path' or 'src' attribute referencing another UXML file
UnityEditor.AssetDatabase:Refresh ()
Unity.UI.Builder.BuilderDocumentOpenUXML:SaveNewDocument (UnityEngine.UIElements.VisualElement,bool,bool&,string) (at Library/PackageCache/com.unity.ui.builder@1.0.0-preview.11/Editor/Builder/Document/BuilderDocumentOpenUXML.cs:475)
Unity.UI.Builder.BuilderDocument:SaveNewDocument (UnityEngine.UIElements.VisualElement,bool,bool&,string) (at Library/PackageCache/com.unity.ui.builder@1.0.0-preview.11/Editor/Builder/Document/BuilderDocument.cs:271)
Unity.UI.Builder.BuilderToolbar:SaveDocument (bool) (at Library/PackageCache/com.unity.ui.builder@1.0.0-preview.11/Editor/Builder/Toolbar/BuilderToolbar.cs:287)
Unity.UI.Builder.BuilderCommandHandler:OnSaveDocument (UnityEngine.UIElements.KeyUpEvent) (at Library/PackageCache/com.unity.ui.builder@1.0.0-preview.11/Editor/Builder/Utilities/BuilderCommandHandler.cs:147)
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
Assets/Scripts/AnimationSelectionTree.uxml (5,14): Semantic - Unknown template name 'Button'
UnityEditor.AssetDatabase:Refresh ()
Unity.UI.Builder.BuilderDocumentOpenUXML:SaveNewDocument (UnityEngine.UIElements.VisualElement,bool,bool&,string) (at Library/PackageCache/com.unity.ui.builder@1.0.0-preview.11/Editor/Builder/Document/BuilderDocumentOpenUXML.cs:475)
Unity.UI.Builder.BuilderDocument:SaveNewDocument (UnityEngine.UIElements.VisualElement,bool,bool&,string) (at Library/PackageCache/com.unity.ui.builder@1.0.0-preview.11/Editor/Builder/Document/BuilderDocument.cs:271)
Unity.UI.Builder.BuilderToolbar:SaveDocument (bool) (at Library/PackageCache/com.unity.ui.builder@1.0.0-preview.11/Editor/Builder/Toolbar/BuilderToolbar.cs:287)
Unity.UI.Builder.BuilderCommandHandler:OnSaveDocument (UnityEngine.UIElements.KeyUpEvent) (at Library/PackageCache/com.unity.ui.builder@1.0.0-preview.11/Editor/Builder/Utilities/BuilderCommandHandler.cs:147)
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

After that the buttons get deleted and I need to re-add them. It only happens occasionally, the rest of the time the file saves just fine.

Button template:

<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
    <Style src="Button.uss" />
    <ui:Button text="Button" display-tooltip-when-elided="false" name="Button" class="button" />
</ui:UXML>

UI Builder generated template:

<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xmlns="UnityEngine.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" editor-extension-mode="False">
    <ui:Template name="Button" src="/Packages/com.xvr.ui/Core/Button.uxml" />
    <ui:VisualElement name="rootContainer">
        <ui:VisualElement name="TabContainer" style="height: 41px; justify-content: flex-start; align-items: stretch; flex-direction: row; background-color: rgb(25, 25, 24);">
            <ui:Instance template="Button" name="Tab1" />
            <ui:Instance template="Button" name="Tab2" />
        </ui:VisualElement>
        <ui:VisualElement name="TabBody1" style="height: 336px; background-color: rgb(25, 25, 24); border-left-color: rgb(68, 72, 73); border-right-color: rgb(68, 72, 73); border-top-color: rgb(68, 72, 73); border-bottom-color: rgb(68, 72, 73); border-top-width: 2px;">
            <ui:ListView focusable="true" name="ScrollView" style="height: 385px;" />
        </ui:VisualElement>
    </ui:VisualElement>
</ui:UXML>

Hello! We can confirm this should be working and not showing errors, but there may be an instability playing a part here. We have 2 questions for you:
1- Can you reproduce this reliably? E.g. if you do this/that actions the errors will pop. If yes, please let us know.
2- Are you using the “Open Sub-Document” feature from the UI Builder to edit your template in-place?

OP here.

I found one consistent way of reproducing the error and submitted a bug report. Casenr: 1300134

I wasn’t using the sub-document functionality, this is all in the main document.

Thank you for reporting! We’ll be checking the case and updating its status there :slight_smile:

I just had this happen in 2020.3.21f1, Win 10

Hello, the original case reported ended up closed because QA couldn’t reproduce with the information provided. If you have a sample project we can use for investigating, please open a new bug using Help > Report a Bug inside Unity. Thanks!