使用 USS 时,可为内置的 VisualElement
属性或 UI 代码中的自定义属性指定值。
除了从 USS 文件中读取值之外,还可以使用 VisualElement
的 C# 属性在 C# 中分配内置属性值。在 C# 中分配的值将覆盖 Unity 样式表 (USS) 中的值。
可使用自定义属性 API 来扩展 USS。
本节列出了支持的类型。
以下关键字具有特殊含义:
auto
inherit
unset
true
false
none
限制:关键字 auto
、inherit
和 unset
会正确导入,但在运行时不受支持。
UIElements 不支持单位。所有属性类型都应该以像素为单位指定。
数值是浮点或整数字面值。例如:flex:1.0
或 width:200
。数值存储为浮点数,包括整数字面值。
只有基于枚举的内置属性才支持枚举。枚举提供可读名称而不是数字。例如:position-type:absolute
。
UIElements 支持以下字面颜色值和函数:
#FFFF00
(rgba,每个通道一个字节)、#0F0
(rgb)rgb(255, 255, 0)
rgba(255, 255, 0, 1.0)
hsl(0, 100%, 50%)
可使用 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")
。url()
函数要求文件路径相对于项目根目录或包含 USS 文件的文件夹。必须包含文件扩展名。例如:background-image: url("Images/my-image.png")
。
对于纹理,如果文件的版本具有 @2x
后缀,则会自动为 Retina 或高 DPI 屏幕加载此文件。
使用引号指定字符串值。例如: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.