Graphics Reference
In-Depth Information
This alternative version of the LoadLevel function shown below uses an index to get a
level name from the levelNames array. It then uses the original LoadLevel function declared
earlier to handle loading. Again, keeping the focus on using a single function for all load-
ings rather than duplicating the code or having loading happen in more than one place:
private void LoadLevel( int indexNum )
{
// load the game level
LoadLevel( levelNames[indexNum] );
}
The final function in the scene manager class is used to reset gameLevelNum when-
ever we start a new game. It is assumed that the script restarting the game will call this
function before calling GoNextLevel() again.
public void ResetGame()
{
// reset the level index counter
gameLevelNum = 0;
}
}
2.2.3 ExtendedCustomMonoBehavior.cs
Extending MonoBehavior is a useful way to avoid repeating common functions or variable
declarations. Many classes share functions and/or variables. For example, most of the scripts
in this topic use a variable named myTransform to refer to a cached version of their transforms
to save having to look up the transform every time we need to access it. As MonoBehavior
provides a host of system functions and calls, making a new class that extends, it can be a use-
ful tool to have in the kit. Whereas a new class would use MonoBehavior, we just derive from
the new ExtendedCustomMonoBehavior script instead.
For the scripts in this topic, the ExtendedCustomMonoBehavior.cs script extends
MonoBehavior to include
1. a myTransform variable to hold a cached reference to a transform
2. a myGO variable to hold a cached reference to a gameObject
3. a myBody variable for a cached reference to a rigidBody
4. a didInit Boolean variable to determine whether or not the script has been
initialized
5. an integer variable called id to hold an id number
6. a Vector3 type variable called tempVEC to use for temporary vector actions
7. a Transform variable called tempTR used for any temporary references to
transforms
8. a function called SetID(integer) so that other classes can set the variableĀ id
Search WWH ::




Custom Search