主题
ClickDetector
概览
| 字段 | 值 |
|---|---|
| Kind | Unit · 单位 Unit |
| Realm | common |
继承关系
- Unit(5 属性 / 28 函数 / 6 事件)
- [ClickDetector](2 属性 / 2 事件)
继承成员
1 个来源 / 5 属性 / 28 函数 / 6 事件
- 来自 Unit(5 属性 / 28 函数 / 6 事件)
- 属性:
AssetId、Name、Desc、Parent、Tags - 函数:
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、HasTag、AddTag、RemoveTag - 事件:
ChildAdded、ChildRemoved、DescendantAdded、DescendantRemoving、AncestryChanged、Destroying
- 属性:
点击检测器是一种可挂载到其他单位上的交互组件,通过 Parent 属性依附到目标单位后,用于识别玩家鼠标左键与右键的点击行为。它提供最大激活距离与启用开关两个可调属性,并在点击命中时向监听者回调传入触发的玩家对象,便于按玩家身份做差异化处理。
适用场景
用于让场景中的单位响应玩家点击。把 ClickDetector 挂到目标单位上,监听左键或右键事件,并在回调中识别触发交互的玩家。
使用要点
先通过 game:CreateUnit("ClickDetector", config) 或 World:CreateUnit("ClickDetector", config) 创建实例,并在配置中指定 Parent 挂载目标、MaxActivationDistance 与 Enabled。随后用 MouseClick:Connect 与 RightMouseClick:Connect 分别连接左键和右键回调,回调参数即为触发的玩家对象;也可在回调内调整 MaxActivationDistance 或把 Enabled 置为关闭。
注意事项
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
ClickDetector 的鼠标左键点击事件。当玩家用鼠标左键点击挂载了该 ClickDetector 的单位时触发,回调函数会收到一个 Player 类型的参数,代表执行点击操作的玩家。该事件为本地事件,可通过 Connect 或 Once 连接响应。
| 回调参数 | 类型 | 说明 |
|---|---|---|
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
ClickDetector 的鼠标右键点击事件。当玩家用鼠标右键点击挂载了该 ClickDetector 的单位时触发,回调函数会收到一个 Player 类型的参数,代表执行点击操作的玩家。该事件为本地事件,可通过 Connect 或 Once 连接响应。
| 回调参数 | 类型 | 说明 |
|---|---|---|
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)