Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Generate VisualElementAsset/StyleSheet from a string?

Discussion in 'UI Toolkit' started by ComputerKim, Mar 22, 2020.

  1. ComputerKim

    ComputerKim

    Joined:
    Jan 6, 2019
    Posts:
    15
    I have spent some time playing around with the UIElementsUniteCPH2019RuntimeDemo project.

    Is there a way to generate VisualElementAsset/StyleSheet from a string?

    If not, is it a planed feature?

    This could be useful if you wanted to load from a file/server.
     
    JesOb likes this.
  2. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,081
    In HTML/JavaScript world it is very useful and easy to use tool :)
     
  3. fherbst

    fherbst

    Joined:
    Jun 24, 2012
    Posts:
    801
  4. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,203
    It's a known limitation and one we'll address post release (next year probably). But for streaming assets, you can use Unity's existing asset streaming features to send .asset (already parsed/imported) USS and UXML assets to the game and reload them at runtime.
     
    Orimay, ComputerKim and fherbst like this.
  5. Orimay

    Orimay

    Joined:
    Nov 16, 2012
    Posts:
    304
    +1 for runtime uss classes created from code
     
  6. achimmihca

    achimmihca

    Joined:
    Feb 13, 2016
    Posts:
    266
    Congrats to the release of Unity 2021.2!
    I see it includes the new UIToolkit.

    Is there now a way to load StyleSheets at runtime (from String)?
    Or did anyone find a workaround?
     
    MostHated likes this.
  7. martinpa_unity

    martinpa_unity

    Unity Technologies

    Joined:
    Oct 18, 2017
    Posts:
    359
    Hi! This is still not possible at the moment.
     
  8. Neiyra

    Neiyra

    Joined:
    Apr 19, 2016
    Posts:
    28
    +1 for runtime uss classes created from code
     
    NotaNaN, seyfe and JesOb like this.
  9. achimmihca

    achimmihca

    Joined:
    Feb 13, 2016
    Posts:
    266
    Here is a compromise that I think would work for everyone: Add a single method to create a StyleSheet from String.

    This way, you can keep rules, selectors, variables etc. as internal implementation details.
    But we would be able to create StyleSheets at runtime by constructing / loading the USS text.

    AFAIK, browsers as well do not have API to add/remove/edit/iterate single rules or selectors of styleSheets.
    Instead, the approach is to add a style-Element to the DOM, i.e., create the CSS text that the browser will parse ( https://stackoverflow.com/questions...ally-create-css-class-in-javascript-and-apply ).
    Same approach would work for Unity.

    Unity, please consider this!

    StyleSheet already has a public constructor. Thus, adding a single method to create a StyleSheet from String would be a minimal extension to public API.

    ---
    See also other thread with same issue: https://forum.unity.com/threads/loading-uxml-and-uss-from-text-in-runtime.1200964/
     
    Last edited: Jul 3, 2022