主题
WeldConstraint
概览
| 字段 | 值 |
|---|---|
| Kind | Unit · 单位 Unit |
| Realm | common |
继承关系
- Unit(4 属性 / 25 函数 / 6 事件)
- [WeldConstraint](4 属性 / 1 函数)
继承成员
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
- 属性:
WeldConstraint 用于描述多个对象之间的刚性连接。可操作编辑器中已配置的约束,也可在服务端创建时通过 ConnectUnitList 与 ExpectRootUnit 配置连接对象。
适用场景
适合查找和控制编辑器中已完成对象绑定的焊接约束;脚本侧应自行保留业务对象引用。一个 WeldConstraint 可采用一个根对象连接多个子对象的一连多结构。
使用要点
优先在编辑器中完成焊接配置,再由脚本按名称查找该 WeldConstraint 并读取或启停。参与焊接的物理对象应保持非 Static;同一对象不要加入多份 WeldConstraint。销毁旧约束后不要在同一调度时机立即为同一组对象重建,应等待旧关系完成清理并再次确认目标状态。
注意事项
ConnectUnitList 与 ExpectRootUnit 是服务端创建时的配置参数,创建完成后不提供编辑能力;请保留参与连接的 Unit 引用。当前运行包的焊接规则是:Static 物理对象不能参与 WeldConstraint;同一个 Unit 最多属于一份 WeldConstraint,但一份约束可以用一个根对象连接多个子对象。旧约束销毁与底层物理关系清理并非同一时刻完成,同一组对象在同一调度时机立即重建可能不生效;应拆分销毁与重建时机,并在目标运行包确认旧关系已解除。
代码示例
读取编辑器预配置的焊接关系
lua
-- @runtime server
local World = game:GetService("World")
local weld = World:FindFirstChild("MyWeldConstraint", true)
local root = World:FindFirstChild("WeldRoot", true)
local child = World:FindFirstChild("WeldChild", true)
if weld == nil or root == nil or child == nil then
print("请先在编辑器中配置焊接关系,并用名称保存对象引用")
return
end
print("焊接约束:", weld.Name, "根对象:", root.Name, "子对象:", child.Name)属性 (4)
| Name | 类型 | 默认值 | 说明 |
|---|---|---|---|
Enabled | Bool | true | 布尔值,表示作者对该焊缝约束的启用意图。默认值为 true,即默认启用;设为 false 可禁用该约束。 |
Active | Bool | false | 只读布尔值,表示当前焊缝约束是否处于生效状态。该属性用于查询约束的实际生效情况,不可由作者直接修改。 |
ConnectUnitList | Array<BasePart> | [] | 该属性以数组形式记录焊接约束所连接的单元,标题标注为“连接的单元列表(仅服务端创建参数)”。 |
ExpectRootUnit | BasePart | - | 该属性用于指定焊接约束期望的根单元,类型为 BasePart,标题标注为“期望Root单元(仅服务端创建参数)”。 |
关联类型
函数 (1)
GetRootUnit
签名:GetRootUnit() -> BasePart
获取该焊接约束所对应装配体的根单元,返回值为 BasePart。
返回值 BasePart
示例代码
读取已配置焊接约束的根单元
lua
-- @runtime server
local world = game:GetService('World')
local weld = world:FindFirstChild('MyWeldConstraint', true)
if weld == nil or not weld:IsA('WeldConstraint') then return end
local root = weld:GetRootUnit()
if root ~= nil then print('焊接根单元:', root.Name) end