StyleRule

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

定义覆盖受 Selector 属性影响的实例属性的风格属性。

概要

属性

  • 读取并联

    一个数字,决定 StyleRule 对其他 StyleRules 中相同属性的应用方式。优先级更高的值在更低的值之前取得优先权。

  • 读取并联

    一串指定哪些实例 StyleRule 应影响的字符串。

  • 只读
    未复制
    读取并联

    一个只读的字符串,用于显示 Selector 属性的错误。

方法

继承自StyleBase方法

活动

继承自StyleBase活动

属性

Priority

读取并联

一个数字,决定 StyleRule 对其他 StyleRules 中相同属性的应用方式。优先级更高的值在更低的值之前取得优先权。例如,如果一个具有优先级 的 与 属性的 具有 属性,它将优先于低优先级 具有 属性。

Selector

读取并联

一串指定哪些实例 StyleRule 应影响的字符串。这可以是选择器和组合器的混合物,用于匹配特性,例如类名、实例名和层次关系。

例如, 有效地意味着风格规则覆盖了每个 标有 ( >)标签的实例,并且 被标记为 ( >),而 和 处于 状态( >)。

选择器

<th>描述</th>
<th>例子</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>[类]</code></td>
<td>匹配 <code>Class.GuiObject</code> 或 <code>Class.UIComponent</code> 类的实例。</td>
<td><code>“框架”</code><code>“图像按钮”</code><code>“UICorner”</code></td>
</tr>
<tr>
<td><code>.[标签]</code></td>
<td>匹配标有 <a href="/studio/properties#instance-tags">标签</a> 的实例与 <code>Class.CollectionService</code> 标签。</td>
<td><code>“.Container”</code><code>“.BlueOnHover”</code></td>
</tr>
<tr>
<td><code>#name</code></td>
<td>匹配特定 <code>Class.Instance.Name</code> 的实例。</td>
<td><code>#ModalFrame</code><code>#CloseButton</code></td>
</tr>
<tr>
<td><code>:[状态]</code></td>
<td>匹配当前在 <code>Enum.GuiState</code> 中的实例。</td>
<td><code>":悬停"</code></td>
</tr>
</tbody>
选择器
组合器

<th>描述</th>
<th>例子</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>></code></td>
<td>匹配以前过滤匹配的直接子实例<b></b>。</td>
<td width="40%"><code>“框架 > .Inventory”</code></td>
</tr>
<tr>
<td><code>>></code></td>
<td>匹配以前过滤匹配的<b>后裔</b>实例。</td>
<td><code>“图像按钮 >> .BlueOnHover”</code></td>
</tr>
<tr>
<td><code>,</code></td>
<td>指定风格规则的多个独立选择器列表。</td>
<td><code>“Frame.TagA, TextLabel.TagA”</code></td>
</tr>
<tr>
<td><code>::</code></td>
<td>在以前的过滤匹配下创建一个幻影 <code>Class.UIComponent</code> 实例,并将样式规则的属性应用到它。</td>
<td><code>“框架::UICorner”</code></td>
</tr>
</tbody>
组合器

代码示例

The following example shows how to define a class selector which targets all TextButton instances and styles them with blue background and light grey text. To test, paste the code into a LocalScript which is a child of a ScreenGui located inside the StarterGui container.

UI Class Selector

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local screenGui = script.Parent
local coreSheet = Instance.new("StyleSheet")
coreSheet.Parent = ReplicatedStorage
local styleLink = Instance.new("StyleLink")
styleLink.StyleSheet = coreSheet
styleLink.Parent = screenGui
local rule = Instance.new("StyleRule")
rule.Parent = coreSheet
-- Class selector
rule.Selector = "TextButton"
-- Set rule properties
rule:SetProperties({
["BackgroundColor3"] = Color3.fromHex("335FFF"),
["TextColor3"] = Color3.fromHex("E1E1E1"),
["Size"] = UDim2.new(0.15, 0, 0, 40),
["BorderSizePixel"] = 0
})
local button = Instance.new("TextButton")
button.Text = "Main Menu"
button.Parent = screenGui

The following example shows how to define a tag selector which utilizes tags applied through CollectionService to target a TextButton tagged with ButtonPrimary. To test, paste the code into a LocalScript which is a child of a ScreenGui located inside the StarterGui container.

UI Tag Selector

local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local screenGui = script.Parent
local coreSheet = Instance.new("StyleSheet")
coreSheet.Parent = ReplicatedStorage
local styleLink = Instance.new("StyleLink")
styleLink.StyleSheet = coreSheet
styleLink.Parent = screenGui
local rule = Instance.new("StyleRule")
rule.Parent = coreSheet
-- Tag selector
rule.Selector = ".ButtonPrimary"
-- Set rule properties
rule:SetProperties({
["BackgroundColor3"] = Color3.fromHex("FF0099"),
["TextColor3"] = Color3.fromHex("E1E1E1"),
["Size"] = UDim2.new(0.15, 0, 0, 40),
["BorderSizePixel"] = 0
})
local button = Instance.new("TextButton")
button.Text = "Main Menu"
button.Parent = screenGui
-- Apply tag to button
CollectionService:AddTag(button, "ButtonPrimary")

