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

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).

主站蜘蛛池模板: 额尔古纳市| 栖霞市| 南皮县| 凭祥市| 怀远县| 沛县| 阜宁县| 铜山县| 南丹县| 沙湾县| 海城市| 芮城县| 双江| 龙陵县| 甘肃省| 伊宁县| 犍为县| 赤壁市| 容城县| 徐闻县| 根河市| 潜山县| 旺苍县| 朝阳市| 东台市| 盐城市| 饶平县| 五寨县| 九江市| 桂平市| 岑溪市| 叙永县| 鄯善县| 黔江区| 专栏| 始兴县| 宜兰市| 陈巴尔虎旗| 蕉岭县| 奉节县| 丹江口市|