StyleRule

Visualizza obsoleti

*Questo contenuto è tradotto usando AI (Beta) e potrebbe contenere errori. Per visualizzare questa pagina in inglese, clicca qui.

Definisce le proprietà di stile che sostituiscono le proprietà sulle istanze interessate dalla proprietà Selector .

Sommario

Proprietà

  • Lettura Parallela

    Un numero che determina come le proprietà del StyleRule si applicano rispetto alle stesse proprietà in altre StyleRules .I valori di priorità più alti hanno la precedenza su quelli più bassi.

  • Lettura Parallela

    Una stringa che specifica quali istanze il StyleRule dovrebbe influenzare.

  • Sola Lettura
    Non Replicato
    Lettura Parallela

    Una stringa read-only che mostra gli errori dalla proprietà Selector.

Metodi

Metodi provenienti da StyleBase

Proprietà

Priority

Lettura Parallela

Un numero che determina come le proprietà del StyleRule si applicano rispetto alle stesse proprietà in altre StyleRules .I valori di priorità più alti hanno la precedenza su quelli più bassi.Ad esempio, se un con una priorità di ha una proprietà di , prenderà precedenza su una priorità più bassa con proprietà.

Selector

Lettura Parallela

Una stringa che specifica quali istanze il StyleRule dovrebbe influenzare.Questo può essere un mix di selezionatori e combinatori per abbinare caratteristiche come il nome della classe, il nome dell'istanza e le relazioni gerarchiche.

Ad esempio, ".Container > ImageLabel.BlueOnHover:Hover" effettivamente significa che la regola di stile sostituisce ogni ImageLabel che è un figlio di un tag dell'istanza contrassegnato con Container ( .Container > ImageLabel ) e è contrassegnato con BlueOnHover ( .BlueOnHover ) e è in stato Enum.GuiState.Hover ( :Hover ).

Selettori

<th>Descrizione</th>
<th>Esempi</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>[ classe ]</code></td>
<td>Corrisponde alle istanze di una classe <code>Class.GuiObject</code> o <code>Class.UIComponent</code> classe.</td>
<td><code>"Frame"</code><code>"ImageButton"</code><code>"UICorner"</code></td>
</tr>
<tr>
<td><code>.[tag]</code></td>
<td>Corrisponde alle istanze <a href="/studio/properties#instance-tags">contrassegnate</a> con un tag <code>Class.CollectionService</code>.</td>
<td><code>".Container"</code><code>".BlueOnHover"</code></td>
</tr>
<tr>
<td><code>#name</code></td>
<td>Corrisponde alle istanze di una classe specifica <code>Class.Instance.Name</code>.</td>
<td><code>"#ModalFrame"</code><code>"#CloseButton"</code></td>
</tr>
<tr>
<td><code>[stato]:</code></td>
<td>Corrisponde alle istanze attualmente in un <code>Enum.GuiState</code> .</td>
<td><code>":Hover"</code></td>
</tr>
</tbody>
Selettore
Combinatori

<th>Descrizione</th>
<th>Esempi</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>></code></td>
<td>Corrisponde alle istanze che sono <b>figli diretti</b> della precedente match del filtro.</td>
<td width="40%"><code>""Frame > .Inventory"</code></td>
</tr>
<tr>
<td><code>>></code></td>
<td>Corrisponde alle istanze che sono <b>discendenti</b> delle precedenti corrispondenze del filtro.</td>
<td><code>""Button immagine >> .BlueOnHover"</code></td>
</tr>
<tr>
<td><code>,</code></td>
<td>Specifica una lista di più selezionatori indipendenti per la regola di stile.</td>
<td><code>""Frame.TagA, TextLabel.TagA"</code></td>
</tr>
<tr>
<td><code>::</code></td>
<td>Crea un'istanza di phantom <code>Class.UIComponent</code> sotto le corrispondenze del filtro precedente e applica le proprietà della regola di stile a essa.</td>
<td><code>""Frame::UICorner"</code></td>
</tr>
</tbody>
Combinatore

Campioni di codice

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

Sola Lettura
Non Replicato
Lettura Parallela

Una stringa read-only che visualizza gli errori dalla proprietà Selector come errori di sintassi, tipi di classe non supportati, ecc.

Metodi

GetProperties

Restituisce un dizionario di coppie chiave-valore che descrivono le proprietà del StyleRule, ad esempio:


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
-- Ottieni il riferimento alla regola di stile
local frameRule = coreSheet.Frame
local props = frameRule:GetProperties()
print(props)
--[[
{
["AnchorPoint"] = 0.5, 0,
["BackgroundColor3"] = 1, 0, 0.25
}
]]

