主题
WorldUnit
概览
| 字段 | 值 |
|---|---|
| Kind | Unit · 单位 Unit |
| Realm | common |
继承关系
- Unit(5 属性 / 28 函数 / 6 事件)
- SpaceUnit(1 属性 / 6 函数)
- BasePart(24 属性 / 9 函数)
- [WorldUnit](31 属性 / 20 函数 / 6 事件)
- AnimatedUnit(3 函数)
- SpawnLocationUnit(8 属性)
- [WorldUnit](31 属性 / 20 函数 / 6 事件)
- BasePart(24 属性 / 9 函数)
- SpaceUnit(1 属性 / 6 函数)
继承成员
3 个来源 / 30 属性 / 43 函数 / 6 事件
- 来自 BasePart(24 属性 / 9 函数)
- 属性:
CFrame、PivotOffset、Position、Rotation、Scale、Size、ModelAlpha、ModelVisible、CastShadow、ModelColor1、ModelColor2、ModelColor3、ModelColor4、SkinId、UseCustomAppearance、CustomAppearanceId、OcclusionType、AssemblyRootPriority、AssemblyLinearVelocity、AssemblyAngularVelocity、AssemblyMass、AssemblyCenterOfMass、Persistent、IsStaticOptimization - 函数:
IsNetworkOwnerSide、GetNetworkOwner、SetNetworkOwner、GetNetworkOwnershipAuto、SetNetworkOwnershipAuto、IsWeldConstraintRoot、IsWeldConstraintChild、GetConnectedUnits、SyncRenderToPhysics
- 属性:
- 来自 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
- 属性:
WorldUnit 是同时具备渲染外观与刚体物理的三维实体,可设置模型资源、物理资源、碰撞分组与质量等参数,并支持施加力、力矩、冲量以及查询指定位置的线速度。它还提供多槽位皮肤与贴图设置能力,并在碰撞进入、碰撞离开以及被抓起、放下时发出本地事件,便于构建可推拉、可碰撞、可交互的场景物体。
适用场景
常用于制作可被推动或击飞的箱子、可被抓举投掷的物件,以及需要响应碰撞与射线检测的场景障碍物;也适合在客户端调整皮肤槽位与模型颜色来呈现不同外观。
使用要点
先通过 World:CreateUnit("WorldUnit", config) 创建实例,按需配置 RenderMeshId、PhysicsMeshId、Enums.BodyType.Dynamic 与 PhysicsActive。创建后可用 ApplyImpulse、ApplyForceToCenterOfMass 或 ApplyTorque 施加物理作用;GetVelocityAtPosition(position) 返回指定世界位置的 Vector3 速度向量。碰撞过滤使用碰撞组或不碰撞单位对接口,实体不再使用时调用 Destroy()。
注意事项
施加力、冲量或力矩前,应把 BodyType 设为 Enums.BodyType.Dynamic 并启用 PhysicsActive;需要可见且可碰撞的实体时,同时设置有效的 RenderMeshId 与 PhysicsMeshId。UseSkinList 与 SkinSlots 是公开的运行时属性;修改槽位列表后应整体赋回 SkinSlots。
代码示例
创建可见的动态物理单位
lua
-- @runtime client
local world = game:GetService("World")
local unit = world:CreateUnit("WorldUnit", {
Name = "DynamicCrate",
RenderMeshId = "official://mesh/59268",
PhysicsMeshId = "official://mesh/59268",
Position = Vector3.New(0, 3, 0),
BodyType = Enums.BodyType.Dynamic,
PhysicsActive = true,
})
unit:ApplyImpulse(Vector3.New(0, 80, 0))属性 (31)
| Name | 类型 | 默认值 | 说明 |
|---|---|---|---|
RenderMeshId | String | official://mesh/59268 | 模型资源ID,类型为字符串,用于指定该 WorldUnit 所引用的渲染模型资源标识。 |
LocalTransparencyModifier | Float | 1 | WorldUnit 的本地透明度乘数,用于在本机侧对其透明度做整体调节。 |
ModelBindParent | Bool | false | 启用后,当父节点为 WorldUnit、RenderUnit、TriggerUnit、PhysicsUnit 等支持父子带动的场景单位时,当前单位将整体跟随父节点运动,保持与父节点的相对位置和旋转。运行时可通过脚本设置 .ModelBindParent = true/false 动态切换该行为。 |
TransparentRenderBias | Int | 0 | WorldUnit 渲染半透明效果时使用的偏置值。 |
UseSkinList | Bool | false | 多槽位皮肤开关;启用后按各子网格的 SkinSlots 配置皮肤与染色。 |
SkinSlots | Array<SkinSlot> | [] | 各子网格的皮肤与染色配置列表;元素包含 SkinId 和 ModelColor1~4。 |
PhysicsMeshId | String | official://mesh/59268 | WorldUnit 参与物理计算所使用的物理资源标识,取值为资源 URI 字符串。 |
BodyType | Int | Enums.BodyType.Dynamic | 物理类型标识;赋值时使用 Enums.BodyType.Static、Enums.BodyType.Kinematic 或 Enums.BodyType.Dynamic。 |
PhysicsActive | Bool | true | 是否启用物理模拟,关闭后物体不参与碰撞和物理计算 |
CollisionGroup | String | "" | WorldUnit 所属的碰撞组名称,用于碰撞关系的分组管理。 |
CanCollide | Bool | true | 设置 WorldUnit 能否与其他单位发生碰撞。 |
CanTouch | Bool | true | 设置本 WorldUnit 能否被其他单位触发碰撞事件。 |
CanTrigger | Bool | true | 设置本 WorldUnit 能否触发其他单位的碰撞事件。 |
Mass | Float | 10 | 物体的质量,仅对动态(Dynamic)物体生效 |
Massless | Bool | false | 设置 WorldUnit 是否为无质量。 |
CustomPhysicalProperties | PhysicalProperties | - | 自定义物理材质属性,类型为 PhysicalProperties,用于为 WorldUnit 提供自定义的物理材质数据。 |
CenterOfMass | Vector3 | [0, 0, 0] | WorldUnit 的 CenterOfMass 属性,类型为 Vector3,表示该单位的质心,默认值为 (0, 0, 0)。 |
GravityEnabled | Bool | true | 是否受重力影响,仅对动态(Dynamic)物体生效 |
UseIndividualGravity | Bool | false | 设置 WorldUnit 是否使用自定义重力值。 |
IndividualGravityValue | Vector3 | [0, -9.8, 0] | WorldUnit 的 IndividualGravityValue 属性,类型为 Vector3,表示该单位使用的自定义重力值,默认值为 (0, -9.8, 0)。 |
LinearDamping | Float | 0 | 线速度的阻尼系数,值越大减速越快,仅对动态(Dynamic)物体生效 |
AngularDamping | Float | 0 | 角速度的阻尼系数,值越大旋转减速越快,仅对动态(Dynamic)物体生效 |
AngularVelocity | Vector3 | [0, 0, 0] | WorldUnit 的 AngularVelocity 属性,类型为 Vector3,表示该单位的角速度,默认值为 (0, 0, 0)。 |
LinearVelocity | Vector3 | [0, 0, 0] | WorldUnit 的 LinearVelocity 属性,类型为 Vector3,表示该单位的线速度,默认值为 (0, 0, 0)。 |
Climbable | Bool | false | Climbable 是 WorldUnit 的布尔属性,用于标记该单位是否可被攀爬。该属性可读写,未设置时使用默认值 false。 |
Liftable | Bool | false | 玩家角色是否可以抓举此物体,仅对动态(Dynamic)物体生效 |
UseCustomThrownAngle | Bool | false | 设置 WorldUnit 是否使用自定义投掷角度。 |
CustomThrownAngle | Float | 45.0 | 设置 WorldUnit 被投掷时使用的角度。 |
UseCustomThrownForce | Bool | false | 布尔类型属性,用于决定投掷单位时是否使用 CustomThrownForce 指定的自定义力度。 |
CustomThrownForce | Float | 150.0 | 浮点类型属性,表示投掷单位时使用的自定义力度数值。 |
CanQuery | Bool | true | 布尔类型属性,用于控制该 WorldUnit 是否可以被射线检测等查询操作命中。默认值为 true,表示该单位参与查询。 |
关联类型
事件 (6)
OnCollisionEnter
签名:OnCollisionEnter(otherUnit: Unit) 触发:local
WorldUnit 的碰撞开始事件。当该 WorldUnit 与其他单位开始发生碰撞时触发,回调会收到一个参数 otherUnit,表示参与本次碰撞的另一个 Unit。
| 回调参数 | 类型 | 说明 |
|---|---|---|
otherUnit | Unit | 与之发生碰撞的另一个物体 |
示例代码
监听编辑器动态物体开始碰撞
lua
-- @runtime client
local unit = game:GetService("World"):FindFirstChild("DynamicCrate", true)
if unit == nil or not unit:IsA("WorldUnit") then return end
unit.OnCollisionEnter:Once(function(otherUnit)
print("开始碰撞:", otherUnit.Name)
end)OnCollisionExit
签名:OnCollisionExit(otherUnit: Unit) 触发:local
WorldUnit 的碰撞结束事件。当该 WorldUnit 与某个单位结束碰撞、彼此分离时触发,回调会收到一个参数 otherUnit,表示刚刚结束碰撞的另一个 Unit。
| 回调参数 | 类型 | 说明 |
|---|---|---|
otherUnit | Unit | 结束碰撞的另一个物体 |
示例代码
监听编辑器动态物体结束碰撞
lua
-- @runtime client
local unit = game:GetService("World"):FindFirstChild("DynamicCrate", true)
if unit == nil or not unit:IsA("WorldUnit") then return end
unit.OnCollisionExit:Once(function(otherUnit)
print("结束碰撞:", otherUnit.Name)
end)OnLocalCollisionEnter
签名:OnLocalCollisionEnter(info: CollisionCallbackInfo) 触发:local
本地碰撞开始事件,回调参数为 info(类型 CollisionCallbackInfo),其中包含碰撞对方单位信息。
| 回调参数 | 类型 | 说明 |
|---|---|---|
info | CollisionCallbackInfo | 碰撞回调信息 |
示例代码
监听 WorldUnit.OnLocalCollisionEnter 事件的示例。
lua
-- @runtime client
local worldUnit = game:CreateUnit("WorldUnit", {})
worldUnit.OnLocalCollisionEnter:Once(function(info)
print("info: " .. tostring(info))
end)OnLocalCollisionExit
签名:OnLocalCollisionExit(info: CollisionCallbackInfo) 触发:local
本地碰撞结束事件,回调参数为 info(类型 CollisionCallbackInfo)。
| 回调参数 | 类型 | 说明 |
|---|---|---|
info | CollisionCallbackInfo | 碰撞回调信息 |
示例代码
监听 WorldUnit.OnLocalCollisionExit 事件的示例。
lua
-- @runtime client
local worldUnit = game:CreateUnit("WorldUnit", {})
worldUnit.OnLocalCollisionExit:Once(function(info)
print("info: " .. tostring(info))
end)OnLiftedBegin
签名:OnLiftedBegin(liftunit: EggyUnit) 触发:local
WorldUnit 的举起开始事件。当该 WorldUnit 被举起行为开始时触发,回调会收到一个参数 liftunit,类型为 EggyUnit,表示发起举起的蛋仔单位。
| 回调参数 | 类型 | 说明 |
|---|---|---|
liftunit | EggyUnit | 举起当前对象的生物 |
示例代码
监听 WorldUnit.OnLiftedBegin 事件的示例。
lua
-- @runtime client
local worldUnit = game:CreateUnit("WorldUnit", {})
worldUnit.OnLiftedBegin:Once(function(liftunit)
print("liftunit: " .. tostring(liftunit))
end)OnLiftedEnd
签名:OnLiftedEnd() 触发:local
当本 WorldUnit 结束被抓举状态时触发的本地事件,回调无参数。
示例代码
监听 WorldUnit.OnLiftedEnd 事件的示例。
lua
-- @runtime client
local worldUnit = game:CreateUnit("WorldUnit", {})
worldUnit.OnLiftedEnd:Once(function()
print("OnLiftedEnd event")
end)函数 (20)
AddCollisionWithGroup
签名:AddCollisionWithGroup(groupName: String) -> void
添加一个允许与当前 WorldUnit 发生碰撞的碰撞组。
| 参数 | 类型 | 说明 |
|---|---|---|
groupName | String | 要加入的碰撞组名称 |
返回值 void
示例代码
注册并添加允许碰撞的组
lua
-- @runtime server
local physicsService = game:GetService("PhysicsService")
local groupName = "MyGroup"
pcall(function() physicsService:RegisterCollisionGroup(groupName) end)
local unit = game:GetService("World"):CreateUnit("WorldUnit", {})
unit:AddCollisionWithGroup(groupName)RemoveCollisionWithGroup
签名:RemoveCollisionWithGroup(groupName: String) -> void
从当前 WorldUnit 的允许碰撞列表中移除指定碰撞组。
| 参数 | 类型 | 说明 |
|---|---|---|
groupName | String | 要移除的碰撞组名称 |
返回值 void
示例代码
移除已添加的碰撞组
lua
-- @runtime server
local physicsService = game:GetService("PhysicsService")
local groupName = "TemporaryGroup"
pcall(function() physicsService:RegisterCollisionGroup(groupName) end)
local unit = game:GetService("World"):CreateUnit("WorldUnit", {})
unit:AddCollisionWithGroup(groupName)
unit:RemoveCollisionWithGroup(groupName)GetCollisionWithGroups
签名:GetCollisionWithGroups() -> Array<String>
获取当前 WorldUnit 允许发生碰撞的碰撞组名称列表。
返回值 Array<String>
示例代码
读取允许碰撞的组
lua
-- @runtime client
local unit = game:GetService("World"):CreateUnit("WorldUnit", {})
for _, groupName in ipairs(unit:GetCollisionWithGroups()) do
print("允许碰撞组:", groupName)
endApplyForceToCenterOfMass
签名:ApplyForceToCenterOfMass(force: Vector3) -> void
在 WorldUnit 的质心处施加力。参数 force 为 Vector3 类型的力向量,函数无返回值。
| 参数 | 类型 | 说明 |
|---|---|---|
force | Vector3 | 世界空间中的力向量 |
返回值 void
示例代码
给动态单位的质心施加向上的力
lua
-- @runtime client
local unit = game:GetService("World"):CreateUnit("WorldUnit", { BodyType = Enums.BodyType.Dynamic, PhysicsActive = true })
unit:ApplyForceToCenterOfMass(Vector3.New(0, 200, 0))ApplyForceAtLocalPosition
签名:ApplyForceAtLocalPosition(force: Vector3, localPosition: Vector3) -> void
在 WorldUnit 的局部坐标位置施加力。参数 force 为 Vector3 类型的力向量,localPosition 为 Vector3 类型的局部坐标位置,函数无返回值。
| 参数 | 类型 | 说明 |
|---|---|---|
force | Vector3 | 世界空间中的力向量 |
localPosition | Vector3 | 物体局部空间中的施力点坐标 |
返回值 void
示例代码
在动态单位的局部位置施加力
lua
-- @runtime client
local unit = game:GetService("World"):CreateUnit("WorldUnit", { BodyType = Enums.BodyType.Dynamic, PhysicsActive = true })
unit:ApplyForceAtLocalPosition(Vector3.New(0, 100, 0), Vector3.New(1, 0, 0))ApplyForceAtWorldPosition
签名:ApplyForceAtWorldPosition(force: Vector3, worldPosition: Vector3) -> void
在 WorldUnit 的世界坐标位置施加力。参数 force 为 Vector3 类型的力向量,worldPosition 为 Vector3 类型的世界坐标位置,函数无返回值。
| 参数 | 类型 | 说明 |
|---|---|---|
force | Vector3 | 世界空间中的力向量 |
worldPosition | Vector3 | 世界空间中的施力点坐标 |
返回值 void
示例代码
在动态单位的世界位置施加力
lua
-- @runtime client
local unit = game:GetService("World"):CreateUnit("WorldUnit", { Position = Vector3.New(0, 5, 0), BodyType = Enums.BodyType.Dynamic, PhysicsActive = true })
unit:ApplyForceAtWorldPosition(Vector3.New(0, 0, 100), unit.Position + Vector3.New(0, 0, 1))ApplyTorque
签名:ApplyTorque(torque: Vector3) -> void
对 WorldUnit 施加力矩。参数 torque 为 Vector3 类型的力矩向量,函数无返回值。
| 参数 | 类型 | 说明 |
|---|---|---|
torque | Vector3 | 各轴方向的力矩大小 |
返回值 void
示例代码
给动态单位施加绕 Y 轴的力矩
lua
-- @runtime client
local unit = game:GetService("World"):CreateUnit("WorldUnit", { BodyType = Enums.BodyType.Dynamic, PhysicsActive = true })
unit:ApplyTorque(Vector3.New(0, 10, 0))ApplyImpulse
签名:ApplyImpulse(impulse: Vector3) -> void
在 WorldUnit 的质心处施加冲量。参数 impulse 为 Vector3 类型的冲量向量,函数无返回值。
| 参数 | 类型 | 说明 |
|---|---|---|
impulse | Vector3 | 冲量向量,方向和大小决定瞬时速度变化 |
返回值 void
示例代码
给动态单位的质心施加向上冲量
lua
-- @runtime client
local unit = game:GetService("World"):CreateUnit("WorldUnit", { BodyType = Enums.BodyType.Dynamic, PhysicsActive = true })
unit:ApplyImpulse(Vector3.New(0, 50, 0))ApplyImpulseAtLocalPosition
签名:ApplyImpulseAtLocalPosition(impulse: Vector3, localPosition: Vector3) -> void
在 WorldUnit 的局部坐标位置施加冲量。参数 impulse 为 Vector3 类型的冲量向量,localPosition 为 Vector3 类型的局部坐标位置,函数无返回值。
| 参数 | 类型 | 说明 |
|---|---|---|
impulse | Vector3 | 冲量向量 |
localPosition | Vector3 | 物体局部空间中的冲量施加点坐标 |
返回值 void
示例代码
在动态单位的局部位置施加冲量
lua
-- @runtime client
local unit = game:GetService("World"):CreateUnit("WorldUnit", { BodyType = Enums.BodyType.Dynamic, PhysicsActive = true })
unit:ApplyImpulseAtLocalPosition(Vector3.New(0, 0, 50), Vector3.New(0, 0, 1))ApplyImpulseAtWorldPosition
签名:ApplyImpulseAtWorldPosition(impulse: Vector3, worldPosition: Vector3) -> void
在 WorldUnit 的世界坐标位置施加冲量。参数 impulse 为 Vector3 类型的冲量向量,worldPosition 为 Vector3 类型的世界坐标位置,函数无返回值。
| 参数 | 类型 | 说明 |
|---|---|---|
impulse | Vector3 | 冲量向量 |
worldPosition | Vector3 | 世界空间中的冲量施加点坐标 |
返回值 void
示例代码
在动态单位的世界位置施加冲量
lua
-- @runtime client
local unit = game:GetService("World"):CreateUnit("WorldUnit", { Position = Vector3.New(0, 5, 0), BodyType = Enums.BodyType.Dynamic, PhysicsActive = true })
unit:ApplyImpulseAtWorldPosition(Vector3.New(50, 0, 0), unit.Position + Vector3.New(1, 0, 0))GetVelocityAtPosition
签名:GetVelocityAtPosition(position: Vector3) -> Vector3
获取 WorldUnit 在指定世界坐标位置处的速度。参数 position 为 Vector3 类型的世界坐标,返回 Vector3 类型的速度。
| 参数 | 类型 | 说明 |
|---|---|---|
position | Vector3 | 要查询速度的世界空间坐标点 |
返回值 Vector3
示例代码
读取动态单位指定世界位置的速度
lua
-- @runtime client
local unit = game:GetService("World"):CreateUnit("WorldUnit", { Position = Vector3.New(0, 5, 0), BodyType = Enums.BodyType.Dynamic, PhysicsActive = true })
unit:ApplyImpulse(Vector3.New(50, 0, 0))
local velocity = unit:GetVelocityAtPosition(unit.Position + Vector3.New(1, 0, 0))
print("指定位置速度:", velocity)AddNoCollisionPairWithUnit
签名:AddNoCollisionPairWithUnit(targetUnit: SpaceUnit) -> void
AddNoCollisionPairWithUnit 用于为当前 WorldUnit 添加一个不碰撞单位对:调用后,该单位与参数 targetUnit 之间形成不碰撞关系。函数无返回值。
| 参数 | 类型 | 说明 |
|---|---|---|
targetUnit | SpaceUnit | 要互不碰撞的目标单位 |
返回值 void
示例代码
与另一个空间单位禁用碰撞
lua
-- @runtime client
local world = game:GetService("World")
local worldUnit = world:CreateUnit("WorldUnit", { Position = Vector3.New(0, 2, 0) })
local targetUnit = world:CreateUnit("WorldUnit", { Position = Vector3.New(2, 2, 0) })
worldUnit:AddNoCollisionPairWithUnit(targetUnit)
print("已添加互不碰撞目标:", targetUnit.Name)RemoveNoCollisionPairWithUnit
签名:RemoveNoCollisionPairWithUnit(targetUnit: SpaceUnit) -> void
RemoveNoCollisionPairWithUnit 用于从当前 WorldUnit 的不碰撞关系列表中移除参数 targetUnit 对应的单位对,恢复两者之间的正常碰撞判定。函数无返回值。
| 参数 | 类型 | 说明 |
|---|---|---|
targetUnit | SpaceUnit | 要恢复碰撞的目标单位 |
返回值 void
示例代码
恢复与另一个空间单位碰撞
lua
-- @runtime client
local world = game:GetService("World")
local worldUnit = world:CreateUnit("WorldUnit", { Position = Vector3.New(0, 2, 0) })
local targetUnit = world:CreateUnit("WorldUnit", { Position = Vector3.New(2, 2, 0) })
worldUnit:AddNoCollisionPairWithUnit(targetUnit)
worldUnit:RemoveNoCollisionPairWithUnit(targetUnit)
print("已恢复与目标单位的碰撞:", targetUnit.Name)RemoveAllNoCollisionPairWithUnit
签名:RemoveAllNoCollisionPairWithUnit() -> void
移除 WorldUnit 上的全部不碰撞单位对,无参数。
返回值 void
示例代码
调用 WorldUnit:RemoveAllNoCollisionPairWithUnit 的示例。
lua
-- @runtime client
local worldUnit = game:CreateUnit("WorldUnit", {})
worldUnit:RemoveAllNoCollisionPairWithUnit()
print("调用完成", worldUnit)GetNoCollisionPairUnitList
签名:GetNoCollisionPairUnitList() -> Array<SpaceUnit>
GetNoCollisionPairUnitList 用于获取当前 WorldUnit 已设置的全部不碰撞单位,返回一个 SpaceUnit 数组;函数不需要参数。
返回值 Array<SpaceUnit>
示例代码
调用 WorldUnit:GetNoCollisionPairUnitList 的示例。
lua
-- @runtime client
local worldUnit = game:CreateUnit("WorldUnit", {})
local result = worldUnit:GetNoCollisionPairUnitList() -- 返回 Array
if result ~= nil then
for index, item in ipairs(result) do
print(index, item)
end
endSetTexture
签名:SetTexture(textureAssetId: String, texIndex?: TextureChannel, slotIndex?: SkinSlotIndex) -> void
WorldUnit 的 SetTexture 函数,用于为 WorldUnit 设置贴图资源。第一个参数 textureAssetId 为贴图资源标识;可选参数 texIndex 指定纹理通道(TextureChannel),可选参数 slotIndex 指定皮肤槽位(SkinSlotIndex)。
⚙ textureAssetId 使用项目中真实存在且有权使用的图片 URI;字符串形态检查不证明资源可用。texIndex 缺省为 Enums.TextureChannel.MAIN;slotIndex 缺省为 Enums.SkinSlotIndex.ALL,在单皮肤模式作用于当前皮肤,在多槽位模式作用于全部槽位。传空字符串清除指定通道覆盖;覆盖全部清空后回退原皮肤。启用自定义外观或单位没有皮肤时,本次调用被忽略。服务端调用同步全端,客户端调用仅本机生效。SetTexture 无返回值,调用结束或打印日志不能作为贴图已加载、视觉效果已确认的证明。示例只在调用方显式调用后修改按名称选中的已有单位,不创建、销毁或改变皮肤配置。目标名必须指向调用方有权操作的预期单位;实例生命周期由调用方管理。
| 参数 | 类型 | 说明 |
|---|---|---|
textureAssetId | String | 贴图资源ID,传空字符串清除该通道覆盖 |
texIndex? | TextureChannel | 贴图通道(TextureChannel.MAIN=主贴图 / NORMAL=法线 / MRE),缺省 TextureChannel.MAIN |
slotIndex? | SkinSlotIndex | 皮肤槽位(SkinSlotIndex.SLOT_1~4);缺省或 SkinSlotIndex.ALL 时:多槽位模式作用于全部槽位、单皮肤模式作用于当前皮肤 |
返回值 void
示例代码
按已有单位名覆盖单皮肤主贴图(真实输入后显式调用)
lua
-- @runtime client
-- 接受调用方明确指定的已有单位名和真实、有权使用的图片 URI;本段只定义函数。
local function applySingleSkinTexture(targetName, textureAssetId)
if type(targetName) ~= "string" or targetName == "" then return end
if type(textureAssetId) ~= "string" then return end
if not string.match(textureAssetId, "^official://.+") and not string.match(textureAssetId, "^custom://.+") then return end
local world = game:GetService("World")
local unit = world:FindFirstChild(targetName, true)
if unit == nil then return end
if not unit:IsA("WorldUnit") then return end
if unit.UseCustomAppearance then return end
if unit.UseSkinList then return end
if unit.SkinId == nil or unit.SkinId == "" then return end
unit:SetTexture(textureAssetId, Enums.TextureChannel.MAIN, Enums.SkinSlotIndex.ALL)
endSetRenderMeshWithDefaultSkin
签名:SetRenderMeshWithDefaultSkin(renderMeshId: String) -> void
切换模型并应用默认皮肤,按传入的模型资源标识替换当前渲染模型,并使用该模型的默认皮肤。
| 参数 | 类型 | 说明 |
|---|---|---|
renderMeshId | String | 模型资源ID |
返回值 void
示例代码
切换为项目配置的模型并应用默认皮肤
lua
-- @runtime client
local world = game:GetService("World")
local worldUnit = world:CreateUnit("WorldUnit", {})
if worldUnit == nil then return end
-- renderMeshId 取项目为该单位配置的网格资源,不要凭空编造资源 ID
local renderMeshId = worldUnit.RenderMeshId
worldUnit:SetRenderMeshWithDefaultSkin(renderMeshId)
worldUnit:Destroy()SetSlotSkinId
签名:SetSlotSkinId(slotIndex: SkinSlotIndex, skinId: String) -> void
为 WorldUnit 的指定皮肤槽位单独设置皮肤。调用时第一个参数使用 Enums.SkinSlotIndex 枚举指定槽位(ALL、SLOT_1、SLOT_2、SLOT_3、SLOT_4),第二个参数传入皮肤资源标识字符串,用于替换该槽位当前使用的皮肤。
| 参数 | 类型 | 说明 |
|---|---|---|
slotIndex | SkinSlotIndex | 槽位索引(SkinSlotIndex.SLOT_1~SLOT_4,对应 0~3;ALL = 全部槽位) |
skinId | String | 皮肤资源ID |
返回值 void
示例代码
为 WorldUnit 槽位设置皮肤资源
lua
-- @runtime client
local world = game:GetService("World")
local unit = world:CreateUnit("WorldUnit", {})
if unit == nil then return end
unit:SetSlotSkinId(Enums.SkinSlotIndex.SLOT_1, "official://skin/10001")
local ids = unit:GetAllSlotSkinIds()
print("槽位皮肤列表:", ids)GetAllSlotSkinIds
签名:GetAllSlotSkinIds() -> Array<String>
获取该 WorldUnit 全部槽位当前使用的皮肤标识列表,返回一个字符串数组,数组元素依次对应各槽位上的皮肤资源 ID。
返回值 Array<String>
示例代码
调用 WorldUnit:GetAllSlotSkinIds 的示例。
lua
-- @runtime client
local worldUnit = game:CreateUnit("WorldUnit", {})
local result = worldUnit:GetAllSlotSkinIds() -- 返回 Array
if result ~= nil then
for index, item in ipairs(result) do
print(index, item)
end
endSetSlotModelColor
签名:SetSlotModelColor(slotIndex: SkinSlotIndex, colorIndex: ModelColorIndex, color: Color3) -> void
SetSlotModelColor 是 WorldUnit 的成员函数,用于为指定皮肤槽位上的指定颜色通道设置染色颜色。调用时需要依次传入槽位索引、颜色通道索引以及颜色值三个参数,其中槽位索引由 Enums.SkinSlotIndex 提供,颜色通道索引由 Enums.ModelColorIndex 提供,颜色值为 Color3 类型。该函数没有返回值。
| 参数 | 类型 | 说明 |
|---|---|---|
slotIndex | SkinSlotIndex | 槽位索引(SkinSlotIndex.SLOT_1~SLOT_4,对应 0~3;ALL = 全部槽位) |
colorIndex | ModelColorIndex | 染色区索引(ModelColorIndex.MODEL_COLOR_1~MODEL_COLOR_4,对应 1~4) |
color | Color3 | 染色颜色 |
返回值 void
示例代码
为 WorldUnit 指定槽位设置模型颜色
lua
-- @runtime client
local world = game:GetService("World")
local unit = world:CreateUnit("WorldUnit", {})
if unit == nil then return end
-- Color 使用 0-255 RGBA 四参数
unit:SetSlotModelColor(Enums.SkinSlotIndex.SLOT_1, Enums.ModelColorIndex.MODEL_COLOR_1, Color.New(255, 0, 0, 255))
print("槽位颜色已设置")