插件工具列按鈕是由 PluginToolbar:CreateButton() 函數創建的對象。它允許使用者通過 Click 事件在 Roblox Studio 啟動單次、一次性行動。
當按下時,Click發生。按鈕也會保持在按下狀態,可以使用 SetActive 手動設置。在插件啟用時(Plugin:Activate()),所有其他PluginToolbars按鈕將被切換關閉。如果工具欄上的所有按鈕都關閉,工具欄的插件將被禁用(Plugin:Deactivate())。
當遊戲視區不可見時,按鈕會被禁用,如果其 Enabled 屬性為假值。停用按鈕會被淡化,並且不會回應使用者點擊。通過設置 ClickableWhenViewportHidden 為真實值,您可以允許插件按鈕仍然可以點擊(例如在腳本編輯期間)。
概要
屬性
決定按鈕在遊戲視窗隱藏時是否可以點擊,例如在不同的 Studio 標籤中編輯腳本時。
決定按鈕在一般情況下是否可點擊。
決定哪一個圖示應該代表按鈕。
屬性
ClickableWhenViewportHidden
點擊視窗隱藏時 確定是否可以點擊插件工具按鈕,當遊戲視窗隱藏時,例如當 Script 在另一個標籤中編輯時。
通常,此屬性很適合啟用,如果使用插件按鈕的 Click 事件觸發的行動在遊戲世界 (工作區) 中不發生。例如,開啟視窗的按鈕應該擁有此屬性為真,因為即使遊戲視窗不可見,顯示視窗也會對用戶可見。
Enabled
啟用 決定一個按鈕在一般情況下是否可點擊。當此屬性為 false 時,按鈕將被灰塵並且無法點擊,防止用戶發射 Click 事件。按鈕預設啟用。
當重新啟用此屬性時,插件按鈕的狀態將不會從用戶離開按鈕的前一個狀態中記得。相反,它會默認為最後由 SetActive() 設置的狀態或不活躍狀態,如果 SetActive() 從未使用過。
插件應在按鈕操作不相關於當前上下文時停用按鈕。例如,給予選擇的隨機顏色的插件按鈕不應在選擇中沒有零件時啟用。查看代碼樣本以獲得更多資訊。
也見:
- ClickableWhenViewportHidden , 決定按鈕在遊戲視圖隱藏時是否可點擊(而不只是一般)
範例程式碼
This code sample is for a studio Plugin. The plugin creates a PluginToolbarButton which randomizes the BrickColor() of each selected part using BrickColor.random(). Furthermore, the button is only enabled if at least one part is selected. It does this by detecting changes in the Selection using Selection.SelectionChanged.
assert(plugin, "This script must be run as a plugin")
local Selection = game:GetService("Selection")
local toolbar = plugin:CreateToolbar("Parts")
local pluginToolbarButton = toolbar:CreateButton(
"Randomize Colors",
"Click this button to assign random colors to selected parts",
"rbxassetid://5325741572" -- A rainbow
)
local function onClick()
local selection = Selection:Get()
for _, object in pairs(selection) do
if object:IsA("BasePart") then
object.BrickColor = BrickColor.random()
end
end
end
pluginToolbarButton.Click:Connect(onClick)
local function doesSelectionContainAPart()
local selection = Selection:Get()
for _, object in pairs(selection) do
if object:IsA("BasePart") then
return true
end
end
return false
end
local function onSelectionChanged()
pluginToolbarButton.Enabled = doesSelectionContainAPart()
end
Selection.SelectionChanged:Connect(onSelectionChanged)
onSelectionChanged()
方法
活動
Click
單擊 發生當使用者按下並釋放插件工具按鈕時。
單擊插件工具按鈕會導致按鈕狀態切換。呼叫 SetActive 手動設置按鈕狀態。
範例程式碼
This code sample demonstrates creating a PluginToolbar and a PluginToolbarButton on it, then connecting a function onClick to the PluginToolbarButton.Click event. When pressed, the button will print "Hello, world" to the output.
assert(plugin, "This script must be run as a plugin")
local toolbar = plugin:CreateToolbar("Hello World Plugin Toolbar")
local pluginToolbarButton =
toolbar:CreateButton("Print Hello World", "Click this button to print Hello World!", "rbxassetid://133293265")
local function onClick()
print("Hello, world")
end
pluginToolbarButton.Click:Connect(onClick)