Search Unity

Feature Request ContainerOverride, let you add things inside templates inside specific elements

Discussion in 'UI Toolkit' started by Guedez, Aug 28, 2020.

  1. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    Let's say I want to make a window border template, and then develop a lot of windows using that as the template, so if I need to change the resize component, or the maximize and close buttons, I change a single component and they all change at once. The issue is that it seems I can't add things to a template.

    The label in the example can be added, but it gets booted out as soon as I save the document. (I tried editing the template by "Open as SubDocument", but it ended changing the original template document.

    By entry points, I mean something that indicates it's a container inside the template, where I can place other components inside.

     
  2. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    Hello,

    UXML supports these two features that can help achieve this kind of scenario:
    • AttributeOverrides
      node lets you override the value of an attribute of a child template. The element is addressed by name and can receive any number of attributes. Currently UI Builder does not support this though (it is planned).
    • contentContainer
      attribute. This attribute can be used in any UXML file to specify where elements added to the instance should be parented. The UI Builder already supports this since this is a just the same feature that allows a ScrollView to be a complex elements which supports child elements.
    A more concrete example of Window template and the main file that use these features:

    Code (CSharp):
    1.  
    2. <UXML xmlns="UnityEngine.UIElements">
    3.   <Template src="WindowTemplate.uxml" name="window"/>
    4.   <Instance template="window" name="window">
    5.       <AttributeOverrides element-name="LabelTitle" text="Window name override"/>
    6.       <Label text="I'm the content"/>
    7.   </Instance>
    8. </UXML>
    9.  
    Code (CSharp):
    1. <UXML xmlns="UnityEngine.UIElements">
    2.     <VisualElement style="background-color: cyan; border-radius:10px; padding:10px;">
    3.         <Label name="LabelTitle" text="Name" style="color: black; font-size:28px;"/>
    4.         <VisualElement name="container" contentContainer="true" style="border-color:grey; border-width:2px;"/>
    5.     </VisualElement>
    6. </UXML>
     
    Guedez likes this.
  3. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    Awesome, I assumed there would be a feature for that, but couldn't figure out where and how
     
  4. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    Actually, half what I needed. It seems only the first
    contentContainer="true"
    is considered, you can't have 3 "entry points", so if I want to add custom components to 3 different parts of a window, I would need all of my windows to have the same 3~
    Code (CSharp):
    1.   <Template src="WindowTemplate.uxml" name="window"/>
    2.   <Instance template="window" name="window">
    3.   </Instance>
    I would need something like:
    Code (CSharp):
    1.     <ui:Instance template="WorldTooltipBase" name="WorldTooltipBase" >
    2.         <ui:ContainerOverride element-name="Onecontainer">
    3.             <Label text="I'm the content A"/>
    4.         </ui:ContainerOverride>
    5.         <ui:ContainerOverride element-name="Someothercontainer">
    6.             <Label text="I'm the content B"/>
    7.         </ui:ContainerOverride>
    8.         <ui:ContainerOverride element-name="Thatothercontainer">
    9.             <Label text="I'm the content C"/>
    10.         </ui:ContainerOverride>
    11.     </ui:Instance>
     
  5. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    Yes, contentContainer is design to work with Add() and therefore is unique.

    We have multi-container feature support called "slots" in the backlog -- but it's not a high priority at the moment.
     
    Guedez likes this.
  6. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    what a shame