When does Time.frameCount increase?
Unity3D offers many callbacks for each frame, the most commonly used ones Update
. There are also less used ones such as OnWillRenderObject
, OnRenderObject
and OnPostRender
. As for the order in which these callbacks are called when Time.frameCount
increments?
I am trying to synchronize the capture of frames that I captured after rendering completed with object properties recorded from other callbacks in this frame. I found that when called OnPostRender
, Time.frameCount
has already been zoomed in, so the frame I am notifying is actually the previous frame. For this reason, I would like to know what calls are called after the number of frames has been increased.
source to share
I wrote this script to commit on change Time.frameCount
:
using UnityEngine;
public class OnFrameCounterDetector : MonoBehaviour {
int frameNumber = 0;
string prevMethod;
private void Awake() {
Time.fixedDeltaTime = 1 / 60;
prevMethod = System.Reflection.MethodBase.GetCurrentMethod().Name;
CheckFrameCount(System.Reflection.MethodBase.GetCurrentMethod().Name);
}
private void OnEnable() {
CheckFrameCount(System.Reflection.MethodBase.GetCurrentMethod().Name);
}
private void Start () {
CheckFrameCount(System.Reflection.MethodBase.GetCurrentMethod().Name);
}
private void FixedUpdate() {
CheckFrameCount(System.Reflection.MethodBase.GetCurrentMethod().Name);
}
private void Update () {
CheckFrameCount(System.Reflection.MethodBase.GetCurrentMethod().Name);
}
private void LateUpdate() {
CheckFrameCount(System.Reflection.MethodBase.GetCurrentMethod().Name);
}
private void OnWillRenderObject() {
CheckFrameCount(System.Reflection.MethodBase.GetCurrentMethod().Name);
}
private void OnPreCull() {
CheckFrameCount(System.Reflection.MethodBase.GetCurrentMethod().Name);
}
private void OnBecameVisible() {
CheckFrameCount(System.Reflection.MethodBase.GetCurrentMethod().Name);
}
private void OnBecameInvisible() {
CheckFrameCount(System.Reflection.MethodBase.GetCurrentMethod().Name);
}
private void OnPreRender() {
CheckFrameCount(System.Reflection.MethodBase.GetCurrentMethod().Name);
}
private void OnRenderObject() {
CheckFrameCount(System.Reflection.MethodBase.GetCurrentMethod().Name);
}
private void OnPostRender() {
CheckFrameCount(System.Reflection.MethodBase.GetCurrentMethod().Name);
}
private void OnRenderImage(RenderTexture source, RenderTexture destination) {
CheckFrameCount(System.Reflection.MethodBase.GetCurrentMethod().Name);
}
private void OnDrawGizmos() {
CheckFrameCount(System.Reflection.MethodBase.GetCurrentMethod().Name);
}
private void OnGUI() {
CheckFrameCount(System.Reflection.MethodBase.GetCurrentMethod().Name);
}
private void OnApplicationPause(bool pause) {
CheckFrameCount(System.Reflection.MethodBase.GetCurrentMethod().Name);
}
private void OnDisable() {
CheckFrameCount(System.Reflection.MethodBase.GetCurrentMethod().Name);
}
private void CheckFrameCount(string callback) {
if (Time.frameCount > frameNumber) {
Debug.LogFormat("frameCount changed between {0} and {1}", prevMethod, callback);
frameNumber = Time.frameCount;
}
prevMethod = callback;
}
}
Inside the editor, I got a change between OnGUI
OR OnRenderObject
and FixedUpdate
, but the scene was almost empty, with only one camera and a game object with Sprite Renderer
on it.
The difference lay in the fact that if I had chosen a gaming facility or a camera in the hierarchy, if the GO was selected, then OnRenderObject
will be signaled if there is no (ie keep the camera in the hierarchy) OnGUI
.
You can use this script in more detailed scenes as most callbacks are not called at all in a simple scene, curious about the results.
By the way, I did not add coroutines with different outputs, they can be added, although it is very easy to catch more "moments" in the game loop, like between Update and LateUpdate, etc.
source to share
According to the Execution Order or MonoBehaviour messages , I think we could assume that after Time.frameCount
increasing, t20> represents the number of frames rendered :
-
OnPostRender
-
OnRenderImage
- Coroutines that caused
yield WaitForEndOfFrame
-
OnApplicationPause
-
OnDisable
(if script is disabled during frame)
source to share