选择服务控制在 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 在 Roblox Studio 中。
如果未选择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)