When using Unity style sheet (USS), you can specify values for VisualElement
properties in USS files. You can also assign property values in C#, using the C# properties of VisualElement
. Values assigned in C# override values from a USS file.
可使用自定义属性 来扩展 USS。自定义 USS 属性需要 --
前缀。
This page introduces the supported data types.
USS 数据类型定义 USS 属性接受的值和关键字。
Property | Description | |
---|---|---|
<length> |
Represents a distance value. | |
<number> |
Represents either an integer or a number with a fractional component. | |
<integer> |
Represents a whole number. | |
<color> |
Represents a color. You can define a color with a #hexadecimal code, the rgb() or rgba() function, or a color keyword (for example, blue or transparent ). |
|
<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. | |
<length>{1,4} |
Represents one to four options for properties that are sharthand for -top- , -bottom- , -left- , and -right- properties, such as padding-top , or border-top-width . |
|
1 length | Applies to all four properties. | |
2 lengths | The first applies to -top- and -bottom- . The second applies to -left- and -right- . |
|
3 lengths | The first applies to -top- . The second applies to -left and -right- . The third is applied to -bottom- . |
|
4 lengths | Applies in this order : -top- , -right , -bottom- , -left-
|
UI 工具包支持像素 (px
) 和百分比 (%
) 作为长度的度量单位。像素值是绝对值,而百分比通常相对于元素父级。
例如:
width:200px;
表示宽度为 200 像素。width:50%;
表示宽度为父元素宽度的一半。务必指定度量单位。如果未指定度量单位,则 UI 工具包会假定属性值以像素为单位。
注意:0
是一个不需要度量单位的特殊值。
数值表示为浮点或整数字面值。例如:flex:1.0
。
Specific keywords are supported for some built-in properties. Keywords provide a descriptive name instead of a number. For example: position:absolute
. All properties support the initial
global keyword which resets a property to its default value. See USS properties for a list of keywords.
UI 工具包支持以下字面颜色值和函数:
#FFFF00
(rgba,每个通道一个字节)、#0F0
(rgb)rgb(255, 255, 0)
rgba(255, 255, 0, 1.0)
blue
、transparent
您可以从 USS 文件中引用项目资源,例如字体和纹理。例如,您可以引用纹理作为元素的背景图像。
您可使用 url()
或 resource()
函数来引用资源。引用的资源将在样式表导入期间进行解析。
例如,下面的样式声明使用 resource()
函数引用 Images
目录中名为 img.png
的纹理资源,并将其指定为背景图像。
`background-image: resource("Images/img.png");
Unity 大多数情况下推荐使用 url()
函数。但 resource()
函数自动支持为不同的屏幕密度加载不同版本的图像资源。
当您使用 url()
函数引用资源时,指定的路径可以是相对的或绝对的:
路径必须包含文件扩展名。
例如,假设您的项目有一个包含所有样式表的 USS
文件夹,以及一个包含所有图像资源的 Resources 文件夹。
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") |
如果您需要支持不同屏幕密度 (DPI) 的屏幕,resource()
函数允许您自动加载正确版本的纹理资源。
您必须:
@2x
后缀。例如,myimage.png
的高 DPI 版本应为 myimage@2x.png
当 Unity 加载资源时,它会自动为当前 DPI 屏幕选择正确的版本。
例如,如果您在 USS 中使用 resource("myimage")
,Unity 将加载 Resources/myimage.png
或 Resources/myimage@2x.png
。
使用引号指定字符串值。例如:--my-property: "foo"
。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.