MicroProfiler 使用指南

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

本使用指南展示了如何使用 MicroProfiler 找到游戏中存在的问题并确定根本原因。下载游戏,在 Studio 中 从文件打开,并按照说明进行操作。


确定问题

  1. 在 Studio 中打开游戏后,开始测试,使用 F5播放 按钮。

  2. 帧率感觉不错,但对这个规模和范围的游戏来说并不够流畅。按 CtrlShiftF5 (ShiftF5) 显示 性能摘要 覆盖层。

    性能摘要显示 45 FPS。

    注意,帧率低于每秒 60 帧 (FPS)。

  3. CtrlAltF6 (F6) 打开 MicroProfiler。

    打开 MicroProfiler 后的视图。

    注意帧时间是 一致 的——条形图大致相同高度——所以造成低帧率的原因是每一帧都在运行,而不是偶尔运行并导致帧时间波动。

  4. CtrlP (P) 暂停 MicroProfiler。使用键盘快捷键暂停会打开 详细模式

  5. 点击并拖动以平移图表。注意在工作线程中,某个特定任务似乎使用了比主线程更多的处理时间。悬停在 RunService.Stepped 上,注意渲染所需时间。

    详细模式,显示进程的长标签。
  6. 时间轴中的堆叠条形指示代码的层次结构,因此继续在更低层次上悬停,尝试识别问题。

    多个进程的悬停详细信息,高亮显示 LocalScript。 多个进程的悬停详细信息,高亮显示 Raycast。

    注意 LocalScript 标签,它指示脚本的名称,以及 Raycast 标签,它指示问题可能与射线检测有关。

创建故障排除标签

现在 MicroProfiler 提供了一个起点,您可以排除问题代码。

  1. 停止播放测试,并在资源管理器窗口中过滤 localscript 以找到文件。

    资源管理器窗口中过滤的视图。

    搜索 raycast 表明这段代码可能是导致游戏性能不佳的罪魁祸首:


    local RAYS_PER_SECOND = 1500
    local function onStepped()
    for _ = 1, RAYS_PER_SECOND do
    local startPosition = getRandomPosition()
    local endPosition = getRandomPosition()
    local direction = endPosition - startPosition
    Workspace:Raycast(
    startPosition,
    endPosition
    )
    end
    end
    RunService.Stepped:Connect(onStepped)

    明确来说,您可以看到代码每秒发射 1,500 条射线,方向是随机的。

  2. 为了验证这段代码确实导致了渲染延迟,用 debug.profilebegin()debug.profileend() 包裹函数内容:


    local function onStepped()
    debug.profilebegin("Raycast Spam")
    for _ = 1, RAYS_PER_SECOND do
    local startPosition = getRandomPosition()
    local endPosition = getRandomPosition()
    local direction = endPosition - startPosition
    Workspace:Raycast(
    startPosition,
    endPosition
    )
    end
    debug.profileend()
    end

确认并修复问题

  1. 开始测试游戏,并再次打开 MicroProfiler。

  2. 注意 MicroProfiler 现在显示自定义标签,表明这个函数确实是根本原因。

    MicroProfiler 详细视图,显示 'Raycast Spam'。
  3. 停止播放测试。

  4. 删除或注释掉 onStepped() 函数和 RunService.Stepped:Connect(onStepped) 连接。

  5. 再次开始测试游戏,再次检查 性能摘要

    性能摘要显示 60 FPS。

    注意帧率的巨大改善以及 MicroProfiler 图表中帧时间的相应下降。

©2026 Roblox Corporation、Roblox、Roblox 标志及 Powering Imagination 是我们在美国及其他国家或地区的注册与未注册商标。