Game Development Reference
In-Depth Information
Using delete/load patterns
Having planned how to organize our GameObjects into globally persistent and level-
specific lifespans, we must further update GameMgr.cs to ensure that only the cur-
rent level is loaded at any time. To do this, perform the following steps:
1. In GameMgr.cs , inside the ChangeState() method, the first thing we do
is tell the game to delete any potential level-specific GameObject hierarchies
that may be loaded:
if (GameObject.Find("_level1")
Destroy (GameObject.Find ("_level1"));
if (GameObject.Find("_level2")
Destroy (GameObject.Find ("_level2"));
if (GameObject.Find("_level3")
Destroy (GameObject.Find ("_level3"));
2. Inside the switch statement that ChangeState() implements, we signal
a LoadLevelAdditive() call when changing to LEVEL1 , LEVEL2 , or
LEVEL3 . However, when switching to MAIN , we simply need to destroy the
_level1 , _level2 , and _level3 GameObjects since _global remains
persistent throughout.
3. Recall that each level-specific scene file must be constructed according to the
pattern _leveln (where n is 1 for LEVEL1 , 2 for LEVEL2 , and 3 for LEVEL3 ).
This is because while Unity does provide a function for loading a scene file
additively, it does not provide a way to unload a scene file once the objects
have been loaded. To accomplish this, we perform the following steps:
1. Ensure that we construct our levels with a single parent GameObject
at the root.
2. Name the root GameObject so that it follows a consistent pattern. We
use _level1 , _level2 , and _level3 for our scene files.
4. This permits us to implement an unload scene file functionality by simply des-
troying the root object. Doing this will destroy the object and all the objects
that are children of its hierarchy.
Search WWH ::




Custom Search