Version: 2022.2
USS properties
USS common properties

USS data types

USS data types define typical values, including keywords and units, that USS properties and functions accept.

语法

USS properties use the same syntax as W3C CSS documents:

  • Keyword values appear as is. For example: auto, baseline.
  • Basic data types appear between angle brackets ( < and >). For example: <length> and <color>.
  • 与属性同名的非尾端出现在尖括号与单直引号(<''>)之间。例如:<'width'>

If a property value has more than one option, the following applies:

  • Side-by-side words mean that all options must occur in the given order.
  • A bar (|) separates two or more options. One of them must occur.
  • Double bars (||`) separate two or more options. One or more of them must occur in any order.
  • Double ampersands (&&`) separate two or more options. All options must occur.
  • 方括号 ([ ]) 表示分组。

Every type, keyword, or angle-bracketed group might be followed by modifiers. The following table lists the possible modifiers:

Modifier The preceding type, keyword, or group
An asterisk (*) Occurs zero or more times
A plus sign (+) Occurs one or more times
A question mark (?) Is optional
A pair of numbers in curly braces ({A,B}) Occurs at least A and at most B times

Length

UI Toolkit supports pixels (px) and percentages (%) as units of measurement for length. Pixel values are absolute, while percentages are relative to the element’s parent.

例如:

  • width:200px; 表示宽度为 200 像素。
  • width:50%; 表示宽度为父元素宽度的一半。

It’s important to specify the unit of measurement. If you don’t specify a unit of measurement, UI Toolkit assumes that the property value is expressed in pixels.

注意:0 是一个不需要度量单位的特殊值。

Numeric

数值表示为浮点或整数字面值。例如:flex:1.0

关键字

Specific keywords are supported for some built-in properties. Keywords give a descriptive name instead of a number. For example: position:absolute.

All properties support the initial global keyword which resets the default value of a property to an element.

In the following example, although you set the color of all the labels to red, the initial keyword of color restores the color of label1 to its default value:

/* Set the color of all the labels to red. */
Label {
    color: red;
}
/* label1 is the name of a specific label. */
#label1{
    color: initial;
}

Color

UI 工具包支持以下字面颜色值和函数:

  • A Hexadecimal value: #FFFF00 (RGBA one byte per channel), #0F0 (RGB)
  • RGB 函数:rgb(255, 255, 0)
  • RGBA 函数:rgba(255, 255, 0, 1.0)
  • Color keywords

资源

您可以从 USS 文件中引用项目资源,例如字体和纹理。例如,您可以引用纹理作为元素的背景图像。

To reference an asset, you can use either the url() function or the resource() function:

  • resource(): Represents an asset in a Resources folder.
  • url(): Represents an asset specified by a path. You can express it as either a relative path or an absolute path.

Use the url() function in most cases. However, the resource() function supports automatically loading different versions of image assets for different screen densities.

Reference assets with the URL function

当您使用 url() 函数引用资源时,指定的路径可以是相对的或绝对的:

  • 相对路径必须相对于包含引用资源的 USS 文件的文件夹。
  • 绝对路径是相对于项目的。

路径必须包含文件扩展名。

For example, if your project has a USS folder that contains all your style sheets, and a Resources folder that contains all your image assets.

Assets
  └─ Editor
      └─ Resources
      └─ USS

要引用名为 thumb.png 的图像,您可以使用以下路径之一:

相对路径 绝对路径
url("../Resources/thumb.png") url("/Assets/Editor/Resources/thumb.png")

url("project:/Assets/Editor/Resources/thumb.png")

url("project:///Assets/Editor/Resources/thumb.png")

Reference assets with the resource function

resource() 函数可以引用 Unity 的资源文件夹中的资源(ResourcesEditor Default Resources)。您可以通过名称来引用资源。

  • 如果文件位于 Editor Default Resources Resources 文件夹中,必须包含文件扩展名。
  • 如果文件位于 Resources 文件夹中,则不得包含文件扩展名。

例如:

文件路径 引用语法
Assets/Resources/Images/my-image.png resource("Images/my-image")
Assets/Editor Default Resources/Images/my-image.png resource("Images/default-image.png")

Reference image assets for High DPI/Retina screens

To support screens with different screen densities (DPI), do the following:

  • 确保纹理的高 DPI 版本在其文件名中带有 @2x 后缀。例如,myimage.png 的高 DPI 版本应为 myimage@2x.png
  • 将纹理资源的常规和高 DPI 版本放在同一项目文件夹中。

当 Unity 加载资源时,它会自动为当前 DPI 屏幕选择正确的版本。

例如,如果您在 USS 中使用 resource("myimage"),Unity 将加载 Resources/myimage.pngResources/myimage@2x.png

Strings

使用引号指定字符串值。例如:--my-property: "foo"

其他资源

USS properties
USS common properties
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961