使用 USS 时,可为内置的 VisualElement
属性或 UI 代码中的自定义属性指定值。
除了从 USS 文件中读取值之外,还可以使用 VisualElement
的 C# 属性在 C# 中分配内置属性值。在 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
可使用 resource()
或 url()
函数来引用资源。例如,指定 background-image: resource("Images/img.png")
可将 Images
目录中的 img.png
指定为背景图像。导入期间将解析引用的资源。
resource()
函数接受位于 Resources
文件夹或 Editor Default Resources
文件夹下的文件,但注意以下几点:
Resources
文件夹下,不要包含文件扩展名。例如:background-image: resource("Images/my-image")
。Editor Default Resources
下,必须包含文件扩展名。例如:background-image: resource("Images/default-image.png")
。此外,在加载纹理时,resource()
提供了一种处理高 DPI/Retina 屏幕的便捷方法。如果在同一位置存在具有相同文件名和后缀 @2x
的纹理,Unity 将根据屏幕 DPI 自动加载该纹理。
例如,如果在 USS 中使用 resource("myimage")
,Unity 将加载 Resources/myimage.png 或 Resources/myimage@2x.png。
url()
函数要求文件路径相对于项目根目录或包含 USS 文件的文件夹。必须包含文件扩展名。在以下示例中,USS 文件位于 Assets\Editor\USS 中,_thumb.png_ 背景图像位于 Assets\Editor\Resources 中:
url("../Resources/thumb.png");
url("/Assets/Editor/Resources/thumb.png");
、url("project:/Assets/Editor/Resources/thumb.png");
或 url("project:///Assets/Editor/Resources/thumb.png");
例如:background-image: url("Images/my-image.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.