Selection

顯示已棄用項目

*此內容是使用 AI(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 數組。

  • Remove(instancesToRemove : Instances):()
    外掛程式安全性
  • Set(selection : Instances):()
    外掛程式安全性

    將 Roblox Studio 中目前選擇的對象設為 Instances 在指定的陣列表中。

屬性

SelectionThickness

唯讀
未複製
平行讀取

方法

Add

()
外掛程式安全性

參數

instancesToAdd: Instances
預設值:""

返回

()

Get

Instances
外掛程式安全性

在 Roblox Studio 中返回目前選擇的 Instances 數組。

如果未選擇 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)