The following example shows how to define a UI modifier selector which applies a phantom UICorner instance to a Frame tagged with RoundedCorner20. To test, paste the code into a LocalScript which is a child of a ScreenGui located inside the StarterGui container.

UI Modifier Selector

local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local screenGui = script.Parent
local coreSheet = Instance.new("StyleSheet")
coreSheet.Parent = ReplicatedStorage
local styleLink = Instance.new("StyleLink")
styleLink.StyleSheet = coreSheet
styleLink.Parent = screenGui
local rule = Instance.new("StyleRule")
rule.Parent = coreSheet
-- UI component selector
rule.Selector = "Frame.RoundedCorner20::UICorner"
-- Set rule property
rule:SetProperty("CornerRadius", UDim.new(0, 20))
-- Create frame
local frame = Instance.new("Frame")
frame.Size = UDim2.new(0.4, 0, 0.2, 0)
frame.Parent = screenGui
-- Apply tag to frame
CollectionService:AddTag(frame, "RoundedCorner20")

SelectorError

只读
未复制
读取并联

一个只读的字符串,用于显示 Selector 属性的错误,例如语法错误、未支持的类型等

方法

GetProperties

返回描述 StyleRule 属性的键值对词典,例如:


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
-- 获取风格规则的参考
local frameRule = coreSheet.Frame
local props = frameRule:GetProperties()
print(props)
--[[
{
["AnchorPoint"] = 0.5, 0,
["BackgroundColor3"] = 1, 0, 0.25
}
]]

返回

描述 StyleRule 属性的键值对词典。

GetProperty

Variant

返回特定属性在 StyleRule 的值。


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
-- 获取风格规则的参考
local frameRule = coreSheet.Frame
local prop = frameRule:GetProperty("AnchorPoint")
print(prop) --> 0.5, 0

参数

name: string

属性的字符串名称,例如 "AnchorPoint""BackgroundColor3" .

默认值:""

返回

Variant

属性的值。

SetProperties

()

SetProperty() 类似,但允许一次宣言和设置多个 StyleRule 的属性。每个任务应为受影响的 GuiObjectUIComponent ( UICorner , UIGradient ,等)的有效属性,每个分配的值应与其属性类型匹配,例如 Vector2 对于 AnchorPointColor3 对于 BackgroundColor3

尝试分配无效属性名称,例如 "AnchorPt""BkColor" 将静默失败。类型匹配错误,例如 CFrame 对于 AnchorPointUDim2 对于 BackgroundColor3 也会失败,并且在 输出窗口 中会出现错误。

要设置/更新 StyleRule 的仅一个属性,请参阅 SetProperty()


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
-- 获取风格规则的参考
local frameRule = coreSheet.Frame
-- 设置规则属性
frameRule:SetProperties({
["AnchorPoint"] = Vector2.new(0.5, 0),
["BackgroundColor3"] = Color3.new(1, 0, 0.25)
})

请注意,您可以通过 **** 前缀将 $ 代币分配为属性值:


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
local tokensSheet = ReplicatedStorage:FindFirstChild("Tokens")
-- 在代币表中设置代币(属性)
tokensSheet:SetAttribute("TopCenterAnchor", Vector2.new(0.5, 0))
tokensSheet:SetAttribute("MainBackgroundColor", Color3.new(0.2, 0.2, 0.3))
-- 获取风格规则的参考
local frameRule = coreSheet.Frame
-- 设置规则属性
frameRule:SetProperties({
["AnchorPoint"] = "$TopCenterAnchor",
["BackgroundColor3"] = "$MainBackgroundColor"
})

参数

styleProperties: Dictionary

定义要设置的属性的键值对词典。

默认值:""

返回

()

SetProperty

()

StyleRule 设置新属性 (或修改现有属性)。参数 name 应为受影响的 GuiObjectUIComponent ( UICorner , UIGradient ,等)的有效属性,并且分配的值应与属性的值类型匹配,例如 Vector2 对于 AnchorPointColor3 对于 BackgroundColor3

尝试分配无效属性名称,例如 "AnchorPt""BkColor" 将静默失败。类型匹配错误,例如 CFrame 对于 AnchorPointUDim2 对于 BackgroundColor3 也会失败,并且在 输出窗口 中会出现错误。

要一次设置多个属性给 StyleRule ,请参阅 SetProperties()


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
-- 获取风格规则的参考
local frameRule = coreSheet.Frame
-- 设置规则属性
frameRule:SetProperty("BackgroundColor3", Color3.new(1, 0, 0.25))

请注意,您可以通过 **** 前缀将 $ 代币分配为属性值:


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
local tokensSheet = ReplicatedStorage:FindFirstChild("Tokens")
-- 在代币表中设置新代币(属性)
tokensSheet:SetAttribute("MainBackgroundColor", Color3.new(0.2, 0.2, 0.3))
-- 获取风格规则的参考
local frameRule = coreSheet.Frame
-- 将规则属性设置为使用代币作为其值
frameRule:SetProperty("BackgroundColor3, "$MainBackgroundColor")

参数

name: string

要设置的属性名称,例如 "BackgroundColor3" .

默认值:""
value: Variant

设置的属性值,例如 Color3.new(1, 0, 0.25) .

默认值:""

返回

()

活动