USS comments and formatting are destroyed when saving any UXML in the UI Builder

Say you have a USS file like this (a little bit exaggerated to show the problem; this isn’t how I write my USS):

/* ========== FONT SETTINGS ============= */

/* This is the basic headline class used for all "title" labels in the UI and the main menu buttons. It does not have
   a font size because each title will set its own font size. */
.headline
{
    color: rgb(255, 0, 0);
    -unity-font-definition: url('../fonts/ComicShark_UITK.asset');
    -unity-text-outline-width: 3px;
    -unity-font-style: bold; /* The bold will be "Cancelled out" by the outline so it won't look too bulky */
    -unity-text-outline-color: rgb(255, 255, 255);
    /* text-shadow: 8px 8px 0 rgb(0, 0, 0); -- commenting this out until text shadows are working properly */
}

The second you save any UXML that references it, it’ll turn into this:

.headline {
    -unity-font-definition: url('project://database/Assets/main/ui/fonts/ComicShark_UITK.asset?fileID=11400000&guid=1987cd0624734ad48ba2a1460a872485&type=2#ComicShark_UITK');
    -unity-text-outline-width: 3px;
    -unity-font-style: bold;
    color: rgb(255, 0, 0);
    -unity-text-outline-color: rgb(255, 255, 255);
}

This becomes a real problem if you want to have “Sections” in the USS split up by comments/whitespace or… it’s just a problem. The full paths and GUIDs being added to URLs makes it much harder to read the important part.

This is especially egregious since I didn’t make any changes at all to the USS in the UI Builder, just the UXML. If the UXML references the stylesheet, stuff breaks.

3 Likes

Indeed, comments and formatting are lost when saving from the UI Builder. This is happening because comments and formatting are not imported in the VisualTreeAsset (because that information is useless at runtime). By the time the UI Builder opens a UXML, that information is long gone.

I agree this is a problem.

This is the price to pay at the moment to be able to move the assets within the project without breaking the uxml.

1 Like

That seems like a hard problem. I wonder if when exporting/saving you could load the source file, match the changed elements, and only replace the text for those ones. It wouldn’t be perfect (eg any formatting/comments inside the element itself would be lost), but it would at least allow for hybrid text+builder workflows.

That’s fair; thanks for the explanation.

It is! We’d need the keep the equivalent of an AST in the editor and not leak it in the runtime (for size/performance). Definitely possible, but even with that, it wouldn’t be perfect, since comments are hard to understand (i.e. if you remove the style property that was just below a comment, should we keep the comment or not?) It would at least allow us to keep formatting in place and even support hex colors and the likes.

It’s something we’ve known and pondered for a while.

1 Like

Could you at least make it so that if you don’t need to overwite save the USS (no added/modified styles), you don’t?

To me, it looks like this is already the case. If you don’t change USS files through the Editor, it won’t write to the file and thus, comments and formatting are preserved.

I’m on preview.17.

If you edit any UXML that references the USS using UI Builder, the USS will be rewritten, even if none of the styles in it changed. It saves both the UXML and the USS.

This behavior also impacts @imports in the USS - they get stripped off. I’d opened a bug report well before seeing this thread.

I enconutered this using the BEM guidelines.

https://docs.unity3d.com/Manual/UIE-USS-WritingStyleSheets.html
https://cssguidelin.es/#bem-like-naming

Was not familiar with BEM until now, but it helped me structuring the USS as well as writing the rules at the same time.

So that’s still our lot two years later? No commenting in uss? hiding emotional breakdown

2 Likes

I tested it, and it seems like the issue were fixed. I added a USS file in builder, but nothing were changed in the USS file. Your comments disappears only in a UXML file (probably because the UI builder rewrites the file)