How to properly use Renderer.OnBecameInvisible for Culling objects off screen?

Hi,

Found an excellent post about checking “Is target in view frustrum”. If i understand it correctly, we would stop rendering of an object by disabling its Renderer when off screen. But as soon as i do this …

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void OnBecameInvisible() {
        GetComponent<MeshRenderer>().enabled = false;
    }
    void OnBecameVisible() {
        GetComponent<MeshRenderer>().enabled = true;
    }
}

… and the object leaves camera frustrum, Its Renderer does not dispatch OnBecameVisible again, once it later again enters the camera frustrum - well, thats obvious, since we disabled it in the first place - so a disabled Renderer would not fire OnBecameVisible.

What gives? Using this culling technique i cant do the culling.

Further more i wanted to actually disable the whole GameObject (not only its Renderer) when it leaves frustrum. Any thoughts on the best-practise implementation-vise here? I guess you cant use Renderer’s isVisible/OnBecameVisible/OnBecameInvisible? Is using WorldToScreenPoint favored approach in this case? Is there better approach than disabling whole GameObject performance-vise?

Brainstorm please, tested methods preferred!

As @HarshadK said, the object’s renderer is automatically culled as long as no part of it is visible on any camera.

It makes sense to “cull” (turn off) expensive components on your GameObject when they leave the view. If you have a NPC with a patrolling script and a navmesh agent, you can turn off the script and agent when they leave view. Projectors also make sense to turn off - as long as they’re projecting onto a mesh that’s visible (like a big ground mesh), they will cause extra draw calls even though the projector itself isn’t visible.

As with everything that’s performance related, optimizing before you know what’s causing bad performance is the stupidest thing you can do. If your game’s running slowly, open the profiler and check what’s slow. Making a blind guess and doing a bunch of manual culling of objects is probably not going to help, and cost a lot of time and effort.

You don’t have to disable the Renderer when gameobject is not shown on the screen as it will not be rendered anyway because of frustrum culling.

We can provide you with method to use based on what exactly you want to achieve here.