You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity中使用Profiler追踪Start()函数无结果,求解决方法

解决Unity中Start()函数Profiler采样无法追踪的问题

问题原因

Start()方法在游戏启动后的第一帧更新前执行,若你是进入Play模式后才启动Profiler录制,会错过这个阶段的采样数据,导致找不到"Start Example"项。

解决步骤

  • 提前启动录制:打开Profiler窗口(Window > Analysis > Profiler),点击红色录制按钮后,再进入Play模式。这样Profiler会捕获从游戏初始化开始的所有调用。
  • 定位采样位置:录制完成后,拖动时间轴到最左侧(游戏启动初始阶段),在Initialization.ScriptRunBehaviorStart节点下就能找到你标记的"Start Example"采样项。

可选简化方案

如果你不想手动调用BeginSample和EndSample,Unity 2020.2及以上版本支持[ProfileMethod]特性,直接标记方法即可自动追踪:

using UnityEngine;
using UnityEngine.Profiling;

public class Example : MonoBehaviour
{
    BoxParameter bParameter;

    [ProfileMethod]
    void Start()
    {
        bParameter = GameObject.Find("Main Camera").GetComponent<ZombieInventory>().box;
        Debug.Log(bParameter.width);
    }

    void Update()
    {
        Profiler.BeginSample("Update Example");
        bParameter = GameObject.Find("Main Camera").GetComponent<ZombieInventory>().box;
        Debug.Log(bParameter.width);
        Profiler.EndSample();
    }
}

内容的提问来源于stack exchange,提问作者user20708378

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 10:45:41