Prefabs are the most requested feature of Unity. It is one of the first things that beginners have to learn while using Unity.
The prefab acts as a template from which you can create new game object examples in the scene, and any edits made to the prefab asset will be immediately reflected in all examples produced from it. You can override components and settings for each example individually as well.
This blog will make you learn more about the prefabs in Unity.
We can create anything with prefab - as complex as we like, and save it for later use. It allows you to any changes or edit in original and update on other copies of it directly without change in individual object.
We can create prefab using following two ways:
Create a new project and create a new Plane and one Cube in the scene.
Now drag and drop Cube game object from Hierarchy to Project as shown in below the image. This will create a prefab of Cube. Now Delete the Cube in the hierarchy view.
The instance will be created when we drag and drop that prefab from project view to scene view or direct from project view to hierarchy. Now you can see that the created instance is with a blue color name.
When the user clicks on the Plane, the instance of Cube prefab will be created.
Make CubeController.cs Script and write code as shown below:
In the above image code, the Instantiate method will create an instance at run time. In an argument we have to specify the object (that we want to create), position and rotation.
When the user clicks on the Plane, the instance of cube prefab will be created.
Now apply this script to the Plane and assign the reference of the Cube from Project View as shown below image.
Now run the scene and click on Plane, you can see that when user clicks on the Plane a Cube gameobject will instantiate at that time at the position and rotation that we were given in the script.
Now what we have to do is, whenever a user clicks on the Cube its color should change to blue. We are going to use a script to achieve this thing. Make another script name with PrefabColor.cs
And write code as shown below:
Now apply this script to our Cube prefab. So when the user clicks on the particular Cube object, its color will change from white to blue.
Now run the scene click on Plane to instantiate Cube prefab as gameobject and after that click on particular Cube to change its color. When you click on Cube you can see that the color of that Cube will change from white to blue as shown below image.
If you want to return the contents of a Prefab instance into a regular GameObject, you need to unpack the Prefab instance. This is exactly the reverse operation of creating (packing) a Prefab, except that it doesn’t destroy the Prefab Asset but only affects the Prefab instance.
You can unpack a Prefab instance by right-clicking on it in the Hierarchy and selecting Unpack Prefab as shown below.
The resulting GameObject in the Scene no longer has any link to its former Prefab Asset. The Prefab Asset itself is not affected by this operation and there may still be other instances of it in your Project.
Prefabs are an essential feature that you should know before developing a game in Unity. It is like a copy of a GameObject that you can duplicate and use in a scene, or use to dynamically generate GameObjects.
Read this blog carefully and start creating a prefab on the Unity platform.
COO of Vasundhara Infotech, a leading Software development company in the USA. His technological interests has helped the company in making useful decisions.
Sign Up to our newsletter to get latest updates staight in your inbox.
Vasundhara respects your privancy. No Spam!
Sign Up to our newsletter to get latest updates staight in your inbox.
Vasundhara respects your privancy. No Spam!