選擇服務控制在 Roblox Studio 中選擇的 Instances 。
此服務在開發 Plugins 時特別有用,因為它讓開發人員可以存取和操作當前選擇。
目前選擇的 Instances 可以使用 Selection:Get() 和 Selection:Set() 函數獲得和設置。Selection.SelectionChanged會在當前選擇變更時發生。
有關使用 Selection 和 Plugins 的更多信息,請參閱 Plugin 。
選擇也常在指令欄中使用,以設置隱藏屬性或執行選擇的 Instances 功能。
請注意,此類別只適用於 Roblox Studio,並不適用於遊戲。
範例程式碼
The following code sample, when used in a plugin or the command bar, will rotate currently selected BaseParts.
local Selection = game:GetService("Selection")
for _, object in pairs(Selection:Get()) do
if object:IsA("BasePart") then
object.CFrame = object.CFrame * CFrame.Angles(0, math.pi / 2, 0)
end
end
屬性
SelectionThickness
方法
Add
參數
返回
Get
在 Roblox Studio 中返回目前選擇的 Instances 數組。
如果未選擇 Instances,返回的陣列將為空。此功能可與 Selection.SelectionChanged 事件一起使用,以在其變更時獲得選擇。
注意,此功能只能在 Plugins 或命令行中使用。
若要變更目前的選擇,請參見 Selection:Set() 。
返回
一個包含目前選擇的 Instances 的陣列。
範例程式碼
The following code sample, when used in a plugin or the command bar, will rotate currently selected BaseParts.
local Selection = game:GetService("Selection")
for _, object in pairs(Selection:Get()) do
if object:IsA("BasePart") then
object.CFrame = object.CFrame * CFrame.Angles(0, math.pi / 2, 0)
end
end
This example prints the number of selected items whenever SelectionChanged is fired:
local selection = game:GetService("Selection")
selection.SelectionChanged:Connect(function()
print("Selection contains " .. #selection:Get() .. " items.")
end)
Remove
參數
返回
Set
將 Roblox Studio 中目前選擇的對象設為 Instances 在指定的陣列表中。
觸發叫此函數會導致 Selection.SelectionChanged 事件發生,除非新選擇集與以前的選擇相同。
請注意,此功能會覆蓋現有的選擇。然而,使用 Selection:Get() 一個 Instance 可以添加到現有選擇中,如下所示:
local selected = Selection:Get()table.insert(selected, object)Selection:Set(selected)
參數
一個 Instances 陣列來設置當前選擇。
返回
範例程式碼
This code sample will select every BasePart in the workspace that is Bright red.
local Selection = game:GetService("Selection")
local newSelection = {}
for _, object in pairs(workspace:GetDescendants()) do
if object:IsA("BasePart") and object.BrickColor == BrickColor.new("Bright red") then
table.insert(newSelection, object)
end
end
Selection:Set(newSelection)
活動
SelectionChanged
在 Roblox Studio 中選擇 Instances 時發生火災。
請注意,此事件不會提供新的選擇。開發人員需要使用 Selection:Get() 函數來獲得當前選擇。
雖然此事件可以在插件和指令欄之外使用,但只適用於 Roblox Studio 的選擇,因此無法在 Studio 之外使用功能。
要變更選擇,請使用 Selection:Set() 功能。
範例程式碼
This example prints the number of selected items whenever SelectionChanged is fired:
local selection = game:GetService("Selection")
selection.SelectionChanged:Connect(function()
print("Selection contains " .. #selection:Get() .. " items.")
end)