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

Question Problem when using uss variables in runtime

Discussion in 'UI Toolkit' started by st069165, Jul 4, 2023.

  1. st069165

    st069165

    Joined:
    Nov 5, 2020
    Posts:
    5
    I used to change the theme rewrite the classes. For example:
    original theme uss file
    Code (CSharp):
    1. .something-class {
    2.     background-color: var(--something-color-1);
    3. }
    new theme uss file
    Code (CSharp):
    1. .something-class {
    2.     background-color: var(--something-color-2);
    3. }
    Next, to change the theme, I added a new uss file to uxml
    Code (CSharp):
    1. somethingUXML.root.styleSheets.Add(newThemeUSS)
    It is clear that there can be many things in a class. So I thought I'd update my way of changing the theme. I thought that I could move the color variables to the usual place and override them, rather than entire classes. For example:
    original theme file
    Code (CSharp):
    1. :root {
    2.     --background-color__something-class: var(--something-color-1);
    3. }
    4.  
    5. .something-class {
    6.     background-color: var(--background-color__something-class);
    7. }
    new theme uss file
    Code (CSharp):
    1. :root {
    2.     --background-color__something-class: var(--something-color-2);
    3. }
    For some reason it doesn't work for all files when I add files from code like this
    original theme file
    Code (CSharp):
    1. :root {
    2.     --background-color__background: var(--background-color-white);
    3. }
    4.  
    5. #Background {
    6.     background-color: var(--background-color__background);
    7.     width: 100%;
    8.     height: 100%;
    9.     position: absolute;
    10. }
    new theme uss file
    Code (CSharp):
    1. :root {
    2.     --background-color__background: var(--background-color-black);
    3. }
    But when I add in the editor manually everything works fine. Why doesn't this work when added from code? How to do it right?
     
    Last edited: Jul 4, 2023
  2. st069165

    st069165

    Joined:
    Nov 5, 2020
    Posts:
    5
    Maybe there is a way to update all classes after changing variables?
     
  3. Maverick

    Maverick

    Joined:
    Dec 18, 2009
    Posts:
    225
    Try removing the old stylesheets first, before adding new one. (at least it worked for me)
     
  4. st069165

    st069165

    Joined:
    Nov 5, 2020
    Posts:
    5
    It didn't work for me.

    When I remove the old style sheet, then add the new one and revert the old one, the result looks similar.

    But I found an amusing coincidence. If you swap the old and new styles in the editor, the result looks the same as with any of the methods of adding from the code. It's like the code keeps adding it in the wrong order, no matter what I add first.
     
  5. st069165

    st069165

    Joined:
    Nov 5, 2020
    Posts:
    5
    Corrected the mistake. The top-level uxml file didn't have my old style definition, so for some reason the uxml kids didn't have it either, although judging by the editor it exists there. Strange behavior