How to create Prefabs in Unity?
What is a Prefab?
A prefab is an asset type that allows you to store a GameObject complete with components and properties. This allows us to share pre-configured hierarchies of objects between scenes. The prefab acts as a template from which you can create new object instances in the scene in the Editor and at runtime.
You should try to prefab everything that you put on your scenes so you can create a new level from an empty scene just by adding prefabs to it. The reason why you should use prefabs is that when a prefab changes, all the instances change too.
To create a Prefab Asset, drag a GameObject from the Hierarchy view into the Project view (you can create a folder and sub-folders to organize all your prefabs). The GameObject, and all its components and child GameObjects, becomes a new Asset in your Project window. The original GameObject is now an instance of the newly created Prefab Asset.
How to edit a prefab in Unity?
The Prefab Mode allows you to view and edit the contents of the Prefab Asset separately from the other scene gameObjects. Any modification you make in Prefab Mode will affect all instances of that Prefab.
To enter the Prefab Mode, you can:
- Use the arrow button next to the Prefab in the Hierarchy window;
- Click the “Open” button in the Inspector window;
- Double-click the prefab in the Project window.
To exit the Prefab Mode, use the back arrow in the header bar to navigate back one step, which is equivalent to clicking the previous breadcrumb in the breadcrumb bar in the Scene View.
Prefab Mode has also an Auto Save toggle in the top right corner of the Scene View. This toggle is on by default, which means that any changes that you make to a Prefab are automatically saved to the Prefab Asset.
Nested Prefabs in Unity
Nested Prefabs are supported in Unity’s latest versions. They are Prefab instances that are included inside other Prefabs. They keep their links to their own Prefab Assets, while also forming part of another Prefab Asset.