class in UnityEngine
/
Implemented in:UnityEngine.CoreModule
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
CloseThe FrameTimingManager provides frame-level CPU and GPU time measurements.
The FrameTimingManager collects only the most important frame stats with a much lower performance overhead compared to Unity Profiler and is designed for use in Players.
Use FrameTimingManager to:
Note: The FrameTimingManager is always active on Development Player builds. However, to use this feature in Release Player builds, go to Edit > Project Settings > Player and enable the Frame Timing Stats property. If you enable the feature in settings, it’ll remain active whether or not you need it at a specific point in time.
You can access all of FrameTimingManager data with GetLatestTimings API. Alternatively you can use ProfilerRecorder to retrive the information about individual metrics.
In both cases the results are returned with a fixed delay of four frames (no data for the current frame).
This is because GPU timing results aren’t immediately available at the end of each frame on most platforms, so the FrameTimingManager waits to get synchronized CPU and GPU data for the frame. However the delay doesn’t guarantee GPU data available for all frames, because the GPU may not have any available resources to return the results, or might fail to return them correctly. In that case the GPU timings are reported as zero.
using Unity.Profiling; using UnityEngine;
public class ExampleScript : MonoBehaviour { FrameTiming[] m_FrameTimings = new FrameTiming[10];
void Update() { // Instruct FrameTimingManager to collect and cache information FrameTimingManager.CaptureFrameTimings();
// Read cached information about N last frames (10 in this example) // The returned value tells how many frames are actually returned // Element 0 of the returned array contains the data for the last fully finished frame. var ret = FrameTimingManager.GetLatestTimings((uint)m_FrameTimings.Length, m_FrameTimings); if (ret > 0) { // Your code logic here } } }
When ProfilerRecorder is used the FrameTimingManager is enabled automatically including in Release builds. Unity only takes measurements when you attach a ProfilerRecorder
to the counter, giving you dynamic control over the feature and its overhead. This is the recommended method if you want to explicitly control collection of GPU timings.
using Unity.Profiling; using UnityEngine;
public class ExampleScript : MonoBehaviour { ProfilerRecorder mainThreadTimeRecorder;
void OnEnable() { // Create ProfilerRecorder and attach it to a counter mainThreadTimeRecorder = ProfilerRecorder.StartNew(ProfilerCategory.Internal, "CPU Main Thread Frame Time"); }
void OnDisable() { // Recorders must be explicitly disposed after use mainThreadTimeRecorder.Dispose(); }
void Update() { var frameTime = mainThreadTimeRecorder.LastValue; // Your code logic here } }
The Frame Timing Manager supports all platforms that are supported by Unity with the following exceptions:
Additional resources: Introduction to the Frame Timing Manager.
CaptureFrameTimings | This function triggers the FrameTimingManager to capture a snapshot of FrameTiming's data, that can then be accessed by the user. |
GetCpuTimerFrequency | This returns the frequency of CPU timer on the current platform. If the platform does not support returning this value it will return 0. |
GetGpuTimerFrequency | This returns the frequency of GPU timer on the current platform. Currently always 1000000000. |
GetLatestTimings | Allows the user to access the currently captured FrameTimings. |
GetVSyncsPerSecond | This returns the number of vsyncs per second on the current platform, used to interpret timing results. If the platform does not support returning this value it will return 0. |
IsFeatureEnabled | Check if frame timing statistics are enabled. |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.