主题
PathfindingLink
概览
| 字段 | 值 |
|---|---|
| Kind | Unit · 单位 Unit |
| Realm | common |
继承关系
- Unit(4 属性 / 25 函数 / 6 事件)
- [PathfindingLink](6 属性)
继承成员
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
- 属性:
PathfindingLink 在两个 Attachment 端点之间建立导航网格之外的寻路连接。它可通过 IsBidirectional 控制是否双向通行,并用 Label 标记连接用途;运行时新增或修改的连接会在启用动态导航网格时参与重烘焙。
适用场景
常用于补全导航网格无法表达的通道,例如把两个平台用寻路链接接起来,让 CreatePath 生成的路线可以跨越该缝隙;也可在运行过程中按 Label 动态增删这类连接来改变可通行区域。
使用要点
通过 World:CreateUnit("PathfindingLink", {}) 创建实例,将两个 Attachment 分别赋给 Attachment0 和 Attachment1,并按需设置 IsBidirectional 与 Label。运行时创建、销毁或修改链接后,只有 PathfindingService.HasDynamicNavMesh 为 true 时,导航网格才会重烘焙并反映该变化。
注意事项
Attachment0 和 Attachment1 应传入 Attachment 实例引用,而不是 Attachment.UnitId 字符串或数字;它们用于定义寻路连接的两个端点。运行时新增或修改链接时,需确认 PathfindingService.HasDynamicNavMesh 为 true 才会触发重烘焙。
代码示例
用两个 Attachment 创建动态寻路链接
lua
-- @runtime server
local World = game:GetService('World')
local PathfindingService = game:GetService('PathfindingService')
if not PathfindingService.HasDynamicNavMesh then
print('当前未启用动态导航网格')
return
end
local startPart = World:CreateUnit('WorldUnit', { Position = Vector3.New(0, 0, 0) })
local endPart = World:CreateUnit('WorldUnit', { Position = Vector3.New(5, 0, 0) })
local startAttachment = World:CreateUnit('Attachment', { Parent = startPart })
local endAttachment = World:CreateUnit('Attachment', { Parent = endPart })
local link = World:CreateUnit('PathfindingLink', {
Attachment0 = startAttachment,
Attachment1 = endAttachment,
IsBidirectional = true,
Label = 'shortcut',
})
print('寻路链接标签:', link.Label)属性 (6)
| Name | 类型 | 默认值 | 说明 |
|---|---|---|---|
AttachmentID0 | Int | 0 | 起始锚点 ID,整数属性,用于记录寻路链接起点所对应锚点的编号。该属性默认值为 0,可读写,仅描述起点一端的锚点标识。 |
AttachmentID1 | Int | 0 | 终点锚点 ID,整数属性,用于记录寻路链接终点所对应锚点的编号。该属性默认值为 0,可读写,仅描述终点一端的锚点标识。 |
Attachment0 | Attachment | - | 起点锚点。Attachment0 是路径链接的起始端,该属性保存一个 Attachment 类型的引用,用于表示这条路径链接从哪个锚点发起。 |
Attachment1 | Attachment | - | 终点锚点。Attachment1 是路径链接的终止端,该属性保存一个 Attachment 类型的引用,用于表示这条路径链接到达哪个锚点结束。 |
IsBidirectional | Bool | true | 是否允许双向通行,决定寻路时是否可以从终点向起点移动。 |
Label | String | "" | 区域标签,用于对寻路链接进行分类或过滤。 |
