Skip to content

RenderTriggerUnit ​

概览 ​

字段值
KindUnit · 单位 Unit
Realmcommon

继承关系 ​

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

继承成员 ​

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

RenderTriggerUnit 是兼具运行时模型渲染与触发检测能力的场景单位;RenderMeshId 指定实际渲染网格,DisplayModelId 只用于编辑器预览。

适用场景 ​

用于需要同时展示模型和检测单位进入、离开的场景区域。可通过 RenderMeshId 指定可见模型,通过物理与碰撞属性设置触发范围,并监听 OnTriggerEnter、OnTriggerExit 处理交互。

使用要点 ​

通过 World:CreateUnit 创建实例,在 config 中把 RenderMeshId 设置为项目提供的有效网格资源 URI,并监听 OnTriggerEnter/OnTriggerExit 处理进入和离开。

注意事项 ​

运行时外观使用 RenderMeshId,DisplayModelId 是 editOnly 的编辑器预览字段,不影响运行时渲染。作者可见脚本可用 SkinId 与 ModelColor1~4 配置单皮肤外观,也可用公开的 UseSkinList 与 SkinSlots 配置多槽位外观;Color 使用 0-255 RGBA 四参数构造。

代码示例 ​

创建可见触发区域并监听进入事件 ​

lua
-- @runtime client
local world = game:GetService("World")
-- 不指定 RenderMeshId 时使用 Meta 声明的默认模型,避免硬编码资源 ID
local renderTrigger = world:CreateUnit("RenderTriggerUnit", {})
if renderTrigger == nil then return end
renderTrigger.ModelColor1 = Color.New(255, 0, 0, 255)
renderTrigger.ModelVisible = true
renderTrigger.OnTriggerEnter:Once(function(otherUnit)
    print("进入触发区域:", otherUnit.Name)
    renderTrigger:Destroy()  -- 完成一次交互后释放自有区域
end)
-- 若未发生进入事件,请在业务结束时调用 renderTrigger:Destroy()

属性 (18) ​

Name类型默认值说明
RenderMeshIdStringofficial://mesh/59268RenderMeshId 是 RenderTriggerUnit 的模型资源 ID 属性,类型为字符串,用于指定该渲染触发单元当前显示的模型资源。
ModelAlphaFloat1控制模型的整体透明度,0 为完全透明,1 为完全不透明
LocalTransparencyModifierFloat1该单元的本地透明度乘数,用于在本地客户端进一步调整模型透明度。
ModelVisibleBooltrue控制模型是否可见,隐藏后仍参与物理碰撞
CastShadowBooltrueCastShadow 是 RenderTriggerUnit 的布尔属性,用于控制该单元是否投射阴影。
ModelBindParentBoolfalse启用后,当父节点为 WorldUnit、RenderUnit、TriggerUnit、PhysicsUnit 等支持父子带动的场景单位时,当前模型渲染表现将跟随父节点的变换自动同步。运行时可通过脚本设置 .ModelBindParent = true/false 动态切换该行为。
PhysicsMeshIdStringofficial://mesh/59268物理资源 ID,字符串类型,属性面板标题为“物理资源ID”。该属性用于为 RenderTriggerUnit 指定物理资源,属于可编辑的公开属性。
DisplayModelIdString""用于在编辑器中预览的模型资源 ID,不影响运行时实际渲染的模型。
ModelColor1Color3Color3.New(255, 255, 255)ModelColor1 是 RenderTriggerUnit 的 Color3 类型属性,表示该单位渲染模型第 1 个染色区域的颜色。
ModelColor2Color3Color3.New(255, 255, 255)ModelColor2 是 RenderTriggerUnit 的 Color3 类型属性,表示该单位渲染模型第 2 个染色区域的颜色。
ModelColor3Color3Color3.New(255, 255, 255)ModelColor3 是 RenderTriggerUnit 的 Color3 类型属性,表示该单位渲染模型第 3 个染色区域的颜色。
ModelColor4Color3Color3.New(255, 255, 255)ModelColor4 是 RenderTriggerUnit 的 Color3 类型属性,表示该单位渲染模型第 4 个染色区域的颜色。
SkinIdString""SkinId 是 RenderTriggerUnit 的皮肤属性,类型为字符串,用于指定该单元使用的皮肤资源。
UseSkinListBoolfalse多槽位皮肤开关;启用后按各子网格的 SkinSlots 配置皮肤与染色。
SkinSlotsArray<SkinSlot>[]各子网格的皮肤与染色配置列表;元素包含 SkinId 和 ModelColor1~4。
UseCustomAppearanceBoolfalse是否启用自定义外观。
CustomAppearanceIdString""自定义外观 ID,字符串类型,属性面板标题为“自定义外观ID”。该属性用于为 RenderTriggerUnit 指定自定义外观资源,属于可编辑的公开属性。
OcclusionTypeInt3 (不处理)OcclusionType 属性用于设置该渲染触发单位在遮挡玩家视角时所采用的规则,影响其被相机遮挡时的表现方式。该属性为整数类型,具体取值对应不同的遮挡处理规则。

