Advanced Concepts
Explore advanced scripting concepts in Unity to enhance your VR game development skills. Learn to use coroutines for smooth multi-frame tasks, manage scene persistence to maintain game state across levels, and implement session persistence to save player progress between game sessions.
We'll cover the following...
Unity provides tools and workflows tailored to game development, such as a visual editor for creating GameObjects and scenes, physics simulations, and support for various platforms. Additionally, Unity uses a component-based system for GameObject behavior. Unlike traditional inheritance-based systems used in other frameworks, each GameObject can have multiple components that define its behavior. Now, we’ll cover some other ideas that will aid us in developing a holistic game.
Multi-frame tasks
The lifespan of an event handler begins with the event’s invocation and ends when the method returns. Therefore, the lifespan of the handler is bound by the next frame to be rendered. However, some tasks demand that the execution of the method should persist across the frame.
Let’s say that we have a method that iteratively increases the size of a GameObject:
If we execute this function in the Start method, we’ll notice that the GameObject size just increases from 1 to 10 in an instant:
As a work-around, we could leverage the Update method and simply enlarge the GameObject on a frame-by-frame basis:
However, a much cleaner way to achieve this is to use a coroutine. ...