Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'UI Toolkit' started by burningmime, Sep 17, 2021.

  1. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
    Say you have a USS file like this (a little bit exaggerated to show the problem; this isn't how I write my USS):

    Code (CSharp):
    1.  
    2. /* ========== FONT SETTINGS ============= */
    3.  
    4. /* This is the basic headline class used for all "title" labels in the UI and the main menu buttons. It does not have
    5.    a font size because each title will set its own font size. */
    6. .headline
    7. {
    8.     color: rgb(255, 0, 0);
    9.     -unity-font-definition: url('../fonts/ComicShark_UITK.asset');
    10.     -unity-text-outline-width: 3px;
    11.     -unity-font-style: bold; /* The bold will be "Cancelled out" by the outline so it won't look too bulky */
    12.     -unity-text-outline-color: rgb(255, 255, 255);
    13.     /* text-shadow: 8px 8px 0 rgb(0, 0, 0); -- commenting this out until text shadows are working properly */
    14. }
    The second you save any UXML that references it, it'll turn into this:

    Code (CSharp):
    1. .headline {
    2.     -unity-font-definition: url('project://database/Assets/main/ui/fonts/ComicShark_UITK.asset?fileID=11400000&guid=1987cd0624734ad48ba2a1460a872485&type=2#ComicShark_UITK');
    3.     -unity-text-outline-width: 3px;
    4.     -unity-font-style: bold;
    5.     color: rgb(255, 0, 0);
    6.     -unity-text-outline-color: rgb(255, 255, 255);
    7. }
    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.
     
    SparkesRS and tonytopper like this.
  2. martinpa_unity

    martinpa_unity

    Unity Technologies

    Joined:
    Oct 18, 2017
    Posts:
    471
    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.
     
    burningmime likes this.
  3. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
    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.
     
  4. martinpa_unity

    martinpa_unity

    Unity Technologies

    Joined:
    Oct 18, 2017
    Posts:
    471
    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.
     
    burningmime likes this.
  5. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
    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?
     
  6. magnetic_scho

    magnetic_scho

    Joined:
    Feb 2, 2020
    Posts:
    92
    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.
     
  7. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
    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.
     
  8. TriptychalEric

    TriptychalEric

    Joined:
    Mar 12, 2021
    Posts:
    1
    This behavior also impacts @imports in the USS - they get stripped off. I'd opened a bug report well before seeing this thread.
     
  9. Dubitrubi

    Dubitrubi

    Joined:
    Jan 31, 2020
    Posts:
    5