DEV Community

Cover image for Digging into Unity: Prefabs
Marc Mintel
Marc Mintel

Posted on • Edited on

Digging into Unity: Prefabs

Hey there, I'm Marc and I'm a professional web developer since a few years. It's time for something else so I decided to learn game development with C# and Unity. I'm going to use dev.to to keep my personal notes and summaries.

So one of the first things that I encountered in Unity are so called Prefabs. What's that? What does it even mean?

What is a Prefab?

The word is short for prefabricated. A Prefab is a reusable asset that contains a GameObject, it's configuration, it's components and everything that's nested below it. You can see Prefabs as templates for GameObjects.

Prefab in a Scene

A Prefab is indicated by this little blue box icon. The arrow icon on the right will bring you to the Prefab editor, where you can edit the Prefab in isolation.

How to create a Prefab?

Just drag a GameObject from your Scene window into your Project window.

When to use Prefabs

The general rule of thumb is that you should almost always prefab your GameObjects, as you are most likely going to reuse them at any time in future. You can even prefab your camera and lighting, which is useful when you create multiple scenes.

What can you do with a Prefab?

Overriding

If you could just use a Prefab as it is it would not be that useful. That's why you can override it's configuration.

Prefab Overrides

Overrides are marked as bold. If a Prefab is overridden it will always keep these overrides, even if you change that property in your Prefab. So if you override a property you are removing the sync. You can then apply your overrides to the Prefab to apply your changes to all instances or reset all overrides to revert your changes of the current instance to the Prefab configuration. Changes made in the Prefab editor are applied immediately.

Nesting

You can also nest Prefabs within Prefabs to combine them in various ways. Nothing else to say I guess.

Unpacking

Unpacking is the process of unlinking a GameObject from a Prefab. That doesn't delete the Prefab nor the GameObject, it just removes the syncing between those.


That's it about Prefabs. As I am still completely new on Unity take this with a grain of salt. The provided information are based on my own experiences and research. If you have something to add or correct please comment it.

Top comments (0)