点击探测器 允许 Scripts 和 LocalScripts 通过其 MouseClick 事件接收 3D 对象的指针输入。当作为父辈传给 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 事件之前发射。
代码示例
Place this code inside a Script inside a ClickDetector. The code sample creates a reference to the parent and defines a function to show a message that greets a player. Finally, it connects the MouseClick event to the defined function.
local clickDetector = script.Parent
local function onClicked(player)
-- Show a message to the player
local msg = Instance.new("Message")
msg.Parent = player:FindFirstChild("PlayerGui")
msg.Text = "Hello, " .. player.Name
wait(2.5)
msg:Destroy()
end
-- Connect the function to the MouseClick event
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 的父辈上时显示滚动器图标。如果此属性为空白,探测器将使用默认标志。
要更改鼠标标志,将此属性设置为您想要使用的图像的资产 ID。
MaxActivationDistance
此属性控制最大距离,以学分为单位,在 Character 和 ClickDetector 或 DragDetector 之间,玩家可以与其进行互动。例实例,一个具有最大激活距离为 5 的角色,距离 ClickDetector 或 DragDetector 的 10 格内,由于它们超出了范围,将无法使用探测器。
方法
活动
MouseClick
当玩家与 Script 或 LocalScript 通过以下输入互动时,此事件会发射从 ClickDetector 或 DragDetector 中:
- 在带有鼠标的平台上,当玩家离开鼠标点击时。
- 在 TouchEnabled 平台上,当玩家点击时。
- 在 GamepadEnabled 平台上,当中心点位于同一模型上且 A 按钮被按下并释放时。
请注意,玩家的 Character 必须位于探测器的 MaxActivationDistance 内。
参数
点击了父辈的 Player 或 ClickDetector 或 DragDetector 的人,谁点击了父辈。
MouseHoverEnter
当父辈的 或 被玩家悬停时,此事件会从 或 发射,当父辈的 或 被悬停时。这不会涉及与检测器的明确互动,您可以听到 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。
参数
whose 鼠标悬停在 Player 或 ClickDetector 或 DragDetector 的父辈上的 cursor。
代码示例
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 上的人。