Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Unity 2020 Virtual Reality Projects

You're reading from   Unity 2020 Virtual Reality Projects Learn VR development by building immersive applications and games with Unity 2019.4 and later versions

Arrow left icon
Product type Paperback
Published in Jul 2020
Publisher Packt
ISBN-13 9781839217333
Length 592 pages
Edition 3rd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Jonathan Linowes Jonathan Linowes
Author Profile Icon Jonathan Linowes
Jonathan Linowes
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Virtually Everything for Everyone 2. Understanding Unity, Content, and Scale FREE CHAPTER 3. Setting Up Your Project for VR 4. Using Gaze-Based Control 5. Interacting with Your Hands 6. Canvasing the World Space UI 7. Teleporting, Locomotion, and Comfort 8. Lighting, Rendering, Realism 9. Playing with Physics and Fire 10. Exploring Interactive Spaces 11. Using All 360 Degrees 12. Animation and VR Storytelling 13. Optimizing for Performance and Comfort 14. Other Books You May Enjoy

Using prefabs

Unity prefabs (short for prefabricated object) allow you to create, store, and modify GameObjects as reusable assets together with all its components, property values, and child GameObjects. Prefabs are easily created by dragging an object from the scene Hierarchy window into the Project window, or they may be imported as part of a Unity asset package. You can read more about prefabs in the Unity Manual at https://docs.unity3d.com/Manual/Prefabs.html.

Let's see how this works by creating a simple reusable model built from simple geometric elements.

Creating and instantiating a prefab

