使用 USS 时,可为内置的 VisualElement
属性或 UI 代码中的自定义属性指定值。
除了从 USS 文件中读取值之外,还可以使用 C#(通过 C# 的 VisualElement
属性)指定内置属性值。使用 C# 指定的值将覆盖 Unity 样式表 (USS) 中的值。
可使用自定义属性 来扩展 USS。自定义 USS 属性需要 --
前缀。
本节列出了支持的类型。
UI 工具包支持像素 (px
) 和百分比 (%
) 作为长度的度量单位。像素值是绝对值,而百分比通常相对于元素父级。
例如:
width:200px;
表示宽度为 200 像素。width:50%;
表示宽度为父元素宽度的一半。务必指定度量单位。如果未指定度量单位,则 UI 工具包会假定属性值以像素为单位。
注意:0
是一个不需要度量单位的特殊值。
数值表示为浮点或整数字面值。例如:flex:1.0
。
某些内置属性支持特定关键字。关键字提供描述性名称而不是数字。例如:position:absolute
。所有属性都支持 initial
全局关键字,该关键字将属性重置为默认值。请参阅支持的属性以查看关键字的列表。
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.