UI 樣式

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

UI 樣式是 Roblox 提供的樣式表解決方案,類似於 CSS,它讓您宣告並全球應用 UI 實例屬性的覆蓋。這種引擎級的支持是 樣式編輯器 和端到端代幣管道的基礎。

概念

樣式 規則StyleSheet 的一部分)適用於符合規則的 Selector 定義的每個實例,以匹配類名、實例名和層次結構關係等特徵。詳情請參閱 Selector 文檔。

樣式 代幣,通過 屬性 定義於代幣 StyleSheet 中,代表可以在樣式和組件中使用的 UI 屬性變量,例如 Frame.BackgroundColor3TextLabel.TextColor3UIStroke.Color 的共同顏色。代幣與 CSS 變量 類似。

樣式 主題,通過主題 StyleSheet屬性 配置,由一組特定代幣組成,可以互相替換,例如定義「淺色」和「深色」主題的顏色代幣。相關主題必須擁有相同的代幣集才能正常工作。

樣式 查詢 允許您根據 UI 的上下文或環境有條件地應用樣式。這些類似於 CSS 容器查詢和媒體查詢,使 UI 能夠根據螢幕大小、輸入類型或用戶輔助設置進行調整。

規則傳播

StyleLink 實例將 StyleSheet 及其相關規則鏈接到父 ScreenGui 和其中的所有 GuiObjects。每棵樹只允許應用一個 StyleSheet

選擇器定義

在高層次上,通過規則的 Selector 定義進行的實例匹配和修改操作如下:

以下設置展示了如何使用大小和顏色代幣、一個主題和類別選擇器 "Frame" 生成一個 200×200 像素的洋紅色 Frame

UI 類別選擇器

local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local screenGui = script.Parent
-- 代幣
local tokens = Instance.new("StyleSheet")
tokens.Name = "Tokens"
tokens.Parent = ReplicatedStorage
tokens:SetAttribute("SquareS", UDim2.fromOffset(50, 50))
tokens:SetAttribute("SquareM", UDim2.fromOffset(100, 100))
tokens:SetAttribute("SquareL", UDim2.fromOffset(200, 200))
tokens:SetAttribute("Fit", UDim2.fromScale(1, 1))
tokens:SetAttribute("Magenta", Color3.fromHex("FF0099"))
tokens:SetAttribute("Gold", Color3.fromHex("FFCC00"))
tokens:SetAttribute("Aqua", Color3.fromHex("0093F0"))
-- ThemeA
local themeA = Instance.new("StyleSheet")
themeA.Name = "ThemeA"
themeA.Parent = ReplicatedStorage
local tokensDerive = Instance.new("StyleDerive")
tokensDerive.StyleSheet = tokens -- 繼承全局代幣
tokensDerive.Parent = themeA
themeA:SetAttribute("FrameSize", "$SquareM")
themeA:SetAttribute("FrameColor", "$Aqua")
-- ThemeB
local themeB = Instance.new("StyleSheet")
themeB.Name = "ThemeB"
themeB.Parent = ReplicatedStorage
local tokensDerive = Instance.new("StyleDerive")
tokensDerive.StyleSheet = tokens -- 繼承全局代幣
tokensDerive.Parent = themeB
themeB:SetAttribute("FrameSize", "$SquareL")
themeB:SetAttribute("FrameColor", "$Magenta")
-- DesignSheet
local designSheet = Instance.new("StyleSheet")
designSheet.Name = "DesignSheet"
designSheet.Parent = ReplicatedStorage
local themeDerive = Instance.new("StyleDerive")
themeDerive.StyleSheet = themeB -- 繼承自 ThemeB
themeDerive.Parent = designSheet
-- 將 DesignSheet 鏈接到 ScreenGui
local styleLink = Instance.new("StyleLink")
styleLink.StyleSheet = designSheet
styleLink.Parent = screenGui
-- 配置規則
local rule = Instance.new("StyleRule")
rule.Selector = "Frame" -- 類別選擇器
rule.Parent = designSheet
-- 設置規則屬性
rule:SetProperties({
["BackgroundColor3"] = "$FrameColor",
["Size"] = "$FrameSize",
["BorderSizePixel"] = 0
})
local frame = Instance.new("Frame")
frame.Parent = screenGui

修改過的屬性

受到樣式影響的實例屬性在 屬性 窗口中會用 標記,將鼠標懸停在受影響的屬性上會顯示影響它的樣式。例如,如果樣式規則告訴標記的 Frame 使用 AnchorPoint 值為 0.5, 0.5,則該物件屬性將顯示 默認0, 0,但其實際的 AnchorPoint 將使用規則的 樣式化 值。

當默認或樣式化的屬性值進一步為特定 UI 物件修改時,將用 粗體 來標示被覆蓋的值。例如 — 假設 AnchorPoint 的默認值為 0, 0 — 為特定實例將該屬性設置為 1, 1 會揭示一個粗體的覆蓋:

對於任何被覆蓋的屬性值,您可以在 屬性 窗口中右鍵單擊它,然後選擇 重設到默認 以將其恢復為樣式值,或將其恢復為默認屬性值(如果尚未被樣式化)。

©2026 Roblox Corporation、Roblox、Roblox 標誌及 Powering Imagination 是我們在美國及其他國家地區的部分註冊與未註冊商標。