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:
auto
, baseline
.<
and >
). For example: <length>
and <color>
.<'
和 '>
)之间。例如:<'width'>
。If a property value has more than one option, the following applies:
|
) separates two or more options. One of them must occur.|
|`) separate two or more options. One or more of them must occur in any order.&
&`) 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 |
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
是一个不需要度量单位的特殊值。
数值表示为浮点或整数字面值。例如: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;
}
UI 工具包支持以下字面颜色值和函数:
#FFFF00
(RGBA one byte per channel), #0F0
(RGB)rgb(255, 255, 0)
rgba(255, 255, 0, 1.0)
您可以从 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.
当您使用 url()
函数引用资源时,指定的路径可以是相对的或绝对的:
路径必须包含文件扩展名。
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")
|
resource()
函数可以引用 Unity 的资源文件夹中的资源(Resources
和 Editor 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") |
To support screens with different screen densities (DPI), do the following:
@2x
后缀。例如,myimage.png
的高 DPI 版本应为 myimage@2x.png
当 Unity 加载资源时,它会自动为当前 DPI 屏幕选择正确的版本。
例如,如果您在 USS 中使用 resource("myimage")
,Unity 将加载 Resources/myimage.png
或 Resources/myimage@2x.png
。
使用引号指定字符串值。例如:--my-property: "foo"
。