Loading scenes additively and asynchronously

Scenes can be loaded either to replace the current scene or can be loaded additively to add its contents to the current scene without unloading the preceding one. This can be toggled via the LoadSceneMode argument of the SceneManager.LoadScene() family of functions. 

Another mode of scene loading is to complete it either synchronously or asynchronously, and there are good reasons to use both. Synchronous loading is the typical means of loading a scene by calling SceneManager.LoadScene(), where the main thread will block until the given scene completes loading. This normally results in poor user experience, as the game appears to freeze as the contents are loaded in (whether as a replacement or additively). This is best used if we want to get the player into the action as soon as possible, or we have no time to wait for scene objects to appear. This would normally be used if we're loading into the first level of the game or returning to the main menu.

For future scene loading, however, we may wish to reduce the performance impact so that we can continue to keep the player in action. Loading a scene can take a lot of work, and the larger the scene, the longer it will take. However, the option of asynchronous additive loading offers a huge benefit: we can let the scene gradually load in the background without causing a significant impact on the user experience. This can be accomplished with SceneManager.LoadSceneAsync() combined with passing in LoadSceneMode.Additive for the loading mode argument.

It's important to realize that scenes do not strictly follow the concept of a game level. In most games, players are normally trapped in one level at a time, but Unity can support multiple scenes being loaded simultaneously through additive loading, allowing each scene to represent a small chunk of a level. Ergo, we could initialize the first scene for the level (Scene-1-1a), and as the player nears the next section, asynchronously and additively load in the next (Scene-1-1b), and repeat this continuously as the player travels through the level.

Exploiting this feature would require a system that either constantly checks the player's position in the level until they get close or uses trigger volumes to broadcast a message that the player is nearing the next section and begin asynchronous loading at the appropriate time. Another important consideration is that the scene's contents won't appear immediately since asynchronous loading effectively spreads the loading out over a handful of frames to cause as little visible impact as possible. We need to make sure that we trigger asynchronous scene loading with more than enough time to spare so that the player won't see objects popping into the game.

Scenes can also be unloaded to clear them out of memory. This will save some memory or runtime performance in the form of removing any components making use of Update() that we no longer need. Again, this can be done both synchronously and asynchronously with SceneManager.UnloadScene() and SceneManager.UnloadSceneAsync(). This can be an enormous performance benefit because we're only using what we need due to the player's location in the level, but note that it is not possible to unload small chunks of a monolithic scene. If the original scene file was enormous, then unloading it would unload everything. The original scene would have to be broken up into smaller scenes and then loaded and unloaded as needed. Similarly, we should only begin to unload a scene if we're certain the player can no longer see its constituent objects; otherwise, they would witness objects disappearing out of nowhere. One last consideration is that scene unloading would trigger the destruction of many objects, which is likely to free up a lot of memory and trigger the garbage collector. The efficient use of memory is also important when making use of this tip.

This approach would require a significant amount of scene redesign work, scriptwriting, testing, and debugging, which is not to be underestimated, but the benefits of improving user experience are exceptional. Having seamless transitions between areas in a game is a benefit that is often praised by players and critics because it doesn't take the player out of the action. If we use it appropriately, it can save a significant amount of runtime performance, improving the user experience further.

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

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