What is Prefab in Unity
Learn what is prefab in unity, why we should use it, and what is the importance of prefab in Game Development.
Being a Unity Developer, it might be obvious that you have already come across the word, prefabs. There are higher chances that you have seen it while watching Unity Tutorials and most of you might have already started using it. Well, for those who are yet to figure out what Prefabs are and why it is so important in Game Development, let’s jump right to the point and understand it better.
What is Prefab in Unity?
Prefab in a general sense is a fully configured template in the form of a Game Object that can be created, stored, transferred, and reused for various applications where all the properties, components, and child game objects remain the same as it is. It comes into the picture where repetition of a particular game object is intended.
When repetition of a particular system is required, it reduces the time load of copy and pasting the values again, maintaining the hierarchy, and setting up certain systems again and again. Simply, drag and drop the Prefab, and your work is done.
Why use Prefab? Why it is so important?
This is one of the most general questions asked by all beginners, why use a prefab while making games? Why is it so important? The answer to the question is pretty simple, Time is money. The more you spend less time on making something, the more you will be productively efficient, and the more likely you will be able to hit the market with a great product or else meet the client deadline. So, prefab helps you to be super-efficient in your game development workflow, just set up the Prefab once and use it hassle-free without going through a redundant process. For example, if you want to scatter a few rocks on a plane where the rocks have various components and custom scripts attached, you can manually put the rocks everywhere, then select all, and put the components required which is a time-consuming process. What you can do instead is, make a rock prefab with all the required components and scripts attached and then simply drag and drop it to the plane wherever you want, that’s it, super easy and super-efficient.
Now the second important reason why we should be using a prefab workflow is to modify the values of all the used assets. What if, the rocks we have implemented need to be changed a bit, let’s say, the scaling of it, now in order to do that, we will select all the game objects again, and then scale it to the desired size, which seems to be pretty easy, isn’t it? But what if those game objects are also present in other scenes as well, now, going to all the scene and changing it would be waste of time and a redundant process. Here comes the role of Prefab, now, if you have set up the rocks as a prefab then simply change the scaling of the prefab itself and you would notice all other prefabs would be changed to that scaling instantly, across scenes as well, which is undoubtedly super easy and super-efficient!
But if this is the case then one more question arises, what if we don’t want to apply the changes to all the prefabs, instead, just change the properties of some of them, is that possible?
In short, yes, without any doubt. Prefabs work as if they are identical but they are flexible too. You can easily change individual properties while overriding current values and also create variants of that prefabs to be used ahead of time.
Common Use case of Prefabs
The use case of prefabs purely depends on the game type and its mechanics. It completely depends on the developer how he is going to set up the game for an efficient workflow. Although, the applications of Prefabs are countless, still, here are a few examples we commonly see,
· Environmental Assets and Props
· Non-Player Characters (NPC)
· Shooting Bullets
· Repeated Assets over scenes
· Player itself
How to create a Prefab?
Creating Prefabs is pretty simple. Once, the Game Object of which Prefab is to be made is set up, just drag and drop that game object to anywhere in your Asset Folder or to any desired folder in your Project panel. And that’s all we need to do; Unity will automatically create a Prefab of that Game Object which can be used in our game for several purposes.
We can make an instance of that Prefab by simply dragging and dropping it to the scene or else hierarchy. Dropping it to the hierarchy will position the Prefab to its preset coordinates whereas dropping it inside the scene will position it in the world space.
Prefabs in the Project Panel can be differentiated from the normal game assets from their icons and text color. Once a Prefab has been created the icon and text turn blue which clearly indicates it’s either an original Prefab or else a Prefab variant.
Now, what is Prefab Variant? Let’s dive a bit deeper.
The prefab variant works on the concept of overrides. As the name suggests, the prefab variants are newly created prefabs from the original prefab but have overridden settings from the base prefab. This gives the user the ability to receive updates from the base prefab for all other settings apart from overridden ones and allows the user to make changes for the overridden settings pretty seamlessly.
This way differs from the generic duplicating the prefabs as a whole new one and helps the developers to maintain several variants with small tweaks and still maintain all the prefabs with major updates from the base prefabs. Creating the variant is as easy as creating a prefab. Just drag and drop the prefab game object from the hierarchy to the project panel again and when prompted by Unity to either make it a variant or a new original prefab just hit the variant button. Now whatever changes we make on the variant are local to that prefab and we can still inherit other settings from the base prefab. Additionally, we can also push all these changes to base prefab if required. This gives more flexibility and opens up several possibilities to work with.
What is Prefab Mode?
Working with all the modifications of the prefab from geometry to materials inside the hierarchy might be a complete mess and could be confusing while working with a long list of hierarchy elements. Not just hierarchy but working with the prefab on a busy scene having hundred of elements would be too tough to manage. This was the scene before Unity realized the pain of all the Unity developers. Thanks to Prefab mode which saves us from such misery. This mode opens up the prefab in an isolation mode in a different window just showcasing the prefab context for better-focused editing. Unlike previously, where developers used to edit the prefab inside the scene itself while getting stressed out and being confused, now, Unity allows you to open prefabs in an isolation context window where developers can focus on the prefab itself and edit the way they want. The prefab mode just shows the prefab and all its elements in a clean way to make it a hassle-free experience while working with big complex prefabs.
Opening up the prefab mode is pretty easy, just double click on the prefab, or else if your prefab is in the hierarchy then click on the small right arrow to open up the prefab mode.
Auto Save Feature in Prefab mode
Lastly, there’s an Auto-Save button that might be super useful while working with prefabs. Inside the Prefab Mode, there’s an AutoSave toggle button to the corner which is undoubtedly a life-saving feature to save us from losing all our progress. Well, in some cases you might keep it off but we do like it turned on for any unavoidable circumstances.
So, this was all about the Importance of Prefabs in Game Development. Hopefully, this adds value to your workflow and increases your work efficiency. It would be great to explore this topic more deeply in the near future.
With Love From VOiD1 Gaming
**VOiD1 Gaming is not sponsored by the above company. All trademarks appearing above are registered trademarks of the respective company.**