OnRenderObject is called more often than I expect.

I was doing some investigation by adding logging to OnWillRenderObject and OnRenderObject to understand when each is called. What I’m seeing is surprising me. I’m testing on GearVR, so I expected OnWillRenderObject and OnRenderObject to be called at most twice per frame, and potentially once if Unity is trying to eliminate duplicate work for each eye. Instead I’m seeing OnWillRenderObject called once per frame, but OnRenderObject called 6 times per frame. Strangely, two of the calls to OnRenderObject are occurring before OnWillRenderObject is called. I’d like to use OnRenderObject to draw some custom geometry, but not if it’s going to cause the custom geometry to be drawn multiple times per frame. Can someone explain what’s going on?

I would simply create the bool:

public bool HasRunThisFrame = false;

and inside “OnWillRenderObject” or “OnRenderObject” (What ever one you find more consistent) and do something like this:

public void OnRenderObject () {
     if (!HasRunThisFrame) {
          //TODO: Draw Objects to Screen
          
          HasRunThisFrame = true;
     }
}

public void Update () {
     HasRunThisFrame = false;
}

That way, “OnRenderObject” will only run once per frame so you aren’t wasting CPU power on drawing stuff needlessly to the screen.

Good luck - Fydar

Turns out the additional calls were because of the Cardboard SDK – we support both Gear and Cardboard, and it turns out the Cardboard SDK creates a PreRender and PostRender camera even when the Cardboard SDK is disabled. It appears that the extra cameras don’t actually do anything when the Cardboard SDK is disabled, but the presence of the cameras does cause extra calls to OnRenderObject.