Selection

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

无法创建
服务

选择服务控制在 Roblox Studio 中选择的 Instances

此服务在开发 Plugins 特别有用,因为它允许开发者访问和操作当前选择。

目前选择的 Instances 可以使用 Selection:Get()Selection:Set() 函数获得并设置。Selection.SelectionChanged会在当前选择更改时触发。

了解有关使用 SelectionPlugins 的更多信息,请参阅 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

概要

方法

  • Add(instancesToAdd : Instances):()
    插件安全性
  • Get():Instances
    插件安全性

    在 Roblox Studio 中返回一个阵列当前选择的 Instances 在 Roblox Studio 中。

  • Remove(instancesToRemove : Instances):()
    插件安全性
  • Set(selection : Instances):()
    插件安全性

    将 Roblox Studio 中现有选定的对象设置为 Instances 在给定的数组列中。

属性

SelectionThickness

只读
未复制
读取并联

方法

Add

()
插件安全性

参数

instancesToAdd: Instances
默认值:""

返回

()

Get

Instances
插件安全性

在 Roblox Studio 中返回一个阵列当前选择的 Instances 在 Roblox Studio 中。

如果未选择Instances,返回的阵列将为空。该函数可以与 Selection.SelectionChanged 事件结合使用,以便随时获取选择。

注意,此函数只能在 Plugins 或命令行中使用。

要更改当前选择,请参阅Selection:Set()


返回

Instances

一个阵列当前选择的 Instances

代码示例

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

This example prints the number of selected items whenever SelectionChanged is fired:

Selection.SelectionChanged

local selection = game:GetService("Selection")
selection.SelectionChanged:Connect(function()
print("Selection contains " .. #selection:Get() .. " items.")
end)

Remove

()
插件安全性

参数

instancesToRemove: Instances
默认值:""

返回

()

Set

()
插件安全性

将 Roblox Studio 中现有选定的对象设置为 Instances 在给定的数组列中。

调用此函数会导致 Selection.SelectionChanged 事件发触发,除非新选择集与以前的选择集相同。

注意,这个函数会覆盖现有的选择。然而,使用 Selection:Get() 一个 Instance 可以添加到现有选择中,如下所示:


local selected = Selection:Get()
table.insert(selected, object)
Selection:Set(selected)

参数

selection: Instances

一个 Instances 阵列用于将当前选择设置为。

默认值:""

返回

()

代码示例

This code sample will select every BasePart in the workspace that is Bright red.

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

在 Roblox Studio 中选择 Instances 时发生火灾。

请注意,此事件不会提供新选择。开发人员需要使用 Selection:Get() 函数来获得当前选择。

虽然这个事件可以在插件和命令栏之外使用,但仅适用于 Roblox Studio 的选择,因此没有 Studio 之外的功能。

要更改选择,请使用 Selection:Set() 函数。


代码示例

This example prints the number of selected items whenever SelectionChanged is fired:

Selection.SelectionChanged

local selection = game:GetService("Selection")
selection.SelectionChanged:Connect(function()
print("Selection contains " .. #selection:Get() .. " items.")
end)