選擇服務控制在 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.
Selection
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
()
參數
instancesToAdd: Instances
預設值:""
返回
()
Get
Instances
返回
Instances
範例程式碼
Selection
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
Selection.SelectionChanged
local selection = game:GetService("Selection")
selection.SelectionChanged:Connect(function()
print("Selection contains " .. #selection:Get() .. " items.")
end)
Remove
()
參數
instancesToRemove: Instances
預設值:""
返回
()
Set
()
參數
selection: Instances
預設值:""
返回
()
範例程式碼
Selection Set
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
範例程式碼
Selection.SelectionChanged
local selection = game:GetService("Selection")
selection.SelectionChanged:Connect(function()
print("Selection contains " .. #selection:Get() .. " items.")
end)