Disabling objects by visibility

Sometimes, we may want components or GameObjects to be disabled when they're not visible. Unity comes with built-in rendering features to avoid rendering objects that are not visible to the player's camera view (through a technique known as Frustum Culling, which is an automatic process) and to avoid rendering objects that are hidden behind other objects (Occlusion Culling, which will be discussed in Chapter 6, Dynamic Graphics), but these are only rendering optimizations. Frustum and Occlusion Culling do not affect components that perform tasks on the CPU, such as AI scripts, user interface, and gameplay logic. We must control their behavior ourselves.

A good solution to this problem is using the OnBecameVisible() and OnBecameInvisible() callbacks. As the names imply, these callback methods are invoked when a renderable object has become visible or invisible with respect to any cameras in our scene. Also, when there are multiple cameras in a scene (for example, a local multiplayer game), the callbacks are only invoked if the object becomes visible to any one camera and becomes invisible to all cameras. This means that the aforementioned callbacks will be called at exactly the right times we expect; if nobody can see it, OnBecameInvisible() gets called, and if at least one player can see it, OnBecameVisible() gets called.

Since the visibility callbacks must communicate with the rendering pipeline, GameObject must have a renderable component attached, such as MeshRenderer or SkinnedMeshRenderer. We must ensure that the components we want to receive the visibility callbacks from are also attached to the same GameObject instance as the renderable object and are not a parent or child GameObject; otherwise, they won't be invoked.

Note that Unity also counts the hidden camera of the Scene window toward the OnBecameVisible() and OnBecameInvisible() callbacks. If we find that these methods are not being invoked properly during Playmode testing, ensure that you turn the Scene window camera away from everything or disable the Scene window entirely.

To enable/disable individual components with the visibility callbacks, we can add the following methods:

void OnBecameVisible() { enabled = true; }
void OnBecameInvisible() { enabled = false; }

Also, to enable/disable the entire GameObject the component is attached to, we can implement the methods this way instead:

void OnBecameVisible() { gameObject.SetActive(true); }
void OnBecameInvisible() { gameObject.SetActive(false); }

Although, be warned that disabling the GameObject containing the renderable object, or one of its parents, will make it impossible for OnBecameVisible() to be called since there's now no graphical representation for the camera to see and trigger the callback with. We should place the component on a child GameObject, and have the script disable that instead, leaving the renderable object always visible (or find another way to re-enable it later).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.48.135