Graphics Reference
In-Depth Information
referring to the scene with Application.levelName. This index number ties up to how
scenes are organized in the Build Settings (File → Build Settings), meaning that moving
things around in build settings can affect how your game works if you choose to use index
numbers to refer to specific scenes later on. To avoid any confusion, I prefer to stick to
names whenever I deal with level loading.
The final part of the BaseGameController deals with the mechanics of pausing and
unpausing a game:
public bool Paused
{
get
{
// get paused
return paused;
}
set
{
// set paused
paused = value;
if (paused)
{
// pause time
Time.timeScale= 0f;
} else {
// unpause Unity
Time.timeScale = 1f;
}
}
}
A Boolean variable named paused holds the game pause state internally. Rather
than provide direct access to this, a getter-setter function is used so that we can act when-
ever the state of paused changes without having to monitor it.
The Get part simply returns the Boolean variable paused.
The Set part sets the Boolean variable paused and goes on to set Time.timeScale to
either 0 (paused) or 1 (unpaused). Setting Time.timeScale affects how time passes. For
example, setting timescale to 0.5 would mean that time passes 2× slower than real time.
The Unity documentation states:
Except for realtimeSinceStartup, timeScale affects all the time and delta time measuring
variables of the Time class.
If you lower timeScale it is recommended to also lower Time.fixedDeltaTime by the
same amount.
FixedUpdate functions will not be called when timeScale is set to zero.
A regular videogame would display some sort of message to tell players whenever
the game is paused, but at this stage, the script is purely providing the mechanics of
pausing, and we will save concerning ourselves too much with user interface until
Chapter 14.
Search WWH ::




Custom Search