关联类型 ​

函数 (5) ​

SetTexture ​

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

以当前已有皮肤为基础替换指定通道的贴图。自定义外观开启或对象没有可替换的当前皮肤时,调用不会生效。单皮肤模式使用省略的 slotIndex 或 Enums.SkinSlotIndex.ALL;指定 SLOT_1 等具体槽位只适用于已配置有效槽位皮肤的多槽位模式。

⚙ 本例使用单皮肤;须关闭自定义外观和多槽位,准备有效的当前 SkinId。多槽位用法须另行启用 UseSkinList 并为目标 SkinSlots 项配置有效 SkinId。传空字符串可清除对应通道覆盖,全部清空后回退原皮肤;本例仅演示提交非空图片 URI。客户端调用仅本机生效,服务端调用同步全端。示例仅定义辅助函数,不自动查询或修改地图;调用方须显式提供自己有权修改的、名称唯一的已有实例名及真实可用且有权使用的图片 URI。FindFirstChild 返回首个同名对象,因此存在同名实例时不得按此例直接调用。字符串格式检查和非空 SkinId 不能证明资源存在、权限或视觉效果;SetTexture 无返回值,日志仅表示已提交调用,不表示贴图加载成功。函数不创建或销毁实例,生命周期由调用方管理。

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

返回值 void

示例代码

按调用方指定的唯一实例名提交贴图设置

lua
-- @runtime client
-- 仅定义辅助函数;调用方确认唯一实例名、修改权限和真实图片资源后显式调用。
local function setConfiguredTriggerTexture(unitName, textureUri)
    if type(unitName) ~= "string" or unitName == "" then return end
    if type(textureUri) ~= "string" or string.find(textureUri, "%s") then return end
    if not string.match(textureUri, "^official://image/.+$") and not string.match(textureUri, "^custom://.+$") then return end

    local world = game:GetService("World")
    local renderTrigger = world:FindFirstChild(unitName, true)
    if renderTrigger == nil then return end
    if not renderTrigger:IsA("RenderTriggerUnit") then return end
    if renderTrigger.UseCustomAppearance then return end
    if renderTrigger.UseSkinList then return end
    local skinId = renderTrigger.SkinId
    if skinId == nil or skinId == "" then return end

    renderTrigger:SetTexture(textureUri, Enums.TextureChannel.MAIN, Enums.SkinSlotIndex.ALL)
    print("已提交本机贴图设置请求;请在可见对象上核对实际效果")
end
-- 准备好输入后,显式调用 setConfiguredTriggerTexture(unitName, textureUri);本块不会自行调用。

SetRenderMeshWithDefaultSkin ​

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

SetRenderMeshWithDefaultSkin 是 RenderTriggerUnit 的成员函数,用于切换该单元使用的模型,并为新模型应用默认皮肤。参数 renderMeshId 为必填的字符串,表示目标模型资源 ID。

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

返回值 void

示例代码

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

lua
-- @runtime client
local world = game:GetService("World")
local renderTrigger = world:CreateUnit("RenderTriggerUnit", {})
if renderTrigger == nil then return end
-- 用单位自身的 RenderMeshId(Meta 默认值)演示,不硬编码资源 ID
renderTrigger:SetRenderMeshWithDefaultSkin(renderTrigger.RenderMeshId)
print("已切换模型:", renderTrigger.RenderMeshId)
renderTrigger:Destroy()

SetSlotSkinId ​

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

成员函数,为指定皮肤槽位设置皮肤资源标识。

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

返回值 void

示例代码

调用 RenderTriggerUnit:SetSlotSkinId 的示例。

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

GetAllSlotSkinIds ​

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

成员函数,返回该单元全部槽位当前所用皮肤标识组成的字符串数组。

返回值 Array<String>

示例代码

调用 RenderTriggerUnit:GetAllSlotSkinIds 的示例。

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

为 RenderTriggerUnit 指定皮肤槽位上的指定模型颜色通道设置颜色值。调用时需依次传入槽位索引、模型颜色通道索引与颜色值,用于替换该槽位的染色表现。

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

返回值 void

示例代码

调用 RenderTriggerUnit:SetSlotModelColor 的示例。

lua
-- @runtime client
local renderTriggerUnit = game:CreateUnit("RenderTriggerUnit", {})
local slotIndex = Enums.SkinSlotIndex.ALL  -- SkinSlotIndex
local colorIndex = Enums.ModelColorIndex.MODEL_COLOR_1  -- ModelColorIndex
local color = Enums.HumanMeshColor.COLOR1  -- Color3
renderTriggerUnit:SetSlotModelColor(slotIndex, colorIndex, color)
print("已调用 SetSlotModelColor", slotIndex)