Skip to content

RenderUnit ​

概览 ​

字段值
KindUnit · 单位 Unit
Realmcommon

继承关系 ​

  • Unit(5 属性 / 28 函数 / 6 事件)
    • SpaceUnit(1 属性 / 6 函数)
      • BasePart(24 属性 / 9 函数)
        • [RenderUnit](17 属性 / 5 函数)

继承成员 ​

3 个来源 / 19 属性 / 43 函数 / 6 事件

RenderUnit 是可创建的 3D 渲染组件,用于在场景中显示模型,并控制整体透明度、可见性、阴影与四个染色区域。

适用场景 ​

用于创建装饰物件或独立外观模型;运行时通过 RenderMeshId、SkinId 与 ModelColor1~ModelColor4 配置单皮肤外观。

使用要点 ​

通过 World:CreateUnit("RenderUnit", config) 创建实例,在 config 中设置有效的 RenderMeshId;需要切换模型并恢复默认皮肤时调用 SetRenderMeshWithDefaultSkin。

注意事项 ​

创建后必须设置 RenderMeshId 指定模型资源才能在场景中显示。UseSkinList 与 SkinSlots 是公开的运行时属性;修改槽位列表后应整体赋回 SkinSlots。TweenService 对 RenderUnit 属性的支持有限,建议直接修改属性而非 Tween 驱动。Color 类型必须用 0-255 RGBA 四参数构造(如 Color.New(255, 0, 0, 255)),不能用 0-1 归一化值。

代码示例 ​

创建并显示渲染单位 ​

lua
-- @runtime client
local World = game:GetService("World")
local renderUnit = World:CreateUnit("RenderUnit", {
    Name = "SceneDecoration",
    RenderMeshId = "official://mesh/59268",
    Position = Vector3.New(0, 2, 0),
    ModelVisible = true,
    ModelAlpha = 1.0,
    ModelColor1 = Color.New(255, 255, 255, 255),
})
print("已创建渲染单位:", renderUnit.Name)

属性 (17) ​

Name类型默认值说明
RenderMeshIdStringofficial://mesh/59268String 类型属性,标题为“模型资源ID”,用于标识该渲染单元所使用的模型资源。
ModelAlphaFloat1ModelAlpha 表示 RenderUnit 模型的透明度,类型为 Float。该属性在 Meta 中公开且标记为可编辑,声明的默认值为 1。
LocalTransparencyModifierFloat1LocalTransparencyModifier 表示 RenderUnit 的本地透明度乘数,类型为 Float。该属性在 Meta 中标记为不可编辑(editable 为 false),声明的默认值为 1。
ModelVisibleBooltrueModelVisible 表示 RenderUnit 的模型是否可见,类型为 Bool。该属性可编辑,Meta 声明的默认值为 true。
CastShadowBooltrueBool 类型属性,标题为“启用阴影投射”,用于表示该渲染单元是否启用阴影投射这一开关状态。
TransparentRenderBiasInt0TransparentRenderBias 表示 RenderUnit 半透明渲染的偏置值,类型为 Int。该属性可编辑,Meta 声明的默认值为 0。
ModelBindParentBoolfalseModelBindParent 表示 RenderUnit 的模型是否随父节点绑定,类型为 Bool。该属性可编辑,Meta 声明的默认值为 false。
ModelColor1Color3Color3.New(255, 255, 255)染色区域1对应的颜色,类型为 Color3,用于控制 RenderUnit 模型第一号染色区域的外观颜色。
ModelColor2Color3Color3.New(255, 255, 255)染色区域2对应的颜色,类型为 Color3,用于控制 RenderUnit 模型第二号染色区域的外观颜色。
ModelColor3Color3Color3.New(255, 255, 255)染色区域3对应的颜色,类型为 Color3,用于控制 RenderUnit 模型第三号染色区域的外观颜色。
ModelColor4Color3Color3.New(255, 255, 255)染色区域4对应的颜色,类型为 Color3,用于控制 RenderUnit 模型第四号染色区域的外观颜色。
SkinIdString""String 类型属性,标题为“皮肤”,用于指定该渲染单元所使用的皮肤资源。
UseSkinListBoolfalse多槽位皮肤开关;启用后按各子网格的 SkinSlots 配置皮肤与染色。
SkinSlotsArray<SkinSlot>[]各子网格的皮肤与染色配置列表;元素包含 SkinId 和 ModelColor1~4。
UseCustomAppearanceBoolfalseUseCustomAppearance 表示 RenderUnit 是否使用自定义外观,类型为 Bool。该属性可编辑,Meta 声明的默认值为 false。
CustomAppearanceIdString""CustomAppearanceId 指定 RenderUnit 所使用的自定义外观标识,类型为 String。该属性可编辑,Meta 声明的默认值为空字符串。
OcclusionTypeInt3 (不处理)该单位遮挡玩家时采用的处理规则;Meta 将类型声明为 Int,默认值为 3(不处理)。

关联类型 ​

函数 (5) ​

SetTexture ​

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

为 RenderUnit 实例设置贴图资源,可在调用时一并指定贴图通道与皮肤槽位,用于更换该渲染单元所使用的贴图。

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

返回值 void

示例代码

为已有渲染单位覆盖贴图

lua
-- @runtime client
local function applyTexture(unitName, textureUri)
    local world = game:GetService("World")
    local unit = world:FindFirstChild(unitName, true)
    if unit == nil or not unit:IsA("RenderUnit") then return end
    unit:SetTexture(textureUri, Enums.TextureChannel.MAIN, Enums.SkinSlotIndex.ALL)
end
-- 调用方传入项目中真实存在且有权使用的图片 URI 与唯一单位名

SetRenderMeshWithDefaultSkin ​

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

RenderUnit 的成员函数,用于把当前渲染单元切换为指定的模型资源,并同时为该模型应用默认皮肤。函数接收一个必填的 String 参数 renderMeshId,Meta 契约中未声明返回值。

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

返回值 void

示例代码

切换为项目配置的模型并应用默认皮肤

lua
-- @runtime client
local world = game:GetService("World")
local renderUnit = world:CreateUnit("RenderUnit", {})
if renderUnit == nil then return end
-- renderMeshId 取项目为该单位配置的网格资源,不要凭空编造资源 ID
local renderMeshId = renderUnit.RenderMeshId
renderUnit:SetRenderMeshWithDefaultSkin(renderMeshId)
renderUnit:Destroy()

SetSlotSkinId ​

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

为渲染单元的指定槽位设置皮肤标识。

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

返回值 void

示例代码

为指定槽位设置皮肤资源

lua
-- @runtime client
local world = game:GetService("World")
local unit = world:CreateUnit("RenderUnit", { RenderMeshId = "official://mesh/59268" })
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>

获取该渲染单元所有槽位当前使用的皮肤标识,返回字符串数组。

返回值 Array<String>

示例代码

调用 RenderUnit:GetAllSlotSkinIds 的示例。

lua
-- @runtime client
local renderUnit = game:CreateUnit("RenderUnit", {})
local result = renderUnit: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

设置指定皮肤槽位中指定染色区域的颜色,用于对 RenderUnit 的单个槽位做精细染色。

参数类型说明
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 unit = world:CreateUnit("RenderUnit", { RenderMeshId = "official://mesh/59268" })
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("槽位颜色已设置")