您可以使用 ProfilerRecorder API 读取 FrameTimingManager 的值,而不是使用 FrameTimingManager C# API。这样做的好处是,当您使用 ProfilerRecorder API 时,FrameTimingManager 仅在您将记录器附加到特定计数器时才记录值。这种行为使您可以控制哪些计数器收集数据,从而减少 FrameTimingManager 对性能的影响。
以下示例演示了如何使用 ProfilerRecordAPI 跟踪仅 CPU 主线程帧时间变量。
using Unity.Profiling;
using UnityEngine;
public class ExampleScript : MonoBehaviour
{
    string statsText;
    ProfilerRecorder mainThreadTimeRecorder;
    void OnEnable()
    {
        mainThreadTimeRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Internal, "CPU Main Thread Frame Time");
    }
    void OnDisable()
    {
        mainThreadTimeRecorder.Dispose();
    }
    void Update()
    {
        var frameTime = mainThreadTimeRecorder.LastValue;
        // Your code logic here
    }
}