Restituzioni

Dizionario di coppie chiave-valore che descrivono le proprietà del StyleRule .

GetProperty

Variant

Restituisce il valore di una proprietà specifica nel StyleRule .


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
-- Ottieni il riferimento alla regola di stile
local frameRule = coreSheet.Frame
local prop = frameRule:GetProperty("AnchorPoint")
print(prop) --> 0.5, 0

Parametri

name: string

Nome della stringa della proprietà, ad esempio "AnchorPoint" o "BackgroundColor3" .

Valore predefinito: ""

Restituzioni

Variant

Valore della proprietà.

SetProperties

()

Simile a SetProperty() ma ti consente di dichiarare e impostare più proprietà del StyleRule contemporaneamente.Ogni assegnazione dovrebbe essere una proprietà valida dell'interessato GuiObject o UIComponent ( UICorner , UIGradient , ecc.), e ogni valore assegnato dovrebbe corrispondere al tipo di valore della proprietà, ad esempio Vector2 per AnchorPoint o Color3 per BackgroundColor3.

Gli tentativi di assegnare nomi di proprietà non validi come "AnchorPt" o "BkColor" falliranno silenziosamente.Tipo di incoerenze come CFrame per AnchorPoint o UDim2 per BackgroundColor3 fallirà anche e apparirà un errore nella finestra Output.

Per impostare/aggiornare solo una proprietà di un StyleRule , vedi SetProperty() .


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
-- Ottieni il riferimento alla regola di stile
local frameRule = coreSheet.Frame
-- Imposta le proprietà delle regole
frameRule:SetProperties({
["AnchorPoint"] = Vector2.new(0.5, 0),
["BackgroundColor3"] = Color3.new(1, 0, 0.25)
})

Nota che puoi assegnare token come valori di proprietà attraverso il prefisso $:


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
local tokensSheet = ReplicatedStorage:FindFirstChild("Tokens")
-- Imposta i gettoni (attributi) sulla scheda dei gettoni
tokensSheet:SetAttribute("TopCenterAnchor", Vector2.new(0.5, 0))
tokensSheet:SetAttribute("MainBackgroundColor", Color3.new(0.2, 0.2, 0.3))
-- Ottieni il riferimento alla regola di stile
local frameRule = coreSheet.Frame
-- Imposta le proprietà delle regole
frameRule:SetProperties({
["AnchorPoint"] = "$TopCenterAnchor",
["BackgroundColor3"] = "$MainBackgroundColor"
})

Parametri

styleProperties: Dictionary

Dizionario di coppie chiave-valore che definiscono le proprietà da impostare.

Valore predefinito: ""

Restituzioni

()

SetProperty

()

Imposta una nuova proprietà (o modifica una proprietà esistente) per il StyleRule .Il parametro name dovrebbe essere una proprietà valida dell'interessato GuiObject o UIComponent ( UICorner , UIGradient , ecc.), e il valore assegnato dovrebbe corrispondere al tipo di valore della proprietà, ad esempio Vector2 per AnchorPoint o Color3 per BackgroundColor3.

Gli tentativi di assegnare nomi di proprietà non validi come "AnchorPt" o "BkColor" falliranno silenziosamente.Tipo di incoerenze come CFrame per AnchorPoint o UDim2 per BackgroundColor3 fallirà anche e apparirà un errore nella finestra Output.

Per impostare più proprietà per un StyleRule alla volta, vedi SetProperties() .


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
-- Ottieni il riferimento alla regola di stile
local frameRule = coreSheet.Frame
-- Imposta la proprietà delle regole
frameRule:SetProperty("BackgroundColor3", Color3.new(1, 0, 0.25))

Nota che puoi assegnare token come valori di proprietà attraverso il prefisso $:


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
local tokensSheet = ReplicatedStorage:FindFirstChild("Tokens")
-- Imposta un nuovo token (attributo) sulla scheda dei token
tokensSheet:SetAttribute("MainBackgroundColor", Color3.new(0.2, 0.2, 0.3))
-- Ottieni il riferimento alla regola di stile
local frameRule = coreSheet.Frame
-- Imposta la proprietà regola per utilizzare il token come suo valore
frameRule:SetProperty("BackgroundColor3, "$MainBackgroundColor")

Parametri

name: string

Nome della proprietà da impostare, ad esempio "BackgroundColor3" .

Valore predefinito: ""
value: Variant

Valore di proprietà da impostare, ad esempio Color3.new(1, 0, 0.25) .

Valore predefinito: ""

Restituzioni

()

Eventi