Selection
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
บริการเลือกควบคุม Instances ที่เลือกใน Roblox Studio
บริการนี้มีประโยชน์เป็นพิเศษเมื่อพัฒนา 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
ส่งคืนคอลเลกชันของตัวเลือกที่เลือกในปัจจุบัน 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
เกิดไฟไหม้เมื่อ Instances ถูกเลือกใน Roblox Studio เปลี่ยนแปลง
โปรดทราบว่าอีเวนต์นี้ไม่ให้ตัวเลือกใหม่ ผู้พัฒนาจะต้องใช้ฟังก์ชัน Selection:Get() เพื่อรับตัวเลือกปัจจุบัน
แม้ว่าอีเวนต์นี้จะสามารถใช้ภายนอกปลั๊กอินและแถบคําสั่งได้ แต่มันใช้ได้เฉพาะการเลือกใน Roblox 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)