- Unity 2017 Game Optimization(Second Edition)
- Chris Dickinson
- 464字
- 2021-07-02 23:21:10
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).
- Getting started with Google Guava
- Java開發入行真功夫
- C/C++常用算法手冊(第3版)
- Unity 5.x By Example
- Learning Salesforce Einstein
- C#實踐教程(第2版)
- 微服務從小白到專家:Spring Cloud和Kubernetes實戰
- UNIX Linux程序設計教程
- Node.js開發指南
- C++從入門到精通(第6版)
- Instant Apache Camel Messaging System
- 計算機應用基礎(Windows 7+Office 2010)
- ASP.NET 4權威指南
- Mastering High Performance with Kotlin
- 匯編語言程序設計教程