选择器确定 USS 规则影响哪些元素。当 Unity 将样式表应用于视觉树时,它会将元素与选择器进行匹配。如果元素与选择器匹配,Unity 会将选择器的样式规则应用于该元素。
USS 支持多种类型简单和复杂的选择器,根据不同条件匹配元素,例如:
name
属性它还支持伪类,可与选择器一起使用以定位处于特定状态的元素。
如果一个元素匹配多个选择器,Unity 会应用优先的选择器中的样式。
USS 支持一组简单的选择器,它们类似于 CSS 中的简单选择器,但不完全相同。它还支持 CSS 复杂选择器和伪类的一个子集。
下表提供了 USS 简单选择器的快速参考。有关详细信息,请参阅简单选择器。
选择器类型: | 语法: | 匹配: |
---|---|---|
C# 类型 | Type {...} |
特定于 C# 类型的元素。 |
USS 类 | .class {...} |
具有指定 USS 类的元素 |
名称 | #name {..} |
具有指定 name 属性的元素 |
通配符 | * {...} |
任意元素 |
下表提供了 USS 复杂选择器的快速参考。有关详细信息,请参阅复杂选择器。
选择器类型: | 语法: | 匹配: |
---|---|---|
后代选择器 | selector1 selector2 {...} |
在任何级别,第一个选择器的_任何_后代也匹配第二个选择器。 |
子选择器 | selector1 > selector2 {...} |
第一个选择器的_直接_后代(子选择器)也匹配第二个选择器。 |
选择器列表 | selector1, selector2, selector3 {..} |
与列表中的任何选择器匹配的任何元素。与列表中的任何选择器匹配的任何元素。列表可以包含简单或复杂选择器的任意组合。 |
下表提供了 USS 简单选择器的快速参考。有关详细信息,请参阅伪类。
伪类 | 元素匹配条件 |
---|---|
:hover |
光标位于元素上。 |
:active |
用户与元素交互。 |
:inactive |
用户停止与元素交互。 |
:focus |
元素具有焦点。 |
:selected |
不可用。Unity 不使用这种伪状态。 |
:disabled |
元素设置为 enabled == false 。 |
:enabled |
元素设置为 enabled == true 。 |
:checked |
元素是一个 Toggle 元素并已启用。 |
:root |
元素是根元素(视觉树中的最高层级元素)。 |
要将同一样式应用于多种类型的元素,您可以在任何样式规则中使用选择器列表。选择器列表由两个或多个用逗号分隔的选择器组成。它可以包含简单和复杂选择器的任意组合,并匹配与列表中至少一个选择器匹配的任何元素。
语法:
selector1, selector2 {...}
一个选择器列表相当于多个选择器,每个选择器都声明了相同的样式规则。
示例:
对于上面的示例 UXML 文档,以下两个 USS 代码片段具有相同的效果。
# container2, Button {
background-color: pink;
border-radius: 10px;
}
# container2 {
background-color: pink;
border-radius: 10px;
}
Button {
background-color: pink;
border-radius: 10px;
}
当一个元素匹配多个选择器时,Unity 会考虑几个因素来确定哪个选择器优先。
Unity 如何确定优先级取决于冲突的选择器是在同一个样式表中还是在不同的样式表中。
如果一个元素匹配多个选择器,则具有最高特异性的选择器优先。
如果两个选择器具有相同的特异性,USS 文件中最后出现的选择器优先。
当一个元素匹配不同样式表中的多个选择器时,Unity 会根据以下因素按以下顺序确定优先级:
选择器特异性是相关性的度量。特异性越高,选择器与其匹配的元素越相关。
*
) 选择器特异性高。直接针对元素的样式优先于元素继承的样式,即使继承的样式是在具有更高特异性的选择器中定义的。
直接应用于元素的样式会覆盖通过 USS 应用的样式。
注意: |
---|
USS 不支持用于覆盖 CSS 中的样式声明的 !important 规则。 |
应用于 UXML 文档中元素的内联样式优先于 USS 样式。您可以认为它们比 USS 选择器具有更高的特异性。
您在 C# 中设置的样式覆盖任何其他样式,包括 USS 样式和内联样式。您可以认为它们具有最高的特异性。
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.