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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question What is the correct way to use style sheets and templates at runtime

Discussion in 'UI Toolkit' started by liekong1991, Nov 9, 2021.

  1. liekong1991

    liekong1991

    Joined:
    Nov 15, 2014
    Posts:
    12
    A UI I am making is as follows:
    upload_2021-11-9_11-29-33.png

    Here's the following questions:

    1,When I want to make a ListView, I need "dataSource", "makeItem" and "bindItem" and other binding methods, right? So I create a Template called "ListItem_Record" below as a .uxml file. Unfortunately, when I drag and drop this template onto my UI, its style is inconsistent with the style before it is not set as a template, Like that:
    upload_2021-11-9_11-36-7.png

    I don’t understand why UI Toolkit will wrap it again when creating the template, which make its style is inconsistent with the style before. If I want them to behave the same, I have to give the '#ListItem_DefectRecord" a ".ListItem_Record" styleSheet to make them act like the same.

    2, Is there an officially recommended way to load .uxml and .uss resources at runtime? When I want to create and specify them at runtime, I did not find any tutorials available. Please Help!
     
  2. Finch07

    Finch07

    Joined:
    Jul 27, 2013
    Posts:
    4
    I don’t have an answer for this, I just wanted to say that I think this behavior is really strange too.
     
  3. liekong1991

    liekong1991

    Joined:
    Nov 15, 2014
    Posts:
    12
    And is there some way to specify the stylesheet for some control in the runtime?
     
  4. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    366
    Yes I run into the same issue.
    I wanted to have templates so that the style can be set via uibuilder. But that extra VisualElement, that is generated when using the template works against that.
    For example having a container with "justify-content: center;" the behavior is different if there is a VisualElement with no style info there or not.

    My workflow is now: When styling the container and the template i put dummy "Visual Element" between to compensate that template-Element
     
  5. liekong1991

    liekong1991

    Joined:
    Nov 15, 2014
    Posts:
    12
    Ugh......It still doesn't seem convenient enough