ClickDetector
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
ตัวตรวจจับคลิก ช่วยให้ Scripts และ LocalScripts ได้รับการรับประกาศจุดบนวัตถุ 3D ผ่านอีเวนต์ MouseClick ของพวกเขาพวกเขาทำงานเมื่อถูกผูกกับ BasePart , Model หรือ Folder วัตถุพวกเขาตรวจจับเหตุการณ์เมาส์พื้นฐาน: ใส่, กรอก, คลิกซ้าย, และคลิกขวาการแตะบนอุปกรณ์ TouchEnabled ยังส่งเหตุการณ์คลิก
สคริปต์ควบคุมเริ่มต้นผูก ButtonR2 กับการโต้ตอบกับ ClickDetectors โดยใช้ ContextActionService:BindActivate() ซึ่งยังสามารถใช้เพื่อยกเลิกได้เมื่อใช้เกมแพด จุดกลางจะเรียกการดำเนินการ MouseHoverEnter และ MouseHoverLeaveปุ่มเปิดใช้งานที่ผูกไว้ยิง MouseClick
MaxActivationDistance สามารถใช้เพื่อจำกัดระยะทางที่ผู้เล่นอาจอยู่จากตัวระบุคลิกก่อนที่จะไม่สามารถคลิกได้อีก
ClickDetector เหตุการณ์ไฟไหม้บนทั้งไคลเอนต์และเซิร์ฟเวอร์เนื่องจาก LocalScript จะทำงานเฉพาะในกรณีที่ลงจาก Player หรือผู้เล่น Character โดยปกติจะไม่มีประโยชน์ที่จะใส่ LocalScript ภายใน ClickDetector เนื่องจากสคริปต์จะไม่ทำงานหรือวัตถุจะไม่สามารถคลิกได้หากคุณต้องการ ในการตรวจจับเหตุการณ์ คุณอาจเลือกสถานที่ที่ดีกว่าแทน
ลําดับความสําคัญของการใส่
หากหลาย ClickDetectors อาจตรวจพบการป้อนข้อมูลของผู้ใช้ เฉพาะส่วนลึกสุดจะเปิดเหตุการณ์ หากสอง ClickDetectors เป็นพี่น้องกัน คนแรกจะได้รับความสำคัญ
หากการดำเนินการที่ผูกด้วย ContextActionService ใช้อินพุตเดียวกับ ClickDetector การดำเนินการที่ผูกด้วย ContextActionService จะได้รับความสำคัญมากกว่ากิจกรรมของตัวตรวจจับคลิก
UserInputService.InputBegan จะยิงก่อนเหตุการณ์ ClickDetector
ตัวอย่างโค้ด
วางโค้ดนี้ไว้ภายใน Script ภายใน ClickDetectorตัวอย่างโค้ดสร้างการอ้างอิงถึงพ่อและกำหนดฟังก์ชันเพื่อแสดงข้อความที่ทักทายผู้เล่นสุดท้าย มันเชื่อมโยงเหตุการณ์ MouseClick กับฟังก์ชันที่กำหนดไว้
local clickDetector = script.Parent
local function onClicked(player)
-- แสดงข้อความให้ผู้เล่น
local msg = Instance.new("Message")
msg.Parent = player:FindFirstChild("PlayerGui")
msg.Text = "Hello, " .. player.Name
wait(2.5)
msg:Destroy()
end
-- เชื่อมฟังก์ชันกับอีเวนต์ MouseClick
clickDetector.MouseClick:Connect(onClicked)
This code sample will allow a part to be clicked to toggle its anchored property. When toggled, the visual appearance of the part is updated (red means anchored, yellow means free).
local part = script.Parent
-- Create a ClickDetector so we can tell when the part is clicked
local cd = Instance.new("ClickDetector", part)
-- This function updates how the part looks based on its Anchored state
local function updateVisuals()
if part.Anchored then
-- When the part is anchored...
part.BrickColor = BrickColor.new("Bright red")
part.Material = Enum.Material.DiamondPlate
else
-- When the part is unanchored...
part.BrickColor = BrickColor.new("Bright yellow")
part.Material = Enum.Material.Wood
end
end
local function onToggle()
-- Toggle the anchored property
part.Anchored = not part.Anchored
-- Update visual state of the brick
updateVisuals()
end
-- Update, then start listening for clicks
updateVisuals()
cd.MouseClick:Connect(onToggle)
สรุป
คุณสมบัติ
ตั้งไอคอนเคอร์เซอร์ให้แสดงเมื่อเมาส์เลื่อนเหนือพ่อของนี้ ClickDetector หรือ DragDetector
ระยะสูงสุดระหว่างตัวละครและ ClickDetector หรือ DragDetector สำหรับผู้เล่นที่จะสามารถโต้ตอบกับมันได้
อีเวนต์
จะเกิดไฟไหม้เมื่อผู้เล่นโต้ตอบกับผู้ปกครองของ ClickDetector หรือ DragDetector
จะเกิดไฟไหม้เมื่อผู้ปกครองของ ClickDetector หรือ DragDetector ถูกวางบนโดยผู้เล่น
เกิดไฟไหม้เมื่อเคอร์เซอร์ของผู้เล่นลอยออกจากพ่อของ ClickDetector หรือ DragDetector
จะเกิดไฟไหม้เมื่อผู้เล่นคลิกขวาเมาส์ของพวกเขาบน ClickDetector หรือ DragDetector
คุณสมบัติ
CursorIcon
ตั้งไอคอนเคอร์เซอร์ให้แสดงเมื่อเมาส์เลื่อนเหนือพ่อของนี้ ClickDetector หรือ DragDetectorหากคุณสมบัตินี้ว่างเปล่า เครื่องตรวจจะใช้ไอคอนเริ่มต้น
เพื่อเปลี่ยนไอคอนเคอร์เซอร์ ตั้งค่าคุณสมบัตินี้เป็นรหัสทรัพย์สินของภาพที่คุณต้องการใช้
MaxActivationDistance
คุณสมบัตินี้ควบคุมระยะสูงสุดในสตัดระหว่าง Character และ ClickDetector หรือ DragDetector อินสแตนซ์ตัวละครภายใน 10 สตัดของ ClickDetector หรือ DragDetector ที่มีระยะการเปิดใช้งานสูงสุด 5 จะไม่สามารถใช้เครื่องตรวจจับได้เพราะพวกเขาอยู่นอกเหนือขอบเขต
วิธีการ
อีเวนต์
MouseClick
อีเวนต์นี้จะยิงจาก Script หรือ LocalScript เมื่อผู้เล่นโต้ตอบกับ ClickDetector หรือ DragDetector ผ่านอินพุตต่อไปนี้:
- บนแพลตฟอร์มที่มีเมาส์เมื่อผู้เล่นออกจากการคลิกเมาส์
- บนแพลตฟอร์ม TouchEnabled เมื่อผู้เล่นแตะ
- บนแพลตฟอร์ม GamepadEnabled เมื่อจุดกลางอยู่เหนือรูปแบบเดียวกันและปุ่ม A ถูกกดและปล่อย
โปรดทราบว่า Character ของผู้เล่นจะต้องอยู่ภายใน MaxActivationDistance ของเครื่องตรวจจับ
พารามิเตอร์
ผู้ที่คลิกที่บรรพบุรุษของ Player หรือ ClickDetector หรือ DragDetector
MouseHoverEnter
อีเวนต์นี้จะยิงจาก Script หรือ LocalScript เมื่อผู้ปกครองของ ClickDetector หรือ DragDetector ถูกวางบนโดยผู้เล่นสิ่งนี้ไม่รวมถึงการโต้ตอบโดยเฉพาะกับเครื่องตรวจจับซึ่งคุณสามารถฟังเหตุการณ์ MouseClick และ RightMouseClick ได้
เนื่องจากลักษณะของการใส่ข้อมูลของผู้ใช้คุณไม่ควรพึ่งพาเหตุการณ์ทั้งหมด MouseHoverEnter ที่ยิงเหตุการณ์ MouseHoverLeave ที่เกี่ยวข้อง
พารามิเตอร์
ผู้ที่เริ่มเลื่อนเหนือพ่อของ Player หรือ ClickDetector หรือ DragDetector ที่เป็นพ่อ
ตัวอย่างโค้ด
The following code will print "[PlayerName] hovered over my parent!" when a player's cursor hovers over the ClickDetector parent. It will also print "[PlayerName] hovered off my parent!" when the player's cursor moves off the ClickDetector parent.
In order for this example to work as expected, it must be placed in a Script or LocalScript whose parent is a ClickDetector.
local clickDetector = script.Parent:FindFirstChildOfClass("ClickDetector")
clickDetector.MouseHoverEnter:Connect(function(player)
print(player.Name .. " hovered over my parent!")
end)
clickDetector.MouseHoverLeave:Connect(function(player)
print(player.Name .. " hovered off my parent!")
end)
MouseHoverLeave
อีเวนต์นี้จะยิงจาก Script หรือ LocalScript เมื่อเคอร์เซอร์ของผู้เล่นลอยออกจากพ่อของ ClickDetector หรือ DragDetectorสิ่งนี้ไม่รวมถึงการโต้ตอบโดยเฉพาะกับเครื่องตรวจจับซึ่งคุณสามารถฟังเหตุการณ์ MouseClick และ RightMouseClick ได้
เนื่องจากลักษณะของการใส่ข้อมูลของผู้ใช้คุณไม่ควรพึ่งพาการยิงเหตุการณ์ทั้งหมด MouseHoverLeave หลังจากเหตุการณ์ที่เกี่ยวข้อง MouseHoverEnter
พารามิเตอร์
เมาส์ของ Player ที่เลื่อนออกจากพ่อของ ClickDetector หรือ DragDetector
ตัวอย่างโค้ด
The following code will print "[PlayerName] hovered over my parent!" when a player's cursor hovers over the ClickDetector parent. It will also print "[PlayerName] hovered off my parent!" when the player's cursor moves off the ClickDetector parent.
In order for this example to work as expected, it must be placed in a Script or LocalScript whose parent is a ClickDetector.
local clickDetector = script.Parent:FindFirstChildOfClass("ClickDetector")
clickDetector.MouseHoverEnter:Connect(function(player)
print(player.Name .. " hovered over my parent!")
end)
clickDetector.MouseHoverLeave:Connect(function(player)
print(player.Name .. " hovered off my parent!")
end)
RightMouseClick
อีเวนต์นี้จะเกิดจากการคลิกขวาของเมาส์ของผู้เล่นที่ Script หรือ LocalScript เมื่อผู้เล่นคลิกเมาส์เคอร์ขวาบน ClickDetector หรือ DragDetectorโปรดทราบว่า Character จะต้องอยู่ภายใน MaxActivationDistance
พารามิเตอร์
ผู้ที่ Player คลิกขวาเคอร์เซอร์ของพวกเขาบนพ่อของ ClickDetector หรือ DragDetector