We're going to create a reusable character prefab named BobHead. First, let's build the little guy from a few primitive 3D objects, as follows:

  1. In the Hierarchy, create an empty GameObject named BobHead (by clicking on +| Create Empty; name it BobHead).
  2. Set its position to something like Position (3, 0.75, 1.25).
  3. Create a child Sphere for its head (right-click the BobHead and click 3D Object | Sphere). Name it Head.
  4. In the ProjectMaterials/ folder, create a new Material, named BobHead Material, and make it a dark blue (for example, #191963).
  5. Drag the BobHead Material onto the Head.
To help you focus on specific objects in your scene you can temporarily disable other objects from view. One way to do this is to fully disable the object in the Inspector (uncheck the Enable checkbox in the upper-left corner), or better, in the Hierarchy window, you can click the View eye icon in the far-left of the Crate GameObject, for example, to temporarily hide it while working on the scene.

Now, add the eyes:

  1. Create a child white-colored eye (right-click the BobHead, then click 3D Object | Sphere) and name it Eye.
  2. Set its Transform Position (0.15, 0.2, -0.35) and Scale (0.15, 0.25, 0.25).
  1. Drag the White Material that we created earlier from the ProjectMaterials/ folder onto the Eye.
  2. Duplicate the eye (select the Eye and press Ctrl + D).
  3. Change its Transform Position X to -0.15.

The resulting model, including the scene Hierarchy, is shown in the following screenshot:

Now to create a prefab of the object. We'll save it in a project folder named Assets/Prefabs/ by going through the following steps:

  1. In the Project window, create a new folder named Prefabs (at the root Assets/, right-click, then click Create | Folder and name it Prefabs).
  2. In the Hierarchy window, click and drag the BobHead GameObject into the Project window's Prefabs/ folder we just created.

Note that a few things have changed in the editor, as shown in the following screenshot:

  • The ProjectAssets/Prefabs/ folder contains the BobHead.prefab file, with a preview image.
  • Because the asset is presently selected, the Inspector shows its component values and a button to Open Prefab for editing. We'll get to that in a moment.
  • The BobHead object name in the Hierarchy is now blue, indicating that it's now a prefab instance.
  • There's also a > icon on the BobHead that you can click to edit the prefab asset (the same as the Open Prefab button).

The BobHead prefab is now available as a template for other BobHead GameObjects; you may want to add one or more to your scenes. All instances of the prefab will inherit the properties of the prefab. To add a new instance, just drag it from the Project window into the scene. Let's do that now:

  1. In the Project window, click the BobHead prefab and drag it into the scene (either the Scene view window or the Hierarchy).
  2. Once positioned as you like, drop it into the scene.
  3. Add a few more too.

The following screenshot shows the scene with multiple BobHead instances. In the Hierarchy, the BobHead objects are blue because they reference prefab assets:

You can now modify the instance as you like, such as adjusting its position and scale.

Keep in mind that a prefab is an asset file that resides in a folder like any other file on your computer. It can reference other assets, such as Textures and Materials, and Unity will remember those links. But a prefab asset cannot have any direct references to objects in the Scene Hierarchy—for example, if you drag a GameObject from the Hierarchy onto a slot of a component of another object, and then save that object as a prefab, the reference to the GameObject will be lost, and it will have a null value when instantiated at a later time.

In summary, we have learned the following definitions:

  • GameObject: Generally refers to game objects in the Scene and Hierarchy, containing a geometric mesh, material, and other components that define its behavior.
  • Asset: Refers to files in the ProjectAssets/ folder that can be added to a scene, including prefab GameObjects, materials, audio clips, and more.
  • Prefab asset: A GameObject saved as an asset.
  • Prefab instance: A prefab that's been added to a scene is said to be instantiated.

Now that we have a prefab, let's see how to edit it.

Editing and overriding a prefab

Suppose that you now decide that the BobHead needs adjustment; perhaps you want to add a hat. You can modify the prefab and all of the instances will be updated. You could select the BobHead prefab in the ProjectPrefabs/ folder and select Open Prefab to edit it (or just double-click it). Right now, we'll edit it first in the scene Hierarchy, then apply the overrides:

  1. To add a hat, select one of the BobHead objects in the hierarchy, right-click,and select Create Empty, then rename it Hat.
  2. Set its Position to (0, 0.5, 0).
  3. Right-click the Hat and select 3D Object | Cylinderto add a cylinder as a child of Hat— name it HatTop.
  1. Set itsTransform Scale to (0.5, 0.1, 0.5).
  2. Drag the Red Material from the Project's Materials/ folder (created earlier in this chapter) onto the HatTop.
  3. Right-click the Hat again and select 3D Object | Cylinderto add a cylinder as a child—name it Brim.
  4. Set itsTransformScale to (0.75, 0.01, 0.75) and Position to (0, -0.075, 0).
  5. Drag the Red Material from the Project's Materials/ folder onto the Brim.
  6. Adjust the Hat transform to Rotation (0, -15, 25) and Position (-0.2, 0.5, 0).

That looks cool! Presently, only one of the BobHead instances has a hat. Suppose we want each of them to have one too. Select the BobHead object in Hierarchy; note the Overrides dropdown in the Inspector. Click it to show the modifications that you have made relative to the original prefab, as shown in the following screenshot: that'd be the Hat:

Do the following steps to actually apply the overrides:

  1. Click the Overrides in Inspector.
  2. Press Apply All.

Voila! Now all the BobHeads in the scene have been updated, as shown in the following screenshot:

Because prefabs may include child objects, it is possible for a child to be another prefab. That's perfectly fine. This is called a nested prefab, and will behave as you expect—for example, we could have made the Hat object its own separate prefab and then inserted it into the BobHead prefab. If you change the original child prefab, then that change will be reflected in the parent prefab.

In addition, modified instances of a prefab can be saved as a separate prefab variant. In that case, the new prefab still references the original prefab so that if the original prefab is changed and said properties have not been overridden by the variant, then the variant (and all its instances) will inherit those changes.

One more thing to point out. When you edit a prefab instance in Unity, you have the option to view it in the Scene window in the context of its position within the scene (Normal), to gray out the surrounding scene (Gray), or hide the context altogether (Hidden). The following image shows a BobHead being editing with a Gray context:

There you have it! Prefabs are a very powerful feature of Unity that we will use throughout this book. Next, we'll look at how to import content from outside of Unity.

You have been reading a chapter from
Unity 2020 Virtual Reality Projects - Third Edition
Published in: Jul 2020
Publisher: Packt
ISBN-13: 9781839217333
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime