Prefabs
Prefabs
Prefabs
Prefabs
A prefab is a complex object that has been bundled up so that it can be re-created over and over
with little extra work. In this hour, you learn all about prefabs. You start by learning about prefabs
and what they do. From there, you learn how to create prefabs in Unity. You learn about the
concept of inheritance. You finish by learning how to add prefabs to your scene both through the
editor and through code.
Prefab Basics
As mentioned earlier, a prefab is a special type of asset that bundles up game objects. Unlike simply
nesting objects in the Hierarchy view, a prefab exists in the Project view and can be reused over and
over across many scenes. This enables you to build complex objects, like an enemy, and use it to
build an army. You can also create prefabs with code. This allows you to generate a nearly infinite
number of objects during runtime. The best part is that any game object, or collection of game
objects, can be put in a prefab. The possibilities are endless!
Prefab Terminology
Some terms that are important to know when working with prefabs. If you are familiar with the
concepts of object-oriented programming practices, you may notice some similarities:
Prefab: The prefab is the base object. This exists only in the Project view. Think of it as the
blueprint.
Instance: An actual object of the prefab in a scene. If the prefab is a blueprint for a car, an
instance is an actual car. If an object in the Scene view is referred to as a prefab, it is really meant
that it is a prefab instance. The phrase instance of a prefab is synonymous with object of
a prefab.
Instantiate: The process of creating an instance of a prefab. It is a verb and is used like: “I
need to instantiate an instance of this prefab.”
Inheritance: This does not mean the same thing as standard programming inheritance. In this
case, the term inheritance refers to the nature by which all instances of a prefab are linked to
the prefab itself. This is covered in greater detail later this hour.
Prefab Structure
Whether you know it or not, you have already worked with prefabs. Unity’s character controller is a
prefab. To instantiate an object of a prefab into a scene, you only need to click and drag it into place
in the Scene view or Hierarchy view (see Figure 12.1).
Inheritance
When the term inheritance is used in conjunction with prefabs, it means the link by which the
instances of a prefab are connected to the actual prefab asset. That is, if you change the prefab asset,
all objects of the prefab are also automatically changed. This is incredibly useful. More often than
not, you put a large number of prefab objects into a scene only to realize that they all need a minor
change. Without inheritance, you would have to change each one independently.
There are two ways in which you can change a prefab asset. The first is by making changes in the
Project view. Just selecting the prefab asset in the Project view will bring up its components and
properties in the Inspector view. If you need to modify a child element, you can expand the prefab
(described earlier) and change those objects in a similar fashion.
Another way you can modify a prefab asset is to drag an instance into the scene. From there, you
can make any major modifications you would like. When finished, simply drag the instance back
onto the prefab asset to update it.
Breaking Prefabs
Sometimes, you need to break a prefab instance’s link to the prefab asset. You might want to do this
if you need an object of the prefab but you don’t want the object to change if the prefab ever
changes. Breaking an instance’s link to the prefab does not change the instance in any way. It still
maintains all of its objects, components, and properties. The only difference is that it is no longer an
instance of the prefab and therefore is no longer affected by inheritance.
To break and object’s link to the prefab asset, simply select the object in the Hierarchy view. After
selecting it, click GameObject > Break Prefab Instance. You will notice that the object does not
change, but its name turns from blue to black. Once the link is broken, it cannot be reapplied.
Instantiating Prefabs Through Code
Placing prefab objects into a scene is a great way to build a consistent and planned level. Sometimes,
however, you want to create instances at runtime. Maybe you want enemies to respawn, or you want
them to be randomly placed. It is also possible that you need so many instances that placing them by
hand is no longer feasible. Whatever the reason, instantiating prefabs through code is a good solution.
There are two ways to instantiate prefab objects in a scene and they both use the Instantiate() method.
The first way is to use Instantiate() like this:
Click here to view code image
Instantiate(GameObject prefab);
As you can see, this method simply reads in a game object variable and makes a new object of it.
The location, rotation, and scale of the new object are the same as the prefab in the Project view.
The second way to use the Instantiate() method is like this:
Click here to view code image
Instantiate(GameObject prefab, Vector3 position, Quaternion rotation);
This method requires three parameters. The first is still the object to make a copy of. The second and
third parameters are the desired position and rotation of the new object. You might have noticed that
the rotation is stored in something called a Quaternion. Just know that this is how Unity stores rotation
information. The true application of the Quaternion is beyond the scope of this hour. An example of the
two methods of instantiating objects in code can be found in the exercise at the end of this hour.
Summary
In this hour, you learned all about prefabs in Unity. You started by learning the basics of prefabs:
the concept, the terminology, and the structure. From there, you learned to make your own prefabs.
You explored how to create them, add them to a scene, modify them, and break them. Finally, you
learned to instantiate prefabs objects through code.
Q&A
Q. Prefabs seem a lot like classes in object oriented programming (OOP). Is that accurate?
A. Yes, there are many similarities between classes and prefabs. Both are like blueprints. Objects
of both are created through instantiation. Objects of both are linked to the original.
Q. How many objects of a prefab can exist in a scene?
A. As many as you want. Be aware, though, that after you get above a certain number, the
performance of the game will be impacted. Every time you create an instance, it is permanent
until destroyed. Therefore, if you create 10,000, there will be 10,000 just sitting in your scene.
Workshop
Take some time to work through the questions here to ensure that you have a firm grasp of the
material.
Quiz
1. What is the term for creating an instance of a prefab asset?
2. What are the two ways to modify a prefab asset?
3. What is inheritance?
4. How many ways can you use the Instantiate() method?
Answers
1. Instantiation.
2. You can modify a prefab asset through the Project view or by modifying an instance in the
Scene view and dragging it back onto the prefab asset in the Project view.
3. It is the link that connects the prefab asset to its instances. It basically means that when the
asset changes, the objects change as well.
4. Two. You can specify just the prefab or you can also specify the position and rotation.
Exercise
In this exercise, you work once again with the prefab you made earlier this hour. This time, you
instantiate objects of the prefab through code and hopefully have some fun with it. You can find the
complete project for this exercise as Hour12_Exercise in the book assets for Hour 12:
Create a new scene in the same project the Lamp prefab is in. Click the Lamp prefab in the
Project view and give it a position of (–1, 1, –5).
Add an empty game object to your scene. Rename the game object SpawnPoint and position
it at (1, 1, –5). Add a plane to your scene and position it at (0, 0, –4) with a rotation of (270, 0,
0).
Add a script to your project. Name the script PrefabGenerator and attach it to the spawn
point object. Listing 12.1 has the complete code for the prefab generator script.
Listing 12.1 PrefabGenerator.cs
using UnityEngine;
using System.Collections;
public class PrefabGenerator : MonoBehaviour {
//We will store a reference to the target prefab from the
inspector public GameObject prefab;
Use this for initialization
void Start () {
}
With the spawn point selected, drag the Lamp prefab onto the Prefab property of the Prefab
Generator component. Now run the scene. Notice how pressing the B button creates a lamp at
its default prefab position and how pressing the spacebar creates an object at the spawn point.
Also notice how the prefabs collide with each other causing some unique interactions.
You might notice while running this scene that the lamps created continue to fall forever and never
disappear from the scene. As a bonus challenge, see if you can create a plane with a trigger below
the scene that will destroy the lamps when they enter. This way, the game cleans up the lamps no
longer visible and the game has no long-term performance issues as a result.