StyleRule
*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.
Xác định các thuộc tính phong cách làm hỏng các thuộc tính trên các thực thể bị ảnh hưởng bởi thuộc tính Selector .
Tóm Tắt
Thuộc Tính
Một số xác định cách các thuộc tính của StyleRule áp dụng so với các thuộc tính tương tự trong StyleRules khác.Giá trị ưu tiên cao hơn có ưu tiên hơn giá trị thấp hơn.
Một chuỗi xác định các ví dụ mà StyleRule nên ảnh hưởng đến.
Một chuỗi chỉ đọc hiển thị lỗi từ thuộc tính Selector.
Phương Pháp
Trả về một từ điển các cặp chìa khóa-giá trị mô tả các thuộc tính của StyleRule .
Trả giá trị của một thuộc tính cụ thể trong StyleRule .
Cho phép bạn tuyên bố và đặt nhiều thuộc tính của StyleRule tại một lúc.
Trả về một array của các StyleRules.
Chèn một mới StyleRule vào mảng các quy tắc.
Tương tự như InsertStyleRule() nhưng cho phép bạn tuyên bố và đặt nhiều StyleRules cùng một lúc.
Sự Kiện
Sự Kiện kế thừa từ StyleBaseBắt lửa khi một hoặc nhiều StyleRules được thay đổi rõ ràng trên kết nối StyleSheet hoặc StyleRule .
Thuộc Tính
Priority
Một số xác định cách các thuộc tính của StyleRule áp dụng so với các thuộc tính tương tự trong StyleRules khác.Giá trị ưu tiên cao hơn có ưu tiên hơn giá trị thấp hơn.Ví dụ, nếu một với ưu tiên có một thuộc tính của , nó sẽ có ưu tiên hơn so với một với các thuộc tính thấp hơn.
Selector
Một chuỗi xác định các ví dụ mà StyleRule nên ảnh hưởng đến.Nó có thể là một hỗn hợp của các lựa chọn và kết hợp để phù hợp với các tính năng như tên lớp, tên của thực thể và các mối quan hệ về cấu trúc.
Ví dụ, ".Container > ImageLabel.BlueOnHover:Hover" hiệu quả có nghĩa là quy tắc phong cách thay thế mọi ImageLabel là con của một thẻ instance được gắn thẻ với Container ( .Container > ImageLabel ) và được gắn thẻ với BlueOnHover ( .BlueOnHover ) và ở trạng thái Enum.GuiState.Hover ( :Hover ).
Lựa chọn
<th>Mô tả</th><th>Ví dụ</th></tr></thead><tbody><tr><td><code>[lớp]</code></td><td>Phù hợp với các ví dụ của một <code>Class.GuiObject</code> hoặc <code>Class.UIComponent</code> lớp.</td><td><code>"Khung"</code><code>"Nút hình ảnh"</code><code>"Góc UIC"</code></td></tr><tr><td><code>.[tag]</code></td><td>Phù hợp với các thẻ <a href="/studio/properties#instance-tags">được gắn thẻ</a> với một thẻ <code>Class.CollectionService</code> .</td><td><code>".Container"</code><code>".BlueOnHover"</code></td></tr><tr><td><code>[tên] #name</code></td><td>Phù hợp với các ví dụ của một lớp cụ thể <code>Class.Instance.Name</code>.</td><td><code>#1 #2</code><code>#3 #4</code></td></tr><tr><td><code>[trạng thái]:</code></td><td>Phù hợp với các ví dụ hiện tại trong một <code>Enum.GuiState</code>.</td><td><code>":Lướt"</code></td></tr></tbody>
Lựa chọn |
---|
Các kết hợp viên
<th>Mô tả</th><th>Ví dụ</th></tr></thead><tbody><tr><td><code>></code></td><td>Phù hợp với các ví dụ mà là <b>trẻ em trực tiếp</b> của các trận lọc trước.</td><td width="40%"><code>“Khung > .Inventory”</code></td></tr><tr><td><code>>></code></td><td>Phù hợp với các ví dụ mà là <b>con cháu</b> của các trận lọc trước.</td><td><code>“Nút hình ảnh >> .BlueOnHover”</code></td></tr><tr><td><code>,</code></td><td>Xác định một danh sách các lựa chọn độc lập nhiều cho quy tắc phong cách.</td><td><code>“Frame.TagA, TextLabel.TagA”</code></td></tr><tr><td><code>::</code></td><td>Tạo một ví dụ lớp <code>Class.UIComponent</code> dưới các bộ lọc trước đó và áp dụng các thuộc tính của quy tắc phong cách cho nó.</td><td><code>“Frame::UICorner”</code></td></tr></tbody>
Kết hợp viên |
---|
Mẫu mã
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")
Phương Pháp
GetProperties
Trả về một từ điển các cặp chìa khóa-giá trị mô tả các thuộc tính của StyleRule, ví dụ:
local ReplicatedStorage = game:GetService("ReplicatedStorage")local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")-- Nhận tham chiếu đến quy tắc phong cáchlocal frameRule = coreSheet.Framelocal props = frameRule:GetProperties()print(props)--[[{["AnchorPoint"] = 0.5, 0,["BackgroundColor3"] = 1, 0, 0.25}]]
Lợi Nhuận
Từ điển các cặp chìa khóa-giá trị mô tả các tính chất của StyleRule .
GetProperty
Trả giá trị của một thuộc tính cụ thể trong StyleRule .
local ReplicatedStorage = game:GetService("ReplicatedStorage")local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")-- Nhận tham chiếu đến quy tắc phong cáchlocal frameRule = coreSheet.Framelocal prop = frameRule:GetProperty("AnchorPoint")print(prop) --> 0.5, 0
Tham Số
Tên chuỗi của thuộc tính, ví dụ "AnchorPoint" hoặc "BackgroundColor3" .
Lợi Nhuận
Giá trị của thuộc tính.
SetProperties
Tương tự như SetProperty() nhưng cho phép bạn tuyên bố và đặt nhiều thuộc tính của StyleRule cùng một lúc. Mỗi nhiệm vụ nên là một thuộc tính hợp lệ của ảnh hưởng GuiObject hoặc UIComponent ( UICorner , UIGradient , v.v.), và mỗi giá trị được gán nên phù hợp với loại giá trị của thuộc tính, ví dụ Vector2 cho AnchorPoint hoặc Color3 cho BackgroundColor3 .
Các nỗ lực để gán tên thuộc tính không hợp lệ như "AnchorPt" hoặc "BkColor" sẽ thất bại lặng lẽ.Loại trùng lặp như CFrame cho AnchorPoint hoặc UDim2 cho BackgroundColor3 sẽ cũng thất bại và một lỗi sẽ xuất hiện trong cửa sổ Thành phẩm.
Để thiết lập/cập nhật chỉ một thuộc tính của một StyleRule , xem SetProperty() .
local ReplicatedStorage = game:GetService("ReplicatedStorage")local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")-- Nhận tham chiếu đến quy tắc phong cáchlocal frameRule = coreSheet.Frame-- Đặt tính chất quy tắcframeRule:SetProperties({["AnchorPoint"] = Vector2.new(0.5, 0),["BackgroundColor3"] = Color3.new(1, 0, 0.25)})
Lưu ý rằng bạn có thể gán token như giá trị thuộc tính thông qua tiền tố $:
local ReplicatedStorage = game:GetService("ReplicatedStorage")local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")local tokensSheet = ReplicatedStorage:FindFirstChild("Tokens")-- Đặt token (thuộc tính) trên trang tokentokensSheet:SetAttribute("TopCenterAnchor", Vector2.new(0.5, 0))tokensSheet:SetAttribute("MainBackgroundColor", Color3.new(0.2, 0.2, 0.3))-- Nhận tham chiếu đến quy tắc phong cáchlocal frameRule = coreSheet.Frame-- Đặt tính chất quy tắcframeRule:SetProperties({["AnchorPoint"] = "$TopCenterAnchor",["BackgroundColor3"] = "$MainBackgroundColor"})
Tham Số
Từ điển các cặp chìa khóa-giá trị định nghĩa các thuộc tính để đặt.
Lợi Nhuận
SetProperty
Đặt một thuộc tính mới (hoặc sửa thuộc tính hiện có) cho StyleRule .Tham số name phải là một thuộc tính hợp lệ của ảnh hưởng GuiObject hoặc UIComponent ( UICorner , UIGradient , v.v.), và giá trị được gán phải phù hợp với loại giá trị của thuộc tính, ví dụ Vector2 đối với AnchorPoint hoặc Color3 đối với BackgroundColor3 .
Các nỗ lực để gán tên thuộc tính không hợp lệ như "AnchorPt" hoặc "BkColor" sẽ thất bại lặng lẽ.Loại trùng lặp như CFrame cho AnchorPoint hoặc UDim2 cho BackgroundColor3 sẽ cũng thất bại và một lỗi sẽ xuất hiện trong cửa sổ Thành phẩm.
Để đặt nhiều thuộc tính cho một StyleRule tại một lúc, xem SetProperties() .
local ReplicatedStorage = game:GetService("ReplicatedStorage")local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")-- Nhận tham chiếu đến quy tắc phong cáchlocal frameRule = coreSheet.Frame-- Đặt tính chất quy tắcframeRule:SetProperty("BackgroundColor3", Color3.new(1, 0, 0.25))
Lưu ý rằng bạn có thể gán token như giá trị thuộc tính thông qua tiền tố $:
local ReplicatedStorage = game:GetService("ReplicatedStorage")local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")local tokensSheet = ReplicatedStorage:FindFirstChild("Tokens")-- Thiết lập token mới (thuộc tính) trên trang tokentokensSheet:SetAttribute("MainBackgroundColor", Color3.new(0.2, 0.2, 0.3))-- Nhận tham chiếu đến quy tắc phong cáchlocal frameRule = coreSheet.Frame-- Đặt tính chất quy tắc để sử dụng token làm giá trị của nóframeRule:SetProperty("BackgroundColor3, "$MainBackgroundColor")
Tham Số
Tên thuộc tính để đặt, ví dụ "BackgroundColor3" .
Giá trị thuộc tính để đặt, ví dụ Color3.new(1, 0, 0.25) .