Version: Unity 6.7 Beta (6000.7)
Language : English
UI authoring interface
Work with elements in-scene

Get started with in-scene UI authoring

This example shows how to create UI with in-scene UI authoring. It follows the same workflow as Get started with UI Builder, but uses in-scene UI authoring instead.

Prerequisites

Before you start, familiarize yourself with the following:

Example overview

This example creates the same character selection screen as Get started with UI Builder, using in-scene UI authoring instead.

Create the root element

Create a root visual element that covers the entire screen. Set your root element to have a background color and center all child elements in the middle of the screen.

To create the root element:

  1. Create a project in Unity with any template.

  2. Create a Panel Renderer and a UXML Document named as MainView.uxml. For instructions, refer to Create a Panel Renderer and a UXML Document.

  3. Enable in-scene UI authoring.

  4. In the Hierarchy window, select the arrow icon (>) next to MainView.uxml to enter the UI Staging mode.

  5. In the UI Staging mode, right-click anywhere in the Hierarchy window, and select UI Toolkit > Standard Elements > Visual Element to add a visual Element as the root element.

    Create new elements with in-scene UI authoring
    Create new elements with in-scene UI authoring
  6. Select the element from the Hierarchy panel.

  7. In the Inspector panel, make sure Flex > Grow is 1. This sets the flex-grow property to 1, making it cover the entire screen.

    Set the Flex property
    Set the Flex property
  8. To center all child elements, set both Align Items and Justify Content to Center.

    Center children
    Center children
  9. Set Background > Color to #732526.

    Note: By default, the Alpha value for colors is 0, making them fully transparent. To make color opaque, set the Alpha value to 255.

    Root element background color
    Root element background color

Create the parent container

Create a new visual element underneath the root element. This element becomes the parent container for the left and right sections of the UI.

To create the parent container:

  1. Right-click the visual element you just created in the Hierarchy window. Select Standard Elements > Visual Element to add a child visual element.

  2. Select the element from the Hierarchy panel.

  3. In the Inspector panel, set Flex > Direction to row.

  4. Set Flex > Grow to 0.

  5. Set Size > Height to 350 pixels.

    Center container properties
    Center container properties

Create the character names list container

Add a list view as the child element of the container to hold the character names.

To create the character names list container:

  1. Select the visual element you just created and select Standard Elements > UI Library.

  2. Under Containers, drag List View to the container visual element in the Hierarchy window.

  3. Select the element from the Hierarchy window.

  4. In the Inspector window, set its name to CharacterList.

    Background container with the empty element inside
    Background container with the empty element inside
  5. Set Size > Width to 230 pixels.

  6. Set Spacing > Margin > Right to 6 pixels.

    Size and Margin foldouts for the character list
    Size and Margin foldouts for the character list
  7. Set Background > Color to #6E3925.

  8. Set Border > Color to #311A11.

  9. Set Border > Width to 4 pixels.

  10. Set Border > Radius to 15 pixels.

    Styled character list
    Styled character list

Create the character details container

Add a new visual element under the same parent as the #CharacterList to hold the character details container. The purpose is that when the user selects a character from the list on the left, it displays the character’s portrait, name, and class.

To create the character details container:

  1. Right-click the parent container visual element in the Hierarchy window. Select Standard Elements > Visual Element.

  2. Select the element from the Hierarchy panel.

  3. In the Inspector panel, set Align > Align Items to flex-end.

  4. Set Align > Justify Content to space-between.

    Justify content property
    Justify content property
  5. Add another VisualElement as the child of the right container.

  6. Select the element from the Hierarchy panel.

  7. Set Flex > Grow to 0.

  8. Set Size > Width to 276 pixels.

  9. In the Align section, set both Align Items and Justify Content to Center.

  10. Set Spacing > Padding to 8 pixels.

    Properties of the character details container
    Properties of the character details container
  11. Set Background > Color to #AA5939.

Create a USS class for the border style

The character details container uses the same border styles as the character names list container. Create a USS class to apply to both containers.

To create a USS class for the border style:

  1. Select Window > UI Toolkit > Style Sheets to open the Style Sheets window.
  2. In the Style Sheets panel, select + > Create New USS.
  3. Name it MainView.uss and select Save.
  4. Click in the Add new selector… field and enter .border. A .border selector appears in the Style Sheets panel.
  5. In the Style Sheets panel, select .border.
  6. In the Inspector panel, set Border > Color to #311A11.
  7. Set Border > Width to 4 pixels.
  8. Set Border > Radius to 15 pixels.
  9. Drag .border from the Style Sheets panel to the character details container VisualElement.
  10. Drag .border from the Style Sheets panel to #CharacterList.
  11. Select #CharacterList.
  12. In the Inspector panel, right-click Border and select Unset to remove the inline style you set earlier.

Your UI layout now looks like the following:

Empty character details panel
Empty character details panel

Create the character portrait background

Add the individual UI controls to the character details container. The first step is to add the character portrait background.

To create the character portrait background:

  1. Add a visual element to the child of the character details container.

  2. Select the element from the Hierarchy panel.

  3. In the Size section, set both Width and Height to 120 pixels.

  4. Set Spacing > Padding to 4 pixels.

  5. Set Background > Color to #FF8554.

  6. Set Border > Color to #311A11.

  7. Set Border > Width to 2 pixels.

  8. Set Border > Radius to 13 pixels.

    Background frame for the character portrait
    Background frame for the character portrait

Create labels

Add two label controls to the character details container to display the selected character’s name and class.

To create labels:

  1. Right-click the parent of the visual element you just created in the Hierarchy window and select Standard Elements > Label.

  2. Set its name to CharacterName.

  3. Create another Label from the character details container in the Hierarchy window.

  4. Set its name to CharacterClass.

    Add labels for name and class
    Add labels for name and class
  5. Select the #CharacterName element.

  6. Set Text > Font Style to B.

  7. Set Text > Size to 18 pixels.

  8. Select File > Save to save the changes to MainView.uxml.

Extract inline styles

In the in-scene UI authoring, you can create elements and use inline styles only to experiment while the number of elements is still small. As you build a more complex UI, it’s easier to manage styles using style sheets. You can extract inline styles to a style sheet in in-scene UI authoring.

To extract inline styles to a style sheet:

  1. Select the root visual element.

  2. In the Inspector panel, in the Style Class List field, enter .background as the class name.

  3. Select Extract Inlined Styles to New Class. This creates a .background class selector with the inline styles you set for the root element and updates the UI Document (UXML) for the root visual element to use the class selector instead of the inline styles.

    Extract inline styles to a new class
    Extract inline styles to a new class
  4. Select File > Save.

If you want to continue to work on the Create a list view runtime UI example, you can repeat the steps to extract styles for all the other elements, and follow the instructions to create the example.

Additional resources

UI authoring interface
Work with elements in-scene