定义覆盖受 Selector 属性影响的实例属性的风格属性。
概要
属性
一个数字,决定 StyleRule 对其他 StyleRules 中相同属性的应用方式。优先级更高的值在更低的值之前取得优先权。
一串指定哪些实例 StyleRule 应影响的字符串。
一个只读的字符串,用于显示 Selector 属性的错误。
方法
返回描述 StyleRule 属性的键值对词典。
返回特定属性在 StyleRule 的值。
让你一次宣言并设置多个属性的 StyleRule 。
返回一组相关的 StyleRules 。
插入新的 StyleRule 到规则阵列中。
与 InsertStyleRule() 类似,但允许一次宣言和设置多个 StyleRules。
属性
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.
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.
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.
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")
方法
GetProperties
返回描述 StyleRule 属性的键值对词典,例如:
local ReplicatedStorage = game:GetService("ReplicatedStorage")local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")-- 获取风格规则的参考local frameRule = coreSheet.Framelocal props = frameRule:GetProperties()print(props)--[[{["AnchorPoint"] = 0.5, 0,["BackgroundColor3"] = 1, 0, 0.25}]]
返回
描述 StyleRule 属性的键值对词典。
GetProperty
返回特定属性在 StyleRule 的值。
local ReplicatedStorage = game:GetService("ReplicatedStorage")local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")-- 获取风格规则的参考local frameRule = coreSheet.Framelocal prop = frameRule:GetProperty("AnchorPoint")print(prop) --> 0.5, 0
参数
属性的字符串名称,例如 "AnchorPoint" 或 "BackgroundColor3" .
返回
属性的值。
SetProperties
与 SetProperty() 类似,但允许一次宣言和设置多个 StyleRule 的属性。每个任务应为受影响的 GuiObject 或 UIComponent ( UICorner , UIGradient ,等)的有效属性,每个分配的值应与其属性类型匹配,例如 Vector2 对于 AnchorPoint 或 Color3 对于 BackgroundColor3 。
尝试分配无效属性名称,例如 "AnchorPt" 或 "BkColor" 将静默失败。类型匹配错误,例如 CFrame 对于 AnchorPoint 或 UDim2 对于 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"})
参数
定义要设置的属性的键值对词典。
返回
SetProperty
为 StyleRule 设置新属性 (或修改现有属性)。参数 name 应为受影响的 GuiObject 或 UIComponent ( UICorner , UIGradient ,等)的有效属性,并且分配的值应与属性的值类型匹配,例如 Vector2 对于 AnchorPoint 或 Color3 对于 BackgroundColor3 。
尝试分配无效属性名称,例如 "AnchorPt" 或 "BkColor" 将静默失败。类型匹配错误,例如 CFrame 对于 AnchorPoint 或 UDim2 对于 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")
参数
要设置的属性名称,例如 "BackgroundColor3" .
设置的属性值,例如 Color3.new(1, 0, 0.25) .