官术网_书友最值得收藏!

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. It does not affect Components that perform tasks on the CPU, such as AI scripts, User Interface, and gameplay logic. We must control that 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. In addition, 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, the GameObject must have a renderable Component attached, such as a MeshRenderer or SkinnedMeshRenderer. We must ensure that the Components we want to receive the visibility callbacks from are also attached to the same GameObject 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 Play Mode testing, ensure that you turn the Scene window Camera away from everything or disable the Scene window entirely.

To enable/disable inpidual 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).

主站蜘蛛池模板: 濉溪县| 兰州市| 米泉市| 南宁市| 扶余县| 新蔡县| 山阴县| 驻马店市| 宁都县| 濮阳县| 博白县| 永福县| 四会市| 濮阳县| 正阳县| 霍邱县| 鹰潭市| 辉南县| 沾化县| 介休市| 马关县| 彭山县| 天等县| 霍州市| 吴旗县| 安新县| 洛浦县| 安陆市| 夏津县| 乌审旗| 会理县| 绥化市| 雅安市| 青田县| 龙陵县| 锡林浩特市| 泽州县| 固阳县| 思南县| 晋州市| 焦作市|