Skip to content

WaterUnit ​

概览 ​

字段值
KindUnit · 单位 Unit
Realmcommon

继承关系 ​

  • Unit(5 属性 / 28 函数 / 6 事件)
    • SpaceUnit(1 属性 / 6 函数)
      • BasePart(24 属性 / 9 函数)
        • TriggerUnit(8 属性 / 7 函数 / 4 事件)
          • [WaterUnit](20 属性 / 5 函数)

继承成员 ​

4 个来源 / 23 属性 / 50 函数 / 10 事件

深水单位用于在场景中划定一片深水区域,进入其中的物体将受到浮力与阻尼等水体物理影响。它通过 WaterDensityMode、WaterDensity、WaterDensityRatio 调节密度表现,用 WaterVelocity 与 WaterVelocityInLocalSpace 描述水流方向和是否跟随局部坐标系。外观侧提供 ModelAlpha、ModelVisible、CastShadow、ModelColor1 至 ModelColor4 以及一套皮肤槽位接口,可分别控制可见度、染色与贴图细节。

适用场景 ​

常用于在关卡里放置河流、湖泊或泳池等水域,让角色与物体进入后自然漂浮并随水流漂移;也用于需要按队伍或阶段切换水面外观与水花颜色的场景。

使用要点 ​

先通过 game:CreateUnit("WaterUnit", config) 或 World:CreateUnit("WaterUnit", config) 创建实例,随后写入 WaterDensityMode、WaterDensity、WaterDensityRatio 与 WaterVelocity 等水体参数;外观可用 SetRenderMeshWithDefaultSkin 指定模型,再用 SetTexture 更换贴图,或用 SetSlotSkinId、SetSlotModelColor 分别调整槽位皮肤与染色,并通过 GetAllSlotSkinIds 读取当前槽位皮肤列表做校验。

注意事项 ​

WaterUnit 是深水区域单位,可通过 game:CreateUnit 或 World:CreateUnit 创建。水体参数(WaterDensity、WaterDensityRatio、WaterVelocity 等)作用于进入区域的对象;外观相关的 SkinId、SkinSlots 与渲染函数需要与 ModelColor 系列属性配合使用。

代码示例 ​

创建 WaterUnit 实例 ​

lua
-- @runtime client
local World = game:GetService('World')
local waterUnit = World:CreateUnit('WaterUnit', {})
if waterUnit == nil then return end
print('WaterUnit 已创建:', waterUnit.Name)

属性 (20) ​

Name类型默认值说明
WaterDensityModeInt0 (DensityTypeDirect)WaterUnit 的密度类型属性,类型为 Enums.Int,用于指定水面密度的取值方式。
WaterDensityFloat6.0WaterUnit 的密度值属性,类型为 Float,用于描述水面的密度数值。
WaterDensityRatioFloat2WaterUnit 的密度比属性,类型为 Float,用于描述水面密度比例。
WaterVelocityInLocalSpaceBoolfalseWaterUnit 的水流速度跟随局部坐标系属性,类型为 Bool,用于决定水流速度是否按局部坐标系解释。
WaterVelocityVector3[0, 0, 0]WaterUnit 的水流速度属性,类型为 Vector3,用于描述水面的水流速度向量。
WaterSplashColorColor3Color3.New(144, 212, 255)WaterSplashColor 是 WaterUnit 的水花颜色属性,类型为 Color3,可公开读写,用于描述水体水花的染色颜色。
RenderMeshIdStringofficial://mesh/250173WaterUnit 的模型资源 ID 属性,类型为 String,用于指定水面显示所使用的模型资源。
PhysicsMeshIdStringofficial://mesh/250173WaterUnit 的物理资源 ID 属性,类型为 String,用于指定水面使用的物理网格资源。
ModelAlphaFloat0.85WaterUnit 的模型透明度属性,类型为 Float,用于描述水面模型在渲染时的整体透明程度。
ModelVisibleBooltrueWaterUnit 的模型可见性属性,类型为 Bool,用于控制水面模型是否显示。
CastShadowBoolfalseWaterUnit 的启用阴影投射属性,类型为 Bool,用于控制水面模型是否投射阴影。
ModelColor1Color3Color3.New(255, 255, 255)ModelColor1 是 WaterUnit 的第 1 号染色区域颜色,类型为 Color3,可公开读写,默认值为白色。
ModelColor2Color3Color3.New(255, 255, 255)ModelColor2 是 WaterUnit 的第 2 号染色区域颜色,类型为 Color3,可公开读写,默认值为白色。
ModelColor3Color3Color3.New(255, 255, 255)ModelColor3 是 WaterUnit 的第 3 号染色区域颜色,类型为 Color3,可公开读写,默认值为白色。
ModelColor4Color3Color3.New(255, 255, 255)ModelColor4 是 WaterUnit 的第 4 号染色区域颜色,类型为 Color3,可公开读写,默认值为白色。
SkinIdString""WaterUnit 的皮肤属性,类型为 String,用于指定水面模型使用的皮肤资源标识。
UseSkinListBoolfalseWaterUnit 的多槽位皮肤开关属性,类型为 Bool,用于决定是否启用多槽位皮肤方案。
SkinSlotsArray<SkinSlot>[]SkinSlots 是 WaterUnit 的皮肤槽位列表属性,类型为数组,可公开读写,默认值为空列表。
UseCustomAppearanceBoolfalseWaterUnit 的使用自定义外观属性,类型为 Bool,用于决定是否启用自定义外观。
CustomAppearanceIdString""WaterUnit 的自定义外观 ID 属性,类型为 String,用于指定所使用的自定义外观资源标识。

