StyleRule

Tampilkan yang Tidak Digunakan Lagi

*Konten ini diterjemahkan menggunakan AI (Beta) dan mungkin mengandung kesalahan. Untuk melihat halaman ini dalam bahasa Inggris, klik di sini.

Mendefinisikan properti gaya yang menggantikan properti pada instans yang dipengaruhi oleh properti Selector .

Rangkuman

Properti

  • Baca Paralel

    Angka yang menentukan bagaimana properti dari StyleRule berlaku relatif terhadap properti yang sama di StyleRules lainnya.Nilai prioritas yang lebih tinggi memiliki prioritas lebih tinggi daripada yang lebih rendah.

  • Baca Paralel

    Sebuah string yang menentukan instansi mana StyleRule harus mempengaruhi.

  • Hanya Baca
    Tidak Direplikasi
    Baca Paralel

    Sebuah string hanya baca yang menampilkan kesalahan dari properti Selector.

Metode

Metode diwarisi dari StyleBase

Acara

Acara diwarisi dari StyleBase

Properti

Priority

Baca Paralel

Angka yang menentukan bagaimana properti dari StyleRule berlaku relatif terhadap properti yang sama di StyleRules lainnya.Nilai prioritas yang lebih tinggi memiliki prioritas lebih tinggi daripada yang lebih rendah.Sebagai contoh, jika StyleRule dengan prioritas 10 memiliki properti AnchorPoint dari 1, 0 , itu akan mengambil prioritas lebih tinggi daripada prioritas lebih rendah StyleRules dengan properti AnchorPoint .

Selector

Baca Paralel

Sebuah string yang menentukan instansi mana StyleRule harus mempengaruhi.Ini bisa menjadi campuran selektor dan kombinator untuk mencocokkan karakteristik seperti nama kelas, nama instans, dan hubungan hierarki.

Sebagai contoh, ".Container > ImageLabel.BlueOnHover:Hover" secara efektif berarti bahwa aturan gaya menggantikan setiap ImageLabel yang merupakan anak dari tag instansi yang ditagih dengan Container ( .Container > ImageLabel ) dan ditagih dengan BlueOnHover ( .BlueOnHover ) dan berada di negara Enum.GuiState.Hover ( :Hover ).

Pemilih

<th>Deskripsi</th>
<th>Contoh</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>[kelas]</code></td>
<td>Pertandingan instansi dari <code>Class.GuiObject</code> atau <code>Class.UIComponent</code> kelas.</td>
<td><code>"Frame"</code><code>"ImageButton"</code><code>"UICorner"</code></td>
</tr>
<tr>
<td><code>.[tag]</code></td>
<td>Mencocokkan instansi <a href="/studio/properties#instance-tags">ditagkan</a> dengan tag <code>Class.CollectionService</code>.</td>
<td><code>".Container"</code><code>".BlueOnHover"</code></td>
</tr>
<tr>
<td><code>#[name]</code></td>
<td>Pertandingan instansi dari kelas spesifik <code>Class.Instance.Name</code>.</td>
<td><code>"#ModalFrame"</code><code>="#Tutup Tombol"</code></td>
</tr>
<tr>
<td><code>[state]:</code></td>
<td>Pertandingan instans saat ini di <code>Enum.GuiState</code> .</td>
<td><code>":Menggantung"</code></td>
</tr>
</tbody>
Seletor
Kombinasi

<th>Deskripsi</th>
<th>Contoh</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>></code></td>
<td>Pertandingan instansi yang adalah <b>anak langsung</b> dari pertandingan filter sebelumnya.</td>
<td width="40%"><code>“Frame > .Inventory”</code></td>
</tr>
<tr>
<td><code>>></code></td>
<td>Cocokkan instansi yang <b>turunan</b> dari filter sebelumnya.</td>
<td><code>“Tombol Gambar >> .BlueOnHover”</code></td>
</tr>
<tr>
<td><code>,</code></td>
<td>Mengidentifikasi daftar selektor independen banyak untuk aturan gaya.</td>
<td><code>“Frame.TagA, TextLabel.TagA”</code></td>
</tr>
<tr>
<td><code>::</code></td>
<td>Membuat instansi kelas phantom <code>Class.UIComponent</code> di bawah filter sebelumnya dan menerapkan properti aturan gaya kepadanya.</td>
<td><code>“Frame::UICorner”</code></td>
</tr>
</tbody>
Penggabung

Contoh Kode

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

Hanya Baca
Tidak Direplikasi
Baca Paralel

Sebuah string hanya baca yang menampilkan kesalahan dari properti Selector seperti kesalahan sintaks, kelas yang tidak didukung, dll.

Metode

GetProperties

