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

Why TemplateContainer's size is always 0X0?

Discussion in 'UI Toolkit' started by Digital_Mimesis_Lab, Apr 25, 2022.

  1. Digital_Mimesis_Lab

    Digital_Mimesis_Lab

    Joined:
    Mar 9, 2022
    Posts:
    24
    As the title says.
     

    Attached Files:

  2. cpalma-unity

    cpalma-unity

    Unity Technologies

    Joined:
    Nov 30, 2020
    Posts:
    90
    Hello! The template container should resize according to the elements inside of it. Is it possible that your elements inside the template are using absolute position? If that's not the case, could you share your uxml file and the preview in UI Builder?
     
  3. Yecats

    Yecats

    Joined:
    Jul 13, 2014
    Posts:
    69
    @cpalma-unity I'm running into this as well. Mine is not set to the absolute position. Were you able to troubleshoot this?
     
  4. martinpa_unity

    martinpa_unity

    Unity Technologies

    Joined:
    Oct 18, 2017
    Posts:
    360
    The TemplateContainer was originally created for the inspector use-case, so that's why, as @cpalma-unity suggests, it will resize accordingly to the size of its children.

    In editor windows and in runtime, you sometimes want a different behaviour by default, which is that your main panel should take all the available space. Out of the box, the TemplateContainer makes it harder to work in that mode, because unless your root elements provide a height or min-height (for example), the TemplateContainer will have a height or 0.

    To make sure that the TemplateContainer's size is not 0, you need to either set the height of its children or you need to make the TemplateContainer's size be dependent on the parent, for example by setting the
    flex-grow
    property of the TemplateContainer to 1.

    Note that you can also avoid some of these issues by using
    VisualTreeAsset.Clone(parent)
    , which will add the content of the uxml file to the parent directly without adding a TemplateContainer.

    Hope this helps!
     
  5. Digital_Mimesis_Lab

    Digital_Mimesis_Lab

    Joined:
    Mar 9, 2022
    Posts:
    24
    Thanks, the last method would work for me , the former one is confused.