Skip to content

ReimuDodge Tutorial Part 5

Barley edited this page Oct 30, 2020 · 31 revisions

Prefabs and Difficulty stages

1. Prefabs

A very important aspect of Unity development that we've glossed over until now is Prefabs. Prefabs are, basically, records of scene objects stored as files. Think of them as blueprints for your GameObject that the scene follows. We're going to turn our Player and Bullet objects into prefabs so that we can reuse them anywhere we like.

1a. Creating prefabs

First, create a "Prefabs" folder in ReimuDodge. Then, in your scene, drag the Bullet object directly into this prefabs folder in the project window. This creates a prefab. Do this for the Player object as well.

1b. Editing prefabs

You'll notice that you can click on these prefab files and edit their attributes in the inspector just like you can in the scene. The objects in your scene are instances of these prefabs and so any changes to the master prefab will also affect the prefab's instances across all of your scenes.

Another way to edit your prefab is to open it. This can be done by hitting "Open Prefab" with the prefab file selected, or by finding an instance of the prefab in your scene and pressing "Open" in the inspector.

This will start prefab editing mode. Just like editing the prefab file directly, any changes you make here will apply to all copies of this prefab in your scene. This is very useful if you want to, for instance, change the speed value for all of your bullets.

Notice the "target" value of the bullet prefab is empty on this screen. This is because the target (the player) must be an object in a scene, and the bullet prefab itself doesn't belong to any scene. In other words, it should be empty because it's a value that must be assigned within a scene.

1c. Editing prefab instances

You can also change prefab instances in your scene, just like normal. Any changes you make to the prefab instance will be bolded, indicating that that field is no longer being controlled by the prefab. You can then then choose to Apply or Revert these changes:

Changing values of individual prefab instances can be useful if you want to create some small differences between your prefab objects, such as a Player object with a different sprite. But there are more advanced techniques we can use to get even fancier.

1d. Prefab Variants

We can make special bullet types using Variants. Try dragging the Bullet prefab you made earlier from your scene back into the Prefabs folder, and select "Prefab Variant" when prompted:

Prefab Variants work like regular prefabs, except they also keep a connection to their "parent" prefab. This means you could, for example, make a bigger bullet type that moves the same speed as an ordinary bullet, or a bullet that kept its size the same but was faster than normal:

And, remember, you can place as many of these as you like in your scene:

TIP - when dealing with lots of similar objects in a scene, it often helps to group them under a parent object to keep the scene from getting too hectic.

This video gives a good overview of what Prefab Variants are all about.

1e. Nested Prefabs

But wait, there's more! Nested prefabs are exactly what they sound like; prefabs inside prefabs. This can obviously be quite powerful, giving you nearly limitless ability to mix-and-match bits of functionality throughout your game. As a quick example, you could use nesting to create a mega-bullet, orbited by several other bullets which shared their attributes.

Check out this video on Nested Prefabs and Prefab Variants for more ideas about what to do with these incredibly useful tools.

Don't worry if this takes some time to understand. Just practice using Prefabs and you'll understand naturally soon enough.

2. Difficulty Levels

Now that we understand how prefabs work, we can use them make the harder difficulty stages.

2a. Create extra scene files

Save the ReimuDodge1 scene and, in your Scenes folder, make two copies of the ReimuDodge1 scene file named "ReimuDodge2" and "ReimuDodge3". You can use the ctr+D shortcut in Unity to duplicate files.

If you open these up in Unity, they should be identical copies of your first scene using the same shared Player and Bullet prefabs.

2b. Increase difficulty

Start with ReimuDodge2 and increase the difficulty by drag more bullet prefabs into the scene. Modify each of them to have a slightly different delay so that they don't all fire at the same time.

Remember that we kept the value of the "target" variable blank? Now that your bullet instances are within the scene, you can drag the Player object (the instance of the Player prefab in our scene, not the Player prefab itself) into the that slot and they should target the player properly. Do this for every bullet and check that the targetting still works.

You can group objects under parent objects like this to stop the scene getting too hectic.

Now, do the same for ReimuDodge3, but this time add even more bullets!

Remember to save your new scenes. We're now very nearly done with the tutorial.

Benefits of Prefabs

Before we go on, we should briefly understand why prefabs are especially helpful in NitorInc.'s case as well as just useful all-around.

  • The majority of your info for your GameObject gets stored in one place in the project. For large microgames with complicated objects, this prevents us from using three times the file space when using the same object in all three scenes.
  • Having your gameobject in one place also makes it a breeze to change anything. Instead of having to apply all your changes to each difficulty scene, simply change the base prefab and have it apply to all of them.
  • This also complements how Github tracks your file changes and allows for cleaner commits. If in one commit you make changes to only the base prefab to have it affect all scenes, you can be sure of that because Github will tell you that only the prefab has been changed and not the .unity scenes. Or you can discard changes to a .unity scene to make sure the prefab changes you applied still work.
  • Being able to revert values and entire prefab instances to the default prefab is especially useful for toying with new features and functionality while having an easy "Back" button.
  • Instances of prefabs can be created with GameObject.Instantiate(). This is near-essential for some microgames that need objects to be created mid-gameplay. (It's a bit slow, so you also don't want to use it too much at once)

Commit

Commit your prefabs and scene changes; we wont need to edit them any more.

Clone this wiki locally