StyleRule

顯示已棄用項目

*此內容是使用 AI(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>#[名稱]</code></td>
<td>匹配特定 <code>Class.Instance.Name</code> 的實例。</td>
<td><code>="#ModalFrame"</code><code>#關閉按鈕</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)

預設值:""

返回

()

活動