关联类型 ​

函数 (5) ​

SetTexture ​

签名:SetTexture(textureAssetId: String, texIndex?: TextureChannel, slotIndex?: SkinSlotIndex) -> void

为 WaterUnit 设置贴图资源,可指定贴图通道与皮肤槽位。

参数类型说明
textureAssetIdString贴图资源ID,传空字符串清除该通道覆盖
texIndex?TextureChannel贴图通道(TextureChannel.MAIN=主贴图 / NORMAL=法线 / MRE),缺省 TextureChannel.MAIN
slotIndex?SkinSlotIndex皮肤槽位(SkinSlotIndex.SLOT_1~4);缺省或 SkinSlotIndex.ALL 时:多槽位模式作用于全部槽位、单皮肤模式作用于当前皮肤

返回值 void

示例代码

调用 WaterUnit:SetTexture 的示例。

lua
-- @runtime client
local waterUnit = game:CreateUnit("WaterUnit", {})
local textureAssetId = "示例文本"  -- String
local texIndex = Enums.TextureChannel.MAIN  -- TextureChannel
local slotIndex = Enums.SkinSlotIndex.ALL  -- SkinSlotIndex
waterUnit:SetTexture(textureAssetId, texIndex, slotIndex)
print("已调用 SetTexture", textureAssetId)

SetRenderMeshWithDefaultSkin ​

签名:SetRenderMeshWithDefaultSkin(renderMeshId: String) -> void

切换 WaterUnit 的显示模型,并为该模型应用默认皮肤。

参数类型说明
renderMeshIdString模型资源ID

返回值 void

示例代码

调用 WaterUnit:SetRenderMeshWithDefaultSkin 的示例。

lua
-- @runtime client
local waterUnit = game:CreateUnit("WaterUnit", {})
local renderMeshId = "default"  -- String
waterUnit:SetRenderMeshWithDefaultSkin(renderMeshId)
print("已调用 SetRenderMeshWithDefaultSkin", renderMeshId)

SetSlotSkinId ​

签名:SetSlotSkinId(slotIndex: SkinSlotIndex, skinId: String) -> void

为 WaterUnit 的指定皮肤槽位设置皮肤。

参数类型说明
slotIndexSkinSlotIndex槽位索引(SkinSlotIndex.SLOT_1~SLOT_4,对应 0~3;ALL = 全部槽位)
skinIdString皮肤资源ID

返回值 void

示例代码

调用 WaterUnit:SetSlotSkinId 的示例。

lua
-- @runtime client
local waterUnit = game:CreateUnit("WaterUnit", {})
local slotIndex = Enums.SkinSlotIndex.ALL  -- SkinSlotIndex
local skinId = "default"  -- String
waterUnit:SetSlotSkinId(slotIndex, skinId)
print("已调用 SetSlotSkinId", slotIndex)

GetAllSlotSkinIds ​

签名:GetAllSlotSkinIds() -> Array<String>

获取 WaterUnit 全部槽位的皮肤资源 ID 列表。

返回值 Array<String>

示例代码

调用 WaterUnit:GetAllSlotSkinIds 的示例。

lua
-- @runtime client
local waterUnit = game:CreateUnit("WaterUnit", {})
local result = waterUnit:GetAllSlotSkinIds()  -- 返回 Array
if result ~= nil then
    for index, item in ipairs(result) do
        print(index, item)
    end
end

SetSlotModelColor ​

签名:SetSlotModelColor(slotIndex: SkinSlotIndex, colorIndex: ModelColorIndex, color: Color3) -> void

为 WaterUnit 的指定皮肤槽位设置某一颜色通道的模型染色。调用时按顺序传入槽位索引 slotIndex、颜色通道 colorIndex 与目标颜色 color,三个参数均为必填。

参数类型说明
slotIndexSkinSlotIndex槽位索引(SkinSlotIndex.SLOT_1~SLOT_4,对应 0~3;ALL = 全部槽位)
colorIndexModelColorIndex染色区索引(ModelColorIndex.MODEL_COLOR_1~MODEL_COLOR_4,对应 1~4)
colorColor3染色颜色

返回值 void

示例代码

为水面槽位染色

lua
-- @runtime client
local world = game:GetService("World")
local waterUnit = world:CreateUnit("WaterUnit", {})
if waterUnit == nil then return end

-- slotIndex 取 SkinSlotIndex,colorIndex 取 ModelColorIndex,color 是 0~255 分量的 Color
waterUnit:SetSlotModelColor(Enums.SkinSlotIndex.SLOT_1, Enums.ModelColorIndex.MODEL_COLOR_1, Color.New(0, 128, 255, 255))