Cases where Prefabs are useful:
- Environmental Assets (Placing trees, bushes, rocks etc.)
- NPCs (non-player characters) (Spawning enemies)
- Projectiles. (Spawning bullets)
- Main Player Instance. (Spawning our player)
Creating a Prefab
I will be using models from this free Low Poly Tree Pack.
Usually, when we drag & drop a model directly into the Scene, it has some issues, like:
- Missing textures/materials
- Wrong scale
- Wrong rotation etc.
We can easily fix it in the Scene by assigning the correct material(s), changing the scale/rotation etc.:
However, if you plan to use the model repeatedly and/or in various Scenes, repeating the steps above will quickly become a tedious task. That's where Prefabs come in handy.
- To create a Prefab, simply Drag & Drop the Object into the Project view.
After the Prefab is created, you can drag & drop it into a Scene to create an exact same copy.
Editing Existing Prefabs
Editing Prefabs can be done in 2 ways, by opening the prefab (Method 1) or by modifying prefab directly in the Scene then applying the changes (Method 2).
- Select the Prefab in the Project view, then in the Inspector View click "Open Prefab"
- The Prefab will be opened in a Scene view where you can modify it.
- After you done, click the back arrow in the Hierarchy View. The changes will be saved and applied to every spawned prefab instance across the Scenes:
- Drop the Prefab into the Scene then start making the changes you need (Changing scale, adding/removing objects etc.)
- In my case I have added another Object to the Hierarchy (Note the (+) symbol near the icon)
NOTE: With this method, changes are not saved automatically, instead you need to manually select the Root object of the Prefab and in Inspector View click on "Overrides" then click "Apply All" to save the changes or "Revert All" to revert the changes. Clicking "Apply All" will modify the main Prefab along with all of its spanwed instances.
NOTE 2: Some actions are not supported when editing Prefab directly in the scene. For example if you need to restructure the hierarchy, you will be presented with a message "Cannot restructure Prefab instance" and with options to either "Open Prefab" or "Cancel". If you click "Open Prefab" it will open the Prefab using Method 1, which sometimes is not desirable.
To avoid opening the Prefab, click "Cancel" then right click on the Prefab instance and click "Unpack Prefab" (alternatively you can click "Unpack Prefab Completely" in case there are child prefabs nested within hierarchy which you want to unpack aswell).
Unpacking Prefab will remove its association with the main Prefab, thus making it fully editable in the Scene.
To save the Prefab back, select the folder where Prefab is located, then drag & drop the modified Object from the Hierarchy View into the Prefab.
Sometimes (if the changes are too drastic), you will be presented with a confirmation message.
Make sure to double-check that you're modifying the right Prefab then click "Replace Anyway" to finalize the action or "Don't Replace" to cancel the action.
Congratulations, the new Prefab is now saved and ready to use!