Singleton Components

As mentioned previously, Static Classes have difficulty interfacing with Unity-related functionality, and cannot directly make use of MonoBehaviour features, such as event callbacks, Coroutines, hierarchical design, and Prefabs. Also, since there's no object to select in the Inspector window, we lose the ability to inspect the a Static Class' data at runtime through the Inspector window, which can make debugging difficult. These are features that we may wish to make use of in our global classes.

A common solution to this problem is to implement a Component that acts like a Singleton--it provides static methods to grant global access, and only one instance of the MonoBehaviour is ever allowed to exist at any given time. ...

Get Unity 2017 Game Optimization - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.