The question is answered, OP unresponsive
How to disable objects that are outside viewport?
Hi,
I tried hard to figure out how to hide whole gameobjets with sprite renderer for performance reasons. Disabling only the sprite renderer does do nothing with performance. I need to disable the whole object that is not seen by the Main Camera and activate it again when seen.
My problem is that I have a cript using update method and check if the object is visible by
bool visible = GetComponent<Renderer>().isVisible;
but when the object is deactivated, Update is not called anymore so the object is never set active again when in the viewport...
The script has to be really simple. Sould not be using update because I have a 2D platformer and my scene is build up from hundreds of sprites. I need all sprites turn off when they are not visible.
What you are looking for would be Occlusion Culling.
Regarding frustum culling and general sprite performance:
Having hundreds of sprites should not be a problem. I created a scene with 2500 sprites and the render time is still way below 10 ms (it's around 5ms, or 200 fps).
I might add that of course you can do it yourself, but that would involve having one gameobject with a script that is constantly getting a reference of every active/non-active gameobject in the scene and updating that if new gameobjects are created or older ones destroyed, and then for every one of them checking if they are really visible (not just rendered, but really visible for the camera) at the moment and if so, activate them.
I'm not sure if that is what you are looking for since Occlusion Culling is a feature that was "invented" many years ago for just that reason.
Then again, you can do it manually, but I don't recommend it since the functionality is already native to the engine and ready to use.
Hope you are happy with this, if not, feel free to describe what exactly you want to achieve.
If you read the description of one of the images in the occlusion culling page, it says:
Regular frustum culling only renders objects within the camera’s view. This is automatic and always happens.
Answer by 1MachoKualquiera · Dec 08, 2015 at 11:57 AM
Maybe you can estimate the coordinates that are outside the viewport, and then say
for (int i = 0; i < gameObject.getGameObjectsWithTag("disable?"; i++)
{
if (transform.position.x < camera.transform.position.x - [outside the camera])
{
//disable the object
}
if (transform.position.x > camera.transform.position.x + [outside the camera])
{
//disable the object
}
//... (the same with y axis)
}
This is just so you take it as an idea, obviously not real code :)