主题
ClickDetector
概览
| 字段 | 值 |
|---|---|
| Kind | Unit · 单位 Unit |
| Realm | common |
继承关系
- Unit(4 属性 / 25 函数 / 6 事件)
- [ClickDetector](2 属性 / 2 事件)
继承成员
1 个来源 / 4 属性 / 25 函数 / 6 事件
- 来自 Unit(4 属性 / 25 函数 / 6 事件)
- 属性:
AssetId、Name、Desc、Parent - 函数:
ClearAllChildren、GetChildren、GetDescendants、IsAncestorOf、IsDescendantOf、FindFirstAncestor、FindFirstAncestorWhichIsA、FindFirstAncestorOfClass、FindFirstChild、FindFirstChildWhichIsA、FindFirstChildOfClass、HasChildren、GetChildCount、GetChildAtIndex、SetAttribute、GetAttribute、GetAllProps、Clone、IsA、Destroy、GetPropertyChangedSignal、GetAttributeChangedSignal、FindFromPath、GetFullPath、GetDebugId - 事件:
ChildAdded、ChildRemoved、DescendantAdded、DescendantRemoving、AncestryChanged、Destroying
- 属性:
点击检测器是一个可挂载到其他单位上的交互组件,用 MaxActivationDistance 控制玩家能够触发点击的有效范围,用 Enabled 控制该检测器是否整体生效。当玩家在范围内点击所挂载的单位时,它会派发 MouseClick 与 RightMouseClick 事件,并在回调中把触发交互的玩家对象传给监听方。
适用场景
用于让场景中的单位响应玩家点击。把 ClickDetector 挂到目标单位上,监听左键或右键事件,并在回调中识别触发交互的玩家。
使用要点
先通过 World:CreateUnit("ClickDetector", {Parent = 目标单位, MaxActivationDistance = 数值, Enabled = true}) 创建实例,并在配置表里指定挂载单位与激活距离。随后用 clickDetector.MouseClick:Connect(function(player) ... end) 和 clickDetector.RightMouseClick:Connect(function(player) ... end) 分别注册左键与右键回调,在回调内部可读写 clickDetector.MaxActivationDistance、clickDetector.Enabled 或 clickDetector.Parent 来完成交互逻辑。
注意事项
ClickDetector 的触发端与部署环境有关,实测存在客户端与服务端都可监听到事件的情况。若同一交互同时在双端监听,请在业务层去重;MouseClick/RightMouseClick 回调中的 player 参数用于识别触发交互的玩家。
代码示例
创建点击检测器并监听一次左键点击
lua
-- @runtime client
local world = game:GetService("World")
local target = world:CreateUnit("WorldUnit", { Position = Vector3.New(0, 2, 0) })
if target == nil then return end
local detector = world:CreateUnit("ClickDetector", {
Parent = target,
MaxActivationDistance = 15,
Enabled = true,
})
if detector == nil then return end
detector.MouseClick:Once(function(player)
print("点击玩家:", player:GetName())
end)属性 (2)
| Name | 类型 | 默认值 | 说明 |
|---|---|---|---|
MaxActivationDistance | Float | 32.0 | 玩家与物体之间的最大距离,超过此距离点击事件不会触发。 |
Enabled | Bool | true | 控制 ClickDetector 是否启用点击检测。 |
关联类型
事件 (2)
MouseClick
签名:MouseClick(player: Player) 触发:local
MouseClick 是 ClickDetector 单位提供的鼠标左键点击事件。当玩家用鼠标左键点击该 ClickDetector 所挂载的对象时触发,回调会收到一个 player 参数,其类型为 Player,代表发起本次点击的玩家。
| 回调参数 | 类型 | 说明 |
|---|---|---|
player | Player | 触发的玩家 |
示例代码
监听左键点击
lua
-- @runtime client
local world = game:GetService('World')
local target = world:CreateUnit('WorldUnit', {})
if target == nil then return end
local detector = world:CreateUnit('ClickDetector', { Parent = target, MaxActivationDistance = 30, Enabled = true })
if detector == nil then return end
detector.MouseClick:Once(function(player)
print('左键点击玩家:', player)
end)RightMouseClick
签名:RightMouseClick(player: Player) 触发:local
RightMouseClick 是 ClickDetector 单位提供的鼠标右键点击事件。当玩家用鼠标右键点击该 ClickDetector 所挂载的对象时触发,回调会收到一个 player 参数,其类型为 Player,代表发起本次点击的玩家。
| 回调参数 | 类型 | 说明 |
|---|---|---|
player | Player | 触发的玩家 |
示例代码
监听右键点击
lua
-- @runtime client
local world = game:GetService('World')
local target = world:CreateUnit('WorldUnit', {})
if target == nil then return end
local detector = world:CreateUnit('ClickDetector', { Parent = target, MaxActivationDistance = 30, Enabled = true })
if detector == nil then return end
detector.RightMouseClick:Once(function(player)
print('右键点击玩家:', player)
end)