In this post I discuss the prefab-focused development process for Observatorium.
Hopefully the information included below will be useful for your own Unity projects.
What Are Prefabs?
A prefab is a reusable GameObject template (comprised of multiple components - such as colliders, renderers and scripts - with associated properties) that allow you quickly duplicate functionality by creating a copy or 'instance' of it.
Why Use Prefabs?
If you are making a gameplay-focused game (where the focus is on some repeatable mechanic), chances are you will be reusing/duplicating alot of similar game objects.
Consider the process of constructing levels for your average first person shooter; once you have built a rough level layout, you will likely populate it with e.g. weapons/armour/ammo/buttons/doors/NPCs: such items are ripe for prefabrication.
Prefabs are useful for cutting down development time as they help you avoid needless duplication: you only have to change a single master object to have important changes propagate throughout your project. Additionally, they force you to think about your assets in a modular fashion which is always useful for enforcing maintainability.
When to use Prefabs?
Generally speaking, it's useful to create a prefab for anything you plan to use in your scene or project more than once. Most of the Observatorium game world objects are set-up as prefabs:
If you have built a very specific layout or sequence that you do not plan to use anywhere else, it may not be ripe for prefabrication: however, the underlying building blocks could be set-up as prefabs.
Unity supports 2 approaches to creating prefabs:
(1) Choose Assets >> Create Prefab from the menu
- This creates an empty prefab template in your Project view (a grey box icon denotes an 'empty' prefab asset has been created)
- To make this prefab useful, drag any Game Object from your scene onto this prefab (the grey box changes to blue to denote it can now be instantiated
(2) Drag an existing game object from your Scene view to your Project view
- I find this process preferable as it shortens the required steps
Creating Instances of Prefabs
Simply dragging your prefab from the Project view to Scene view will create an instance of it. You can also duplicate prefab instances in your Scene view.
You can tell that you have created an instance of a prefab because
(1) It will appear as blue text in your scene hierarchy
(2) Selecting it shows 'prefab' text at the top of the inspector view next to Select/Revert/Apply options
Tweaking your Prefabs
Once you start instancing prefabs you are free to change their properties as you build your levels. You can see what properties you have changed in the inspector by looking for the bold headings in the Inspector. Adding a brand new component to an instance of a prefab will cause the entire component (i.e. all underlying properties) to appear bolded.
These are 3 very useful features provided by Unity that help you edit your prefabs in a safe and efficient manner:
(1) Select - clicking this button will select the associated prefab in your Project View - useful for checking you are modifying the correct data or if you are trying to find the source prefab.
(2) Revert - if you make any changes to a prefab instance you dont like, Revert will grab the state of the master version.
(3) Apply - if you make changes to a prefab instance you feel would be beneficial to all related instances, hitting Apply will propagate those changes through your project. Alternatively, you can alter the master prefab by modifying the source asset directly.
Some specific prefab examples from Observatorium:
The reason I love working with prefabs for Observatorium is so that I can write general code that relies on instances of prefabs rather than bespoke game objects. All of the objects you see in the GIF below - the stars, fish, rocks and even the ship - are prefab instances. The sketch code does the heavy lifting:
Building puzzles in this way means I can very quickly change the behaviour of a puzzle simply by adding new stars or fish or moving existing ones around. It also means I reduce the dependancies between the puzzle manager (which handles the sketching) and the game world objects which makes it very easy for me to add new types of stars and fish. Additionally, if I ever choose to modify the behaviour of an existing star or fish type, that behaviour could be easily propagated throughout my project thanks to the prefab system.
Prefabs are very powerful but they require care when using them. Some common pitfalls:
- Before creating a prefab, it's important to check you don't already have duplicates of the object you are about to turn into a prefab. Unity won't treat these duplicated objects as prefabs as they were not instanced from a specific prefab.
- Keep an eye on the blue text in your scene hierarchy - if something isn't showing as blue that should be, you may be missing a prefab asset you once had in your project.
- When applying changes to your prefabs the position and rotation are not affected - you will generally move and rotate instances often - but scale is affected.
If you have any thoughts or comments on working with prefabs then please post below.
Thanks for reading!
The Man Who Flew Away