Kembalikan kamus pasangan kunci-nilai yang menggambarkan properti dari StyleRule, misalnya:


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
-- Dapatkan referensi untuk aturan gaya
local frameRule = coreSheet.Frame
local props = frameRule:GetProperties()
print(props)
--[[
{
["AnchorPoint"] = 0.5, 0,
["BackgroundColor3"] = 1, 0, 0.25
}
]]

Memberikan nilai

Kamus pasangan kunci-nilai yang menggambarkan properti dari StyleRule .

GetProperty

Variant

Kembalikan nilai properti tertentu di StyleRule .


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
-- Dapatkan referensi untuk aturan gaya
local frameRule = coreSheet.Frame
local prop = frameRule:GetProperty("AnchorPoint")
print(prop) --> 0.5, 0

Parameter

name: string

Nama string properti, misalnya "AnchorPoint" atau "BackgroundColor3".

Nilai Default: ""

Memberikan nilai

Variant

Nilai dari property.

SetProperties

()

Mirip dengan SetProperty() tetapi memungkinkan Anda menyatakan dan mengatur banyak properti dari StyleRule sekaligus.Setiap tugas harus menjadi properti yang valid dari pengguna yang terpengaruh GuiObject atau UIComponent ( UICorner , UIGradient , dll.), dan setiap nilai yang ditugaskan harus sesuai dengan jenis nilai propertinya, misalnya Vector2 untuk AnchorPoint atau Color3 untuk BackgroundColor3 .

Upaya untuk menetapkan nama properti yang tidak valid seperti "AnchorPt" atau "BkColor" akan gagal diam-diam.Jenis ketidakcocokan seperti CFrame untuk AnchorPoint atau UDim2 untuk BackgroundColor3 juga akan gagal dan kesalahan akan muncul di jendela Output.

Untuk mengatur/memperbarui hanya satu properti dari StyleRule , lihat SetProperty() .


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
-- Dapatkan referensi untuk aturan gaya
local frameRule = coreSheet.Frame
-- Tetapkan properti aturan
frameRule:SetProperties({
["AnchorPoint"] = Vector2.new(0.5, 0),
["BackgroundColor3"] = Color3.new(1, 0, 0.25)
})

Perhatikan bahwa Anda dapat menetapkan token sebagai nilai properti melalui prefiks $:


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
local tokensSheet = ReplicatedStorage:FindFirstChild("Tokens")
-- Tetapkan token (属性) di lembar token
tokensSheet:SetAttribute("TopCenterAnchor", Vector2.new(0.5, 0))
tokensSheet:SetAttribute("MainBackgroundColor", Color3.new(0.2, 0.2, 0.3))
-- Dapatkan referensi untuk aturan gaya
local frameRule = coreSheet.Frame
-- Tetapkan properti aturan
frameRule:SetProperties({
["AnchorPoint"] = "$TopCenterAnchor",
["BackgroundColor3"] = "$MainBackgroundColor"
})

Parameter

styleProperties: Dictionary

Kamus pasangan kunci-nilai yang mendefinisikan properti untuk disetel.

Nilai Default: ""

Memberikan nilai

()

SetProperty

()

Mengatur properti baru (atau memodifikasi properti yang ada) untuk StyleRule .Parameter name harus menjadi properti yang valid dari terpengaruh GuiObject atau UIComponent ( UICorner , UIGradient , dll.), dan nilai yang ditugaskan harus sesuai dengan jenis nilai properti, misalnya Vector2 untuk AnchorPoint atau Color3 untuk BackgroundColor3.

Upaya untuk menetapkan nama properti yang tidak valid seperti "AnchorPt" atau "BkColor" akan gagal diam-diam.Jenis ketidakcocokan seperti CFrame untuk AnchorPoint atau UDim2 untuk BackgroundColor3 juga akan gagal dan kesalahan akan muncul di jendela Output.

Untuk mengatur beberapa properti untuk StyleRule sekaligus, lihat SetProperties() .


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
-- Dapatkan referensi untuk aturan gaya
local frameRule = coreSheet.Frame
-- Tetapkan properti aturan
frameRule:SetProperty("BackgroundColor3", Color3.new(1, 0, 0.25))

Perhatikan bahwa Anda dapat menetapkan token sebagai nilai properti melalui prefiks $:


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
local tokensSheet = ReplicatedStorage:FindFirstChild("Tokens")
-- Tetapkan token baru (attribut) di lembar token
tokensSheet:SetAttribute("MainBackgroundColor", Color3.new(0.2, 0.2, 0.3))
-- Dapatkan referensi untuk aturan gaya
local frameRule = coreSheet.Frame
-- Atur properti aturan untuk menggunakan token sebagai nilainya
frameRule:SetProperty("BackgroundColor3, "$MainBackgroundColor")

Parameter

name: string

Nama properti untuk disetel, misalnya "BackgroundColor3" .

Nilai Default: ""
value: Variant

Nilai properti untuk disetel, misalnya Color3.new(1, 0, 0.25) .

Nilai Default: ""

Memberikan nilai

()

Acara