Game Development Reference
In-Depth Information
Utilizing the Prefabs
It is quite typical to put an asset into the scene long enough to get it set up and functioning correctly,
and then create a prefab from it and delete it from the scene. Many objects, such as projectiles, are
created and destroyed on demand throughout the game. Others are reused in different levels. Either
way, when using prefabs, you only have to set them up once.
If you have made changes to any of the scene objects since you created their prefabs, you can
“apply” the changes made to the object to the prefabs or revert the objects back to the prefab's
configuration. Once you make changes to a prefab in the Project view, all instances of the object,
regardless of scene, will reflect the changes. Let's test the functionality on the bench. Most garden
benches do not drive themselves around people's gardens.
1.
Open the GardenSetUp scene again by double-clicking on it in the
Scenes folder.
2.
Select the StoneGardenBench in the Hierarchy view, and activate it.
3.
From the GameObject menu, select Break Prefab Instance.
4.
Save the scene.
The lettering is no longer blue (indicating an instance) in the Hierarchy view. The bench will now
retain its functionality in this scene.
1.
Open the GardenLevel1 scene.
The bench's prefab does not reside in the Prefabs folder with the rest of the imported objects,
so you may have trouble locating it.
2.
Select the StoneGardenBench in the Hierarchy view and, from the right-click
menu, choose Select Prefab.
The prefab is temporarily highlighted yellow in the Project view.
3.
Select the StoneGardenBench prefab in the Project view, and inspect its
components.
4.
Select the StoneGardenBench in the Hierarchy view, and note the differences.
5.
In the Inspector, near the top, press Revert on the Prefab line.
The bench in the Hierarchy view loses the scripts and Rigidbody components that caused it to turn
and head out of the garden. It also becomes active in the scene again.
6.
Deactivate the bench in the scene.
7.
In the Inspector, click Apply at the far right of the Prefab line to update the
prefab in the Project view to match the one in the Hierarchy view.
8.
Select the StoneGardenBench in the Project view, and note that it is
now inactive.
9.
Reactivate the prefab in the Project view.
The instance of it in the scene is also activated.
 
Search WWH ::




Custom Search