主题
ModelUnit
概览
| 字段 | 值 |
|---|---|
| Kind | Unit · 单位 Unit |
| Realm | common |
继承关系
- Unit(5 属性 / 28 函数 / 6 事件)
继承成员
2 个来源 / 6 属性 / 34 函数 / 6 事件
- 来自 SpaceUnit(1 属性 / 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
- 属性:
ModelUnit 是用于组织多个 SpaceUnit 子对象并以统一枢轴进行整体移动的模型容器。
适用场景
在场景中需要把多个子对象当成一个整体来摆放、平移或整体缩放时使用,例如把一组装饰部件打包成一个模型,再整体移动到一个新位置并读取它占据的空间范围。
使用要点
通过 World:CreateUnit 创建 ModelUnit,把多个 SpaceUnit 子对象的 Parent 设为该容器;先设置 WorldPivot 或 PrimaryPart,再设置 Position/Rotation、调用 MoveTo(position) 或使用继承的 PivotTo 移动整个组合。
注意事项
PrimaryPart 是模型整体移动和物理模拟的参考基准;设置后枢轴跟随该部件,WorldPivot 会被忽略。未设置 PrimaryPart 时,WorldPivot 只更新模型自身枢轴,不会直接移动子对象,但 PivotTo、MoveTo 等操作会以它计算位移增量。新模型在首次设置 WorldPivot 前以原点为默认枢轴,建议移动前主动设置 WorldPivot 或 PrimaryPart。Position 与 Rotation 是 PivotTo 的便捷属性,同时仍可使用从 SpaceUnit 公开继承的 PivotTo、GetPivot、TranslateBy 等成员。
代码示例
创建模型并按枢轴整体移动
lua
-- @runtime client
local World = game:GetService("World")
local model = World:CreateUnit("ModelUnit", {
Name = "MovingGroup",
WorldPivot = CFrame.New(0, 0, 0),
})
if model == nil then return end
World:CreateUnit("WorldUnit", { Name = "ChildA", Parent = model })
World:CreateUnit("WorldUnit", { Name = "ChildB", Parent = model })
model:MoveTo(Vector3.New(10, 0, 0))
print("模型位置:", model.Position)
model:Destroy()属性 (6)
| Name | 类型 | 默认值 | 说明 |
|---|---|---|---|
PrimaryPart | SpaceUnit | - | 模型的核心部件,作为整体移动和物理模拟的参考基准。设置后枢轴跟随该部件;未设置时由 WorldPivot 决定。 |
WorldPivot | CFrame | - | 未设置 PrimaryPart 时使用的世界空间枢轴。直接赋值只更新 ModelUnit 自身枢轴,不会移动子对象;PivotTo、MoveTo 等操作以它计算位移增量。 |
Position | Vector3 | [0.0, 0.0, 0.0] | 模型在世界空间中的位置,是调用 PivotTo 的便捷属性。 |
Rotation | Quaternion | [0.0, 0.0, 0.0, 1.0] | 模型在世界空间中的四元数旋转,是调用 PivotTo 的便捷属性。 |
Scale | Float | 1.0 | ModelUnit 的整体缩放比例属性,类型为 Float,默认值为 1.0;该属性公开、可编辑且非只读,可直接读写。 |
ModelStreamingMode | ModelStreamingMode | Enums.ModelStreamingMode.Default | ModelUnit 的 ModelStreamingMode 属性用于指定该模型单位的流式加载模式,取值类型为 ModelStreamingMode 枚举。该属性公开且可编辑,不是只读属性,可在模型单位创建后通过赋值修改。 |
关联类型
函数 (5)
GetBoundingBox
签名:GetBoundingBox() -> CFrame (包围盒中心), Vector3 (包围盒尺寸)
获取 ModelUnit 模型整体的有向包围盒(OBB),包围盒朝向与模型枢轴一致。该函数无参数,按顺序返回两个独立值:第一个是 CFrame(包围盒中心的世界坐标与枢轴朝向),第二个是 Vector3(包围盒在枢轴各轴上的尺寸)。接收时应按返回值顺序分别取出,例如 local center, size = model:GetBoundingBox(),它不是可遍历的数组。
返回值 CFrame (包围盒中心), Vector3 (包围盒尺寸)
示例代码
读取包围盒中心与尺寸
lua
-- @runtime client
local World = game:GetService("World")
local model = World:CreateUnit("ModelUnit", {
Name = "BoundBoxModel",
WorldPivot = CFrame.New(0, 0, 0),
})
if model == nil then return end
-- 按顺序接收两个返回值:中心 CFrame 与尺寸 Vector3
local center, size = model:GetBoundingBox()
print("包围盒中心:", center.Position)
print(string.format("尺寸: %.2f x %.2f x %.2f", size.x, size.y, size.z))ScaleTo
签名:ScaleTo(scaleFactor: Float) -> void
把 ModelUnit 整体缩放到指定的比例系数,参数 scaleFactor 为 Float 类型的目标缩放比例(1.0 为原始大小)。
| 参数 | 类型 | 说明 |
|---|---|---|
scaleFactor | Float | 目标缩放比例,1.0 为原始大小 |
返回值 void
示例代码
缩放模型并复位到原始大小
lua
-- @runtime client
local World = game:GetService("World")
local model = World:CreateUnit("ModelUnit", { Name = "ScaledModel" })
if model == nil then return end
-- 1.0 为原始大小,2.0 表示放大到两倍
model:ScaleTo(2.0)
print("放大后缩放比例:", model:GetScale())
-- 用完后复位,避免影响同场景其他逻辑
model:ScaleTo(1.0)
print("复位后缩放比例:", model:GetScale())GetScale
签名:GetScale() -> Float (当前缩放比例)
读取 ModelUnit 当前的整体缩放比例,返回 Float;新建模型初始为 1.0。
返回值 Float (当前缩放比例)
示例代码
读取当前缩放比例
lua
-- @runtime client
local World = game:GetService("World")
local model = World:CreateUnit("ModelUnit", { Name = "ScaledModel" })
if model == nil then return end
print("当前缩放比例:", model:GetScale())MoveTo
签名:MoveTo(position: Vector3) -> void
将模型整体移动到指定的世界空间位置。
⚙ MoveTo 使用当前模型枢轴计算整体位移;移动前应先设置 WorldPivot 或 PrimaryPart,避免新模型默认原点枢轴带来意外偏移。
| 参数 | 类型 | 说明 |
|---|---|---|
position | Vector3 | 世界空间中的目标坐标 |
返回值 void
示例代码
按已设置的枢轴移动模型
lua
-- @runtime client
local World = game:GetService("World")
local model = World:CreateUnit("ModelUnit", {
Name = "MovingModel",
WorldPivot = CFrame.New(0, 0, 0),
})
if model == nil then return end
World:CreateUnit("WorldUnit", { Name = "Child", Parent = model })
model:MoveTo(Vector3.New(10, 5, 0))
print("移动后位置:", model.Position)
model:Destroy()GetExtentsSize
签名:GetExtentsSize() -> Vector3 (包围盒各轴的尺寸)
获取模型包围盒在各轴上的尺寸,返回 Vector3。
返回值 Vector3 (包围盒各轴的尺寸)
示例代码
读取模型包围盒尺寸
lua
-- @runtime client
local World = game:GetService("World")
local model = World:CreateUnit("ModelUnit", { Name = "ExtentsModel" })
if model == nil then return end
local extents = model:GetExtentsSize()
print(string.format("尺寸: x=%.2f y=%.2f z=%.2f", extents.x, extents.y, extents.z))