文档

Unity Engine

Download Unity

Unity Engine

Relative and absolute positioning

Use relative and absolute positioning to lay out UI in C#.
阅读时间3 分钟最后更新于 14 天前

Version: 6000.5+
This example demonstrates the difference between relative and absolute positioning. This example also demonstrates how to use C# and UXML/USS to add and style UI controls.

Example overview

The example uses the automatic layout system to add boxes to an Editor and a runtime UI. One box demonstrates a relative offset of
25 px
, while another box demonstrates the absolute position of
25 px, 25 px
.
The example structures the Editor UI with C# script and the runtime UI with UXML and CSS.

Visual element positioning

You can find the completed files that this example creates in this GitHub repository.

Prerequisites

This guide is for developers familiar with the Unity Editor, UI Toolkit, and C# scripting. Before you start, get familiar with the following:

Create the example for the Editor UI

To create the example for the Editor UI (four gray boxes for comparison, one black box with absolute positioning, one purple box with relative positioning):
  1. Create a Unity project with any template.
  2. In the Project window, create a folder named
    Editor
    .
  3. In the
    Editor
    folder, create a C# file named
    PositioningTestWindow.cs
    with the following content:
    using UnityEditor;using UnityEngine;using UnityEngine.UIElements;public class PositioningTestWindow : EditorWindow{ [MenuItem("UI Toolkit Examples/Positioning Test Window")] public static void ShowExample() { var wnd = GetWindow<PositioningTestWindow>(); wnd.titleContent = new GUIContent("Positioning Test Window"); } public void CreateGUI() { VisualElement root = rootVisualElement; for (int i = 0; i < 2; i++) { var temp = new VisualElement(); temp.style.width = 70; temp.style.height = 70; temp.style.marginBottom = 2; temp.style.backgroundColor = Color.gray; root.Add(temp); } // Relative positioning var relative = new Label("Relative\nPos\n25, 0"); relative.style.width = 70; relative.style.height = 70; relative.style.left = 25; relative.style.marginBottom = 2; relative.style.backgroundColor = new Color(0.2165094f, 0, 0.254717f); root.Add(relative); for (int i = 0; i < 2; i++) { var temp = new VisualElement(); temp.style.width = 70; temp.style.height = 70; temp.style.marginBottom = 2; temp.style.backgroundColor = Color.gray; root.Add(temp); } // Absolute positioning var absolutePositionElement = new Label("Absolute\nPos\n25, 25"); absolutePositionElement.style.position = Position.Absolute; absolutePositionElement.style.top = 25; absolutePositionElement.style.left = 25; absolutePositionElement.style.width = 70; absolutePositionElement.style.height = 70; absolutePositionElement.style.backgroundColor = Color.black; root.Add(absolutePositionElement); }}
  4. To test the example, from the menu, select UI Toolkit Examples > Positioning Test Window.

Create the example for the runtime UI

To create the example for the runtime UI:
  1. Create a USS file named
    PositioningTest.uss
    with the following content:
    .box { height: 70px; width: 70px; margin-bottom: 2px; background-color: gray; }#relative{ width: 70px; height: 70px; background-color: purple; left: 25px; margin-bottom: 2px; position:relative;}#absolutePositionElement{ left: 25px; top: 25px; width: 70px; height: 70px; background-color: black; position: absolute;}
  2. Create a UXML document named
    PositioningTest.uxml
    with the following content:
    <ui:UXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False"> <Style src="project://database/Packages/com.unity.documentation-examples/UIToolkit/relative-and-absolute-position/PositioningTest.uss?fileID=7433441132597879392&amp;guid=5431fce9478274238800565a45373e0b&amp;type=3#PositioningTest"/> <ui:VisualElement class="box"/> <ui:VisualElement class="box"/> <ui:Label text="Relative&#10;Pos&#10;25, 0" name="relative" triple-click-selects-line="false" display-tooltip-when-elided="false"/> <ui:VisualElement class="box"/> <ui:VisualElement class="box"/> <ui:Label text="Absolute&#10;Pos&#10;25, 25" name="absolutePositionElement"/></ui:UXML>
  3. Right-click in the Hierarchy window, and then select UI Toolkit > Panel Renderer.
  4. In the
    Inspector

    ?

    window of the Panel Renderer, select Panel Renderer > Source Asset > PositioningTest.
  5. Enter Play mode and adjust the resolution as necessary to view the result.

Additional resources