Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Build Your Own Metaverse with Unity
Build Your Own Metaverse with Unity

Build Your Own Metaverse with Unity: A practical guide to developing your own cross-platform Metaverse with Unity3D and Firebase

By David Cantón Nadales
€32.99 €22.99
Book Sep 2023 586 pages 1st Edition
eBook
€32.99 €22.99
Print
€41.99
Subscription
€14.99 Monthly
eBook
€32.99 €22.99
Print
€41.99
Subscription
€14.99 Monthly

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Sep 15, 2023
Length 586 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781837631735
Vendor :
Unity Technologies
Category :
Concepts :
Table of content icon View table of contents Preview book icon Preview Book

Build Your Own Metaverse with Unity

Getting Started with Unity and Firebase

The metaverse and the future are a combination of increasingly united concepts, a new formula that was born to stay and lay the foundations for new forms of communication in the coming years. A metaverse is a shared, three-dimensional virtual world where people can interact with each other and with digital objects in real time. Its utility lies in providing an expansive space for creativity, collaboration, and entertainment.

It allows users to explore immersive virtual environments, engage in social, commercial, and educational activities, and experience new forms of expression and experiences. In addition, the metaverse can serve as a platform for developing innovative solutions in a variety of areas, such as remote work, distance learning, product design, and simulation, offering endless possibilities for human interaction and imagination.

In the coming years, the way we attend work meetings, concerts, training courses, and even dating to find a partner may occur in the metaverse. This book will help you get started with programming a metaverse by using the Unity 3D video game engine, the Google Firebase suite, and other amazing tools.

In this first chapter, you will learn the first steps of configuring your computer so that you can start developing virtual worlds in Unity 3D. We will start by laying down the basics about the Unity 3D video game engine, introducing the services offered by Firebase and explaining how they will help us in our project, and covering what other tools we will need.

Once we have reviewed all the concepts and technologies we will use, we will install Unity 3D on our computers. This chapter’s mission is to ensure we completely configure our work environment for satisfactory project development.

We will cover the following topics:

  • Installing Unity
  • Organizing your project assets
  • Choosing an input handler
  • Understanding Firebase services

Note for advanced readers

If you have previously worked with Unity, you will know how to install it correctly on your system. If you already have it installed, you can skip the following steps – just keep the version of Unity that we will use as a base in this project in mind to avoid incompatibilities in future chapters.

Technical requirements

For the Unity 3D game engine, you will need a Windows or Mac computer. Normally, Unity can be run on most computers nowadays, but if you want to have a look at the minimum requirements for a computer to run Unity, go to https://docs.unity3d.com/Manual/system-requirements.html.

If you plan to compile your metaverse for iOS or Mac, you must have a Mac, with the latest version of macOS and also a current Apple developer account. If you only want to compile for Android, Windows, or Linux, you can use a Windows or Mac computer indifferently. In this book, the screenshots provided have been taken from a Mac, but this should not affect you in any way, even if you have Windows.

Fortunately, our project can be tested by running the scene directly from Unity. This will make it much easier for you to progressively see small changes with just one click. Only when we get closer to the end of this book will we need an Android and/or iOS device to test compilations on mobile devices, but the latter is optional if you want to compile for mobile devices.

You must follow this first chapter in detail to make sure that your computer uses the same version of Unity and other dependent plugins that we will show throughout.

Almost certainly, the project that we will be creating throughout this book will be compatible with future versions of Unity 3D. So, if, for any reason, you need to use another version of Unity that’s different from the one recommended here, it will not be a problem.

This book’s GitHub repository contains the complete project that we will work on: https://github.com/PacktPublishing/Build-Your-Own-Metaverse-with-Unity/tree/main/UnityProject.

Installing Unity Hub

To develop our metaverse, just like a video game, we will need to install Unity on our computers. In this section, we will follow all the necessary steps to do so. We will install Unity Hub, which will allow us to manage multiple versions of Unity in a simple and orderly way, create a project from scratch, and explore the fundamental parts of the editor to get familiar with the graphical interface.

Unity Hub is an application that allows us to manage and install multiple versions of the Unity editor on our computer. Also, for each version, it allows us to install different build platforms and tools in an isolated way. It is very common that throughout our professional careers developing video games, we need different versions of Unity to support different projects, which, for whatever reason, cannot be updated or do not work quite right in later versions of Unity.

This is where Unity Hub comes into play – it allows us to easily install any version of Unity in isolation without it conflicting with other versions. It also allows us to load existing Unity projects on our computers and configure which version of Unity it should use when opening.

To install this tool, execute the following steps:

  1. Please download and install the Unity Hub program from https://unity3d.com/get-unity/download. It is recommended that you use Unity Hub to manage the Unity versions on your computer instead of downloading and installing the Unity editor directly.

The screenshots in this book were taken in Unity Hub version 3.0.0. The functions and buttons in earlier or future versions should not change.

  1. You will need to activate a license for Unity if you have not already done so. The Unity User license has free plans for Students and Community; you can decide if you need to upgrade your plan to Plus or Pro in the future. For our project, a free plan is sufficient since all Unity features are open in the free license.
  2. Press the Download For Mac or Download For Windows button, as shown in the following screenshot:
Figure 1.1 – Installing Unity Hub instead of downloading Unity directly

Figure 1.1 – Installing Unity Hub instead of downloading Unity directly

  1. If this is your first experience with Unity or you want to expand your knowledge, I recommend that you take a look at the Learn and Community menus that you will find in Unity Hub when you open it.
  2. If this is the first time you are using Unity Hub, you will need to create a new account for free. You can also log in with your Google, Facebook, or Apple account and accept the terms and conditions of use.

Once we have downloaded Unity Hub, we can proceed to install it on our computers. Unity Hub allows us to manage multiple installations of different versions of the Unity editor on our computers. This will allow us to maintain compatibility with all previous projects that we have or those that we will create in the future. Next, I will guide you through the process of installing a new instance of the Unity editor step by step.

Installing the Unity editor

As a recommendation, whenever you start a new project in Unity, it is interesting to download the latest version available. That is why, in our metaverse, we will do it this way.

When starting a new engine installation, Unity will show us different options, such as the recommended LTS version, other recent versions, and a history of previous versions, all of which are very useful when you have a legacy project that needs a specific version to work.

To start a new installation, execute the following steps:

  1. On the Unity Hub main screen, select the Installs tab and click on the Install Editor button:
Figure 1.2 – The Install Editor button in Unity Hub

Figure 1.2 – The Install Editor button in Unity Hub

  1. Click on the Archive tab and then click on the download archive link, a tab will open in your browser that will show the Unity version catalog. If you wish, you can also go to the url https://unity.com/releases/editor/archive in your browser:
Figure 1.3 – Archive tab in Unity Hub

Figure 1.3 – Archive tab in Unity Hub

  1. This project has been built from version 2021.1.14f1. Once you are on the Unity version catalog page, you can search for the desired version. Click on the Unity 2021.X tab to find the 2021.1.14f version, finally click on the Unity Hub button, this action will launch the download in Unity Hub.

About the Unity version

In this book, we have built the project on version 2021.3.14f1. This version will most likely be lower than the version Unity recommends at the time of installation. We cannot guarantee that the project will work in future versions of Unity. To use a particular version of Unity or if the version you are looking for is not listed in Unity Hub, you can access the version history at https://unity.com/releases/editor/archive.

  1. The next step is very important: to configure the installation, we must select the components that will be installed initially. Do not worry if you have skipped this step – you will be able to reinstall other components in the future. For the correct course of our project, we will need to check the following:
    1. If you are working on Mac, you will have to select Visual Studio for Mac, whereas if you are on Windows, the Microsoft Visual Studio Community 20xx option will appear.
    2. In Chapter 14, Distributing, we will learn how to compile and export our project on different platforms. To successfully carry out this part, at this point (or later if you wish) we need to install the necessary components to compile for Android, iOS, and Windows.
    3. Check the following components:
    • Android Build Support, with OpenJDK and Android SDK and NDK Tools
    • iOS Build Support
    • Linux Build Support (IL2CPP)
    • Mac Build Support (IL2CPP) (Mono) if you are using Windows
    • Windows Build Support (Mono) (IL2CPP) If you are using Windows

Some of the components are heavy and may take a while to download and install; it’s a good time for a coffee!

Changing Unity’s installation location

Using the Unity Hub gear icon, you can change the location where the Unity version you have selected will be installed. You can change the path from the Installs tab:

Figure 1.4 – Setting the default installation folder

Figure 1.4 – Setting the default installation folder

Well, after knowing the main requirements to be able to work with Unity3D without problems, we will see in detail how to create new projects from Unity Hub.

Creating and managing Unity projects

The Unity Hub tool also serves to manage our projects, regardless of the version of Unity in which they were conceived. We can have an infinite number of projects, old and new, and each of them can be configured to be edited in a different version of Unity. You can easily add the projects you have in some directory on your computer using the Open button.

Don’t worry if you have loaded a project that uses a version you don’t have installed in Unity Hub. As you can see in Figure 1.5, a warning icon will appear to warn you that the project uses a version of the engine that you do not have installed and allows you to install that particular version by clicking on the button specifying the version number:

Figure 1.5 – Managing multiple projects with different versions of Unity

Figure 1.5 – Managing multiple projects with different versions of Unity

To begin, we will need to create a new project. Each project that we’ll create from Unity Hub will be created in a specific folder that we create. Don’t worry about this – the project creation wizard will ask us each time where we want to create it.

If you always want to create them in the same directory and you want to avoid configuring this every time you create a new project, you can configure it at a general level in the same window that appears in Figure 1.4.

In the Projects tab, you will see which directory is used by default for each new project. Just click and select the new destination and click Accept.

Now, to create our new project, execute the following steps:

  1. With Unity Hub open with the Projects tab active, click on the New Project button, located at the top right. Once the new project creation wizard opens, it will ask us to update the initial configuration we want to have (template), the name of our project, and the location where it will be created:
Figure 1.6 – The New project button in Unity Hub

Figure 1.6 – The New project button in Unity Hub

  1. Scroll down and select the 3D (URP) template. We will see why we have selected this item and not another one shortly. It is possible that you may not have used the 3D (URP) template before and you need to download it; in this case, click on the Download template button:
Figure 1.7 – Creating a new project

Figure 1.7 – Creating a new project

  1. Once you have downloaded the template, you can fill in the title of your new project and its location. In my case, I am going to call this project MyMetaverse and I will save it in my default directory.
  2. Remember that you can choose the directory you prefer as this will not influence the development of the project. In my case and for your reference, I have used the /Users/davidcantonnadales/Projects/MyMetaverse path. If you’re using Windows, it can be something like C:Projects/MyMetaverse:
Figure 1.8 – Setting up the name and location of the new project

Figure 1.8 – Setting up the name and location of the new project

Things to consider when choosing your project name

In some projects I have done, I have had problems with the spaces in the project creation directory. Keep in mind that Unity will create a folder with the same name as the project you have chosen. For example, if we had chosen My Metaverse instead of MyMetaverse, it would create a directory with a blank space. To avoid possible problems later on, I recommend that you avoid blank spaces.

You may have some doubt in your mind as to why we have used the URP rendering method instead of the default one. I have specified the technical benefits we have obtained by selecting the URP rendering method for our project in the following note.

Why did we choose the 3D template (URP)?

Unity allows you to create Universal Render Pipeline (URP) or High Definition Render Pipeline (HDRP) projects so that you can focus on performance or quality rendering.

URP offers better graphics quality than the default Standard Render Pipeline (SRP) built-in system in Unity. URP is designed for casual games for mobile and PC/Mac, offering great compatibility between different devices. On the other hand, there is a superior render pipeline called High Definition Render Pipeline (HDRP) that’s oriented to AAA games for consoles and PC with great features.

If you want to learn detailed technical information about the differences between URP and HDRP in Unity, I recommend that you check out this blog post: https://vionixstudio.com/2022/02/12/urp-vs-hdrp-in-unity.

  1. Once you have clicked the Create button, Unity will take a few seconds to configure and create the new project. It will also load the default plugins. Once everything has finished loading, the editor will open with a default scene. When this whole process is finished, you will see something similar to Figure 1.9:
Figure 1.9 – The appearance of the Unity editor after creating a new project

Figure 1.9 – The appearance of the Unity editor after creating a new project

With that, we have created our first project. As you can see, Unity has created our first blank scene called SampleScene. Before we move on to the next point, let me briefly explain something about the target platforms that Unity offers.

As I commented at the beginning of this book, our project will be compatible with multiple platforms – that is, it can be compiled and exported for Android, iOS, Windows, macOS, and Linux. At the end of this book, we will learn how to do this for each of the platforms.

In Unity, when we create a new project, by default, it is configured to be exported for Mac/Windows. You can view the target platform’s settings by opening the Build Settings menu. On Mac and Windows, you can access this menu by clicking on File in the toolbar and then on the Build Settings menu:

Figure 1.10 – The Build Settings window

Figure 1.10 – The Build Settings window

For now, and until we finish most of the chapters and get to the Cross Platform compilation part, we will not need to change these settings. We will dig into this later.

Basics of using the Unity editor

If this is the first time you’re using Unity, do not skip this section as we will briefly review all the parts that comprise Unity. We will explore the most important menus and begin to learn the basic actions that will lay the foundations of the knowledge necessary for you to advance to the following chapters.

The first and most basic thing to know about Unity is its organization. By default, when we create a new project, we see the Unity editor with five clear divisions called layouts. There are several predefined layouts that we will see later. In addition, we can customize this aspect to our taste and save the configuration.

If we pay attention to the Unity editor, we can distinguish mainly five sections in the layout that load by default. To learn about other layouts, take a look at Windows | Layouts, as shown in the following figure:

Figure 1.11 – The default layout in the Unity editor

Figure 1.11 – The default layout in the Unity editor

Let’s review what the divisions we have numbered in the preceding screenshot are for:

  1. Hierarchy window: All the objects that appear in our scene (GameObjects) are organized as a tree, and they can be relocated and grouped in parent-child mode. There is a root node with the name of the scene; the rest will hang from here. It is here where we will select the object we are going to work with so that we can move it, enlarge it, edit it, and so on.
  2. Scene window: This is the three-dimensional representation of the world we are building. This is where the objects that we have in the Hierarchy window will be represented graphically. When you select an object of the hierarchy, it will be marked in the scene, as shown in the preceding screenshot with the GameObject called Main Camera.
  3. Inspector window: This section displays information about the position, rotation, and scale properties of the selected object in the Hierarchy window. The changes we make to these values will affect the visual representation of the Scene window. Also, in this Inspector window, we can manage the components of an object.

By way of summary, it can be said that components are pieces with unique functionality, which are attached to a GameObject and cause it to acquire a new functionality or feature. These components can also be scripts that we make in C#. We will start programming our own scripts in Chapter 5, Preparing a New World for Travel.

  1. Project window: Here, you will find the folders of the added resources and the assets or plugins that have been installed in our project. For example, here, we will find the images, audio, or 3D objects that we will be using in some of the scenes.
  2. Project window in detail: As a master detail view, this window shows the contents of the folders that were selected in the Project window. The assets that we select here will have their information displayed in the Inspector window.

We already know how a layout is composed in Unity; inside each window of the layout, we can find other functionalities that we can access by changing tabs, as shown in the following figure:

Figure 1.12 – Tabs with other functions in the layout

Figure 1.12 – Tabs with other functions in the layout

If we look at the Scene window in Figure 1.12, we will see that there are two tabs called Scene and Game (1). The Scene tab, which is checked by default, as we mentioned previously, shows the 3D representation of the objects placed in the Hierarchy window. Here, we can move freely with the mouse and keyboard to explore the scene.

In the Game tab, we will see what is focusing and rendering the camera in our scene – in this case, the Main Camera GameObject.

In the Project window, there are two tabs – Project and Console (2). In Project, as we have seen previously, the folders of the resources and assets that we have in our project are shown. In the Console tab, we will see information about errors and logs when we run our project.

On the other hand, we have the main menu bar of Unity, in which we can find the following options:

Figure 1.13 – The Unity editor’s main menu bar

Figure 1.13 – The Unity editor’s main menu bar

Let’s take a closer look:

  • File: Here, you can find the options to create a new scene, save the changes of the current scene, or open another one. You also have options to open or close a project. Finally, in Build Settings, you can change the Target Platform settings, as we saw previously.
  • Edit: Here, we can see different actions, such as ordering or copying and pasting, that we can use for the GameObject of the current scene. Another very important menu that you can access from here is Project Settings, which contains all the settings applicable to our project.
  • Assets: This menu allows you to search, import, and manage your project assets.
  • GameObject: This menu allows you to add new items to your scene. It also gives you advanced options to create them as parents or children of others. There is a list of GameObjects that comes with Unity by default, such as lights, particle systems, cameras, and others.
  • Component: This menu shows a list of built-in components that you can add to your selected GameObject.
  • Window: This menu includes very important options, such as layout management, which we talked about previously. There are also actions to organize the editor panels.

A very important menu that we will see later is called Package Manager. This is an asset manager. When you acquire a new asset or when you want to acquire a new asset, this is where you can find it and add it to your project.

Last but not least, we have the main tools for manipulating the GameObject in the scene, as shown in Figure 1.14:

Figure 1.14 – The main toolbars in the Unity editor

Figure 1.14 – The main toolbars in the Unity editor

Let’s take a look:

  1. Tools: These are the buttons you will use the most in any project you create in Unity. These five buttons are used to rotate, move, transform, and scale the currently selected GameObject.
  2. View options: This bar has display options that affect the current scene, such as enabling or disabling the lighting, changing the camera rendering to 2D/3D, disabling the sound, and so on.
  3. Gizmo: This cross is used to change the viewing perspective in the scene. Here, you can view the scene from above, left, right, or below... just click on the different orientations.

Creating a new scene

Now that we’ve covered a lot of the theory regarding Unity and its interface, it’s time to get down to business. For this, we are going to create our first scene. For now, this scene will only have a floor, but it will serve as a base for the following chapters.

Follow these steps to create a new scene from scratch:

  1. Create a new scene from the File menu by selecting New Scene.
  2. The New Scene creation window will appear. Since our project is being created with URP, select the Standard (URP) option. This will create the scene with the appropriate settings:
Figure 1.15 – Creating a new scene with URP configuration

Figure 1.15 – Creating a new scene with URP configuration

  1. Upon clicking the Create button, you will need to choose the name of your new scene. The scene we are creating will be our main scene, so you could call it MainScene, for example.

The Where option allows you to specify the directory inside the project where you will create the scene. For the moment, you can leave it as-is. We will learn more about how assets are organized in the project later:

Figure 1.16 – Choosing a name for the new scene

Figure 1.16 – Choosing a name for the new scene

  1. After clicking the Save button, the new scene will be created and opened. By default, Unity adds a Camera object, a Light object, and a GameObject called Global Volume, which is in charge of adding post-processing settings to our camera and will make everything look much better.
  2. Now, to create our floor, we will use a Plane object. Click GameObject | 3D Object | Plane.
  3. We already have a floor; you can see that it has been created in the Hierarchy window. Now, you can change the name of the newly created object. For example, you can call it Floor instead of Plane.
  4. Make sure that the ground is at the lowest part of the scene and in the center. For this, modify the position values by clicking on the GameObject from the Hierarchy panel and changing the X, Y, and Z positions to 0, 0, and 0. This can be seen in Figure 1.17:
Figure 1.17 – Changing the position of the floor

Figure 1.17 – Changing the position of the floor

  1. The floor we’ve created is too small – we would like it to be wider so that the character we create later can walk several steps before falling into the void. You can modify the floor’s size by manipulating the Scale values in the Inspector panel.

By default, the panel is created with 1, 1, 1 values; we will change these to 10, 10, 10. As you change these values, you will see how the floor is enlarged.

  1. Finally, save the changes you’ve made to the scene via the File | Save menu. The scene will now look like this:
Figure 1.18 – The new scene with a floor

Figure 1.18 – The new scene with a floor

Getting familiar with Scene View controls

At this point, we already know the basics of the Unity interface, including how to create a new scene and change the Position and Scale properties of a GameObject. At this point, I recommend that you explore the controls to navigate the scene with the mouse and keyboard.

For example, using right-click to pivot the view, press Alt + left-click (Option + left-click on Mac) to orbit the view around the center of the view and center-click the mouse to move the view.

You can also use Alt + right-click (Option + right-click on Mac) or the mouse scroll wheel to move closer or further out. For more detailed information about keyboard and mouse combinations, you can visit https://docs.unity3d.com/Manual/SceneViewNavigation.html.

Advanced object positioning documentation

You can find extended information on how to change the position, rotation, and scale properties of a GameObject at https://docs.unity3d.com/Manual/PositioningGameObjects.html.

Now that you have learned everything you need to install Unity to create new projects and scenes, you know the most important points of the user interface, as well as how to place objects and modify their properties in a scene. Next, we’ll look at a very important technique that will lay the foundation for efficiently organizing files, folders, and assets in your project.

Organizing your project assets

When we talk about organizing assets and resources in our project, we are referring to how we want the folder structure in the Project panel to look. There is not a single established way to do this – it depends on how we like and feel more comfortable working. Unity recommends a series of points you should take into account when doing this, as well as to avoid file problems.

The following are recommendations for organizing our project:

  • Establish a folder and file naming guide before starting.
  • Don’t lose track of the folder naming throughout the project. On many occasions, we will add files and resources and forget to rename them.
  • Do not use blank spaces in folders or individual files.
  • Separate the files to be used in testing from the production files by creating another folder dedicated to them.
  • Always use the Assets root folder; everything you create should hang from this folder. Avoid creating other folders at the same level as the Assets folder.
  • Keep your private files separate from downloaded assets or Unity’s resources.

As we have said before, there is no standard way to organize projects in Unity. I use a technique that helps me have good order and be more efficient when working. Figure 1.19 is the default structure that can be seen when creating a project:

Figure 1.19 – Detailed view of the Project panel

Figure 1.19 – Detailed view of the Project panel

As shown in the preceding figure, under the root Assets folder, we have MainScene, Scenes, Settings, and TutorialInfo. At first sight, this seems like a good form of organization, but in reality, it is not quite like that. Let me explain why.

As the project grows, you will be downloading assets and installing them in the project. This will cause an infinite number of new folders that hang from the Assets folder to be created. These folders are sorted alphabetically, and this can become chaotic if you try to locate your own folders, mixed in among the folders of third parties.

A widespread technique among Unity programmers is to identify your folders with the underscore symbol (_) so that they always appear at the top, as shown in Figure 1.20:

Figure 1.20 – Custom organization

Figure 1.20 – Custom organization

This way, you will no longer care that third-party plugins can create an infinite number of folders in your Assets root folder since you will always have yours at the top of your _App folder.

Within the _App folder, you can create the folder relationship that suits you best. From my experience, most assets can be perfectly organized into materials, scenes, scripts, sounds, or textures.

To finish organizing the project as a whole, we will move the scene we have created to its new location and delete what Unity has created:

  1. Move the MainScene scene file, along with its configuration folder, inside _App | Scenes:
Figure 1.21 – Reorganizing our assets

Figure 1.21 – Reorganizing our assets

  1. Delete the TutorialInfo folder from the root Assets folder.
  2. Delete the old Scenes folder located in the root Assets folder. Inside this is the SampleScene example scene, but don’t worry, we don’t need that either.

Once we have reorganized our assets and removed the sample material, our Project panel will look something like Figure 1.22:

Figure 1.22 – Screenshot of what the new organization should look like

Figure 1.22 – Screenshot of what the new organization should look like

Note

If you want to dive deeper into this matter, I recommend https://unity.com/how-to/organizing-your-project and https://docs.unity3d.com/Manual/SpecialFolders.html.

Good job! With that, you have learned everything you need to have your work well organized. Over time and when you have worked on multiple projects, you will notice the difference if you have your files well organized. As projects grow, if we don’t apply some technique to keep everything organized, we will lose a lot of time looking for the assets we need. Next, we will cover a very easy and interesting point regarding how to configure our project so that later, in Chapter 2, Preparing Our Player, we can move our character with the keyboard and mouse.

Choosing an input handler

The Input Manager in Unity is the area where you define the keys or axes of the keyboard and mouse that will be detected when the user plays the game. These keys and axes can be related to a name and then accessed via code. For example, in one of our scripts, we can ask if the user has made a jump because they have pressed the spacebar.

A few years ago, Unity used an Input Manager system that is now obsolete due to the arrival of a new system called the new Input System.

Unity’s new Input System was created to unify the controls on PC, PlayStation, Xbox, and other systems.

Let’s start by adding the new Input System to our project:

  1. Open Package Manager. This can be found under Window | Package Manager:
Figure 1.23 – Locating the new Input System plugin in Package Manager

Figure 1.23 – Locating the new Input System plugin in Package Manager

  1. Change the source of Packages: In Project to Packages: Unity Registry (1).
  2. Scroll down the list and look for Input System (2) and click the Install button (3).
  3. Unity will display a warning that for the changes to activate the new Input System, the project must be restarted. Click the Yes button to restart the editor.

If you have unsaved changes in the scene, you will see a popup, warning you of this so that you can save it before the editor closes:

Figure 1.24 – Unity notice about applying changes to the project

Figure 1.24 – Unity notice about applying changes to the project

Note

You can find detailed information about the differences between the old and the new system at https://blog.unity.com/technology/introducing-the-new-input-system. This is recommended reading.

Perfect! Our project has been configured to work. Now, it’s time to get to know Firebase and learn about all the services that it offers.

Understanding the different Firebase services

Firebase is a cloud platform for web and mobile application development that’s owned by Google. It has SDKs for a wide range of platforms and technologies, which help you use it in virtually any environment, whether it is on the web with JavaScript, a desktop application written in .NET, a native mobile app on Android or iOS, and so on. Most importantly, it has a huge community, which makes this suite a great ally in any project.

It was created in 2011 and Google acquired it in 2014. Initially, it was only a real-time database. Due to its success, more and more features were added, which, in part, allows us to group the SDKs of Google products for different purposes, making it easier to use.

What is Firebase for?

Its essential function is to simplify the creation of web applications and mobile, desktop, and even REST API projects and their development, allowing for high productivity or helping to make the work faster, but without sacrificing quality.

Its tools cover practically any technical challenge that a complete application must overcome, considering that its grouping simplifies the management tasks to the same platform. The functionalities of these tools can be divided into four groups:

  • Compilation
  • Launch and monitoring
  • Analytics
  • Participation

It is especially interesting because it helps developers not need to spend so much time on the backend, both in terms of development and maintenance issues.

Firebase functionalities

This chapter will help you understand and love Firebase from these very pages. During the journey of our project, we will be connecting features and functionalities of our metaverse in this suite.

Compilation

The Compilation section is where Firebase includes the most important functions that you will use the most. Services such as user identification, file hosting, and databases can be accessed from here. The following is a more detailed review of the most important services in this section:

  • Authentication: Identifying the users of an app is necessary in most cases if they want to access all its features. Firebase offers an authentication system that allows both registration (via email and password) and access using profiles from other external platforms (such as Facebook, Google, Apple, or Twitter), which is a very convenient alternative for users who are reluctant to complete the process.

Due to this, these types of tasks are simplified. This is also because access is managed from Google servers and greater security and data protection can be achieved. It should be mentioned that Firebase can securely store login data in the cloud, which prevents someone from having to identify themselves every time they open the application.

  • App Check: This offers security techniques that protect your token-based applications.
  • Firestore Database: One of Firebase’s most prominent and essential tools is real-time databases. These are hosted in the cloud, are non-SQL, and store data as JSON. They allow the data and information of the application to be hosted and available in real time, keeping them updated even if the user does not perform any action.

Firebase automatically sends events to applications when data changes, storing the new data on disk. Even if a user is offline, their data is available to everyone else, and the changes that are made are synchronized once the connection is restored.

Nowadays, Firebase maintains two real-time databases – Realtime Database and Firestore Database. The latter is a very powerful evolution of the former and allows you to manage indexes and make and apply more complex filters from the SDK. We will use the latter in our project.

  • Extensions: These are prefabricated actions that facilitate automated tasks on your database, such as sending an email when a new item is created, optimizing a photo when a user uploads it, translating text from a database field, and so on.
  • Storage: Firebase has a storage system where developers can store their application files (and link them with references to a file tree to improve app performance) and synchronize them. Like most Firebase tools, it can be customized through certain rules. This storage is a great aid for handling user files (photos they have uploaded), which can be served more quickly and easily. It also makes downloading file references more secure.
  • Hosting: Firebase also offers a server to host the apps quickly and easily – that is, static and secure hosting. It provides SSL and HTTP2 security certificates automatically and free of charge for each domain. It works by placing them on Firebase’s Content Delivery Network (CDN), a network that receives the uploaded files and delivers the content.
  • Functions: This option allows you to deploy functions hosted in Firebase as a backend. These functions can be of the REST type (GET, PUT, DELETE, and POST) or of Trigger type – that is, they are launched automatically when something happens in the database, such as when a new document is added, a value changes in a specific field, or a document is deleted.

Finally, you can also create schedule functions. These are equivalent to cron tasks and can be programmed to be executed in a time range – for example, every 5 minutes, every 24 hours, and so on.

  • Machine Learning: These are pre-built functions that make it easier for you to perform tasks in mobile applications, such as recognizing text in a photo, detecting a face, and tracking objects using the camera.
  • Remote Config: This allows you to dynamically configure your project based on constants that you generate in the cloud. These constants can be numeric values, text, or even store a whole tree in JSON. This tool is very powerful if you want to dynamize the look and feel of your app without needing to recompile and deploy. For example, in these constants, we can store a discount of our shop, a Boolean variable that decides whether to display a banner or not.

Launch and monitoring

This section is where Firebase groups the services related to quality and analysis. Important services such as monitoring and managing bugs in our software with Crashlytics, obtaining performance metrics with Performance, robotically testing app features, and distributing binaries to testers can be accessed from here. The following is a more detailed review of the most important services in this section:

  • Crashlytics: To maintain and improve the quality of the app, special attention must be paid to bugs, so bug tracking (and also the overall performance of the app) is key to being able to act and fix them. For this reason, Firebase offers crash reporting, which detects and helps solve the app’s problems, obtaining a very detailed error report (with data such as the device or the situation in which the exception occurs) and organizing them as it groups them by similarity and classifies them by severity.
  • Performance: This provides statistics on the load times of a mobile app or website. Here, you can find metrics calls to other REST APIs that occur within your code.
  • Test Lab: The Test Lab allows you to test your app on virtual Android devices based on the parameters you configure. This way, it is much easier to detect possible errors before launching the application.
  • App Distribution: This allows you to distribute an APK or AAB to private testers. This makes it much easier to manage new versions. Firebase automatically sends notifications to users when you upload a new version for testing.

Analytics

Have you met or heard about Google Analytics before? Firebase Analytics is practically the same but oriented to mobile apps. With this service, you can get powerful information about the users of your app.

Analyzing data and results is key to making consistent and informed decisions for your project and associated marketing strategy. With Firebase Analytics, you can monitor various parameters and obtain a variety of metrics from a single dashboard for free. It is compatible with iOS, Android, C++, and Unity and, among other functions, allows you to do the following:

  • Obtain measurements and analysis of the events taking place in the application. You can receive unlimited reports with up to 25 attributes.
  • Check the performance of events, notifications, and advertising campaigns in networks, based on user behavior.
  • Get to know the user with segmented information. For example, you can obtain statistical data on language, access devices, age, gender, location, and so on. Insights on usage and loyalty to the app can also be obtained.

Participation

Participation is where Firebase groups the services related to communication or interaction with other users of our app. Push messaging communication, the creation and consumption of Magic Links with Dynamic Links, and A/B testing are some of the most interesting services that we will find here. Let’s look at these in more detail:

  • A/B testing: This allows you to use Remote Config to measure the success of your project on two different variants.
  • Messaging: This is useful for sending notifications and messages to various users in real time and through various platforms.
  • Dynamic Links: These are smart links, which allow the user to be redirected to specific areas or contents of the application, depending on the objective to be achieved and the personalization given to various parameters of this URL.

Thus, the operation of these links is directed as we want and ensures a pleasant experience for the user on various platforms. They are particularly useful for directing content to certain segments of users, whether they’re current or potential, in which case they may receive a recommendation to install our app.

  • AdMob: This allows you to monetize your mobile apps easily and intuitively; with a few lines of code, you can display banners and ads automatically.

Advantages and disadvantages of using Firebase

You will probably agree with me that this tool has many benefits for developers who use it. We will add below an interesting link with official documentation and examples, ideal to broaden your knowledge of the services mentioned above.

Here is a list of the benefits of Firebase:

  • Highly recommended for applications that need to share data in real time.
  • Its functionalities complement each other very well and can be easily managed from a single panel. Moreover, it is not necessary to use all these options for the application, and you can choose only those that interest you most.
  • They have official SDKs for client applications developed in iOS, Android, JavaScript, C++, and Unity. They also have libraries to integrate into an API for the NodeJS, Java, Python, and GoLang languages. Finally, although they do not have official support, there are libraries for frameworks such as Angular, Ember, Flutter, React, React Native, Rx, and Vue that are recommended on their website.
  • It allows you to send notifications. They are very simple to implement and manage, and they are also extremely useful for maintaining communication with users.
  • Allows monetization – that is, from Firebase itself, advertising can be added to the app, allowing it to be easily monetized.
  • Includes Analytics, which specializes in certain mobile application metrics and is integrated into the Firebase central panel with a very intuitive operation.
  • Google offers abundant introductory and informative documentation (in great depth) to make diving into Firebase much easier.
  • Free email support, regardless of whether the developer uses the free or paid version.
  • It is scalable – that is, the startup is free but allows it to adapt to the needs of the application with different payment plans.
  • Offers security to users with SSL certificates.
  • Allows developers to take their attention away from the backend and complex infrastructures to focus entirely on other aspects.

The most mentioned disadvantage is Firebase’s price. We have already discussed the scalability of Firebase, where starting with the Spark plan is free. However, it has limitations (mainly regarding the number of concurrent users and storage space), so it may be necessary to purchase a paid version. The paid plans are Flame ($25 per month) and Blaze (pay-as-you-go).

As a side note, in the many years that I have been using Firebase, I have deployed more than 50 projects, some small, some medium, and some larger, and I can say that I have received invoices of a few cents or on occasion less than $10. This is an insignificant amount, in my opinion, for the stability, quality, and monitoring of the services offered.

You can always set up budget alerts and decide to stop the service when your budget is exceeded – you will receive an email when you are approaching your limit.

Note

You can find technical documentation and tutorials by browsing to https://firebase.google.com/docs.

At this point, you know what Firebase can offer us. As you have seen, it is a suite full of fascinating services. Can you imagine how all this will help us in our impressive metaverse? From Chapter 3, Preparing Our Home Sweet Home: Part 1, we will perform various actions and integrate our project with Firebase services, step by step and progressively. Now, let’s get down to business and create our project in the Firebase console.

Configuring a new Firebase project

Firebase will be our traveling companion throughout this book. It is like a Swiss Army knife that will provide us with solutions to technical problems, such as data persistence in the cloud, dynamic configuration, hosting user-generated files, identifying users in our system, and much more.

To explore the tools offered by this Google suite, we must create an account for free:

  1. Navigate to https://console.firebase.google.com.
  2. Since Firebase is a Google service, you will need to use your Gmail account to log in. If you are already logged in with Gmail, you will be taken directly to the welcome screen; otherwise, you will be prompted to log in. Once logged in, you will see the following screen:
Figure 1.25 – The welcome page in the Firebase console

Figure 1.25 – The welcome page in the Firebase console

  1. Click on the Create a project button. At this point, we will be asked for the name of the project. We can use the same name we used for our project in Unity:
Figure 1.26 – Setting up the name of your project in Firebase

Figure 1.26 – Setting up the name of your project in Firebase

  1. After you have entered the name of your project, you can click the Continue button.
  2. In step 2 of 3, the wizard will ask you if you want to activate Google Analytics. I recommend enabling the Enable Google Analytics for this project option as it will be very useful in the future.
  3. When you click on Continue, it will ask you the location that your Analytics account will have. Google Analytics will ask you to select an active account or create a new one, if you do not have one, you can select the Create new account option, in this case, after choosing an account name, accept the Google Analytics terms of use and check the Use the default settings to share Google Analytics data box to make the account creation easy and click the Continue button.
  4. Firebase will take a few seconds to finish configuring the project, so be patient. Once the process has finished... congratulations! You have successfully created your first Firebase project:
Figure 1.27 – The home page of the new Firebase project

Figure 1.27 – The home page of the new Firebase project

Great! With that, you know all the theory behind Firebase’s services and created your first project in the console. Now, let’s go one step further and download the Firebase SDK for Unity. This is a very important first step so that in future chapters, where we need to program our scripts to access Firebase, we have done all the necessary work already and we won’t have to stop.

Installing the Firebase SDK

We have reached the last part of this chapter; after this step, we will focus more on practice and less on theory. We have already seen all the benefits that Firebase offers; after this section, you will understand the reason you should use it in our project.

Next, we are going to follow a few simple steps to download and install the Firebase SDK in our project. To do this, execute the following steps:

  1. Navigate to https://firebase.google.com/download/unity.
  2. The Firebase SDK will automatically start downloading. If this does not happen, look for a hyperlink that looks like what’s shown in Figure 1.28:
Figure 1.28 – Manual download button

Figure 1.28 – Manual download button

  1. It may take some time to download as it is approximately 940 MB. In the folder where your downloads land, you will find a ZIP file named firebase_unity_sdk_X.X.X.zip. In my case, I have downloaded the firebase_unity_sdk_10.1.1.zip version.
  2. When unzipping, a new folder called firebase_unity_sdk will appear, the contents of which are shown in Figure 1.29:
Figure 1.29 – Firebase SDK files for Unity

Figure 1.29 – Firebase SDK files for Unity

  1. This is fantastic! As you can see, the SDK comes fragmented by services. This will help us integrate only what we are going to need and expand in the future if we need new functionality that requires it. We will avoid having plugins and unnecessary code in our project.
  2. To get started in the following chapters, we will need to install FirebaseAuth.unitypackage, FirebaseFirestore.unitypackage, and FirebaseStorage.unitypackage. When we need more functionality in our project, we will go back to this folder and install what we need.
  3. Double-click on FirebaseAuth.unitypackage to install it. A modal window will open in Unity, warning us about the import and showing us which files are going to be created. Click on the Import button:
Figure 1.30 – Unity’s new asset import dialog

Figure 1.30 – Unity’s new asset import dialog

  1. When the progress bar finishes, do the same with the FirebaseFirestore.unitypackage and FirebaseStorage.unitypackage files.

Once you’ve finished installing these, you will see that a new folder has been created in your project called Firebase, which contains the internal libraries needed to use it in the code we will program later:

Figure 1.31 – New Firebase folder and configuration warning

Figure 1.31 – New Firebase folder and configuration warning

As you may have seen, a yellow warning about a missing configuration will appear. You might be wondering, Once I have imported the SDK and I have created my project in the Firebase console, how can I connect the Unity project with the Firebase console? This is done with the google-services.json file for Android and the GoogleService-Info.plist file for iOS. For Windows, Mac, and Linux, Firebase will take the google-services.json mobile file and automatically generate one called google-services-desktop.json the first time you run the project after importing the files.

  1. To generate these files, go to the Firebase console at https://console.firebase.google.com.
  2. On the main screen, click on the Unity icon. You can also access it from the gear button and click on the Project settings option:
Figure 1.32 – Preparing Unity’s configuration in the Firebase console

Figure 1.32 – Preparing Unity’s configuration in the Firebase console

  1. You will be presented with a form that contains simple information that you must fill in to continue.
  2. Check the Register as an Apple app check box and enter an Apple Package ID property of your choice. Here, I have entered com.mymetaverse.app (it must be in inverted domain format, which is comprised of three parts – xxx.xxx.xxx).
  3. Check the Register as Android app check box and enter an Android Package Name property. You can use the same one you entered for iOS.
  4. Click on the Register App button.
  5. Once this process has finished, you will be prompted to download the two files that we had previously named. The remaining steps of the wizard do not require further action, so you can click on the Continue button until it is finished:
Figure 1.33 – Downloading the configuration files for the Unity project

Figure 1.33 – Downloading the configuration files for the Unity project

  1. Finally, drag the two files (google-services.json and GoogleService-Info.plist) to the root of your Unity project, just inside the Assets folder:
Figure 1.34 – Adding the configuration files to the Assets folder

Figure 1.34 – Adding the configuration files to the Assets folder

Congratulations! With this last step, we have perfectly configured our project with Firebase. Finally, I would like to warn you about the yellow warning that appeared before. This will not disappear on its own – you have to press the Clear button that is located in the Console panel.

Congratulations! You have finished configuring Firebase and connected it to our project. I know that this part may seem tedious because it is very theoretical and focuses on configuration tasks, but it is tremendously necessary to be able to advance without obstacles in the following chapters.

Having connected the project in the Firebase console to our Unity project, we will be able to start programming functionalities that attack our Firebase database directly, for example. We will be able to integrate a sign-in/sign-up system for our users, make queries to the database, and much more.

Summary

In this chapter, we learned the basics of working with Unity, from downloading and installing it to understanding and putting the basic concepts into practice. We already know about the main parts of the Unity interface, and we will learn about them more in the future. We will expand our knowledge of the game engine by exploring new features, menus, and tricks.

At this point, we have our project with URP created and configured so that we can advance correctly in the following chapters. On the other hand, we have learned about what Firebase is and how its services can help us in multiple ways.

In addition, we learned how to configure a project from scratch in the Firebase console, which allowed us to connect it to our Firebase project, as well as how to download and install the SDK.

In the next chapter, we will learn how to create an animated 3D character that we will be able to make move, jump, and run by pressing certain keys, as well as look around by moving the mouse. Fascinating, isn’t it? See you in Chapter 2, Preparing Our Player.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Create your own Metaverse from scratch with detailed and practical instructions
  • Use Firebase tools to create a distinctive and feature-rich virtual world
  • Compile and distribute the finished project to various platforms
  • Purchase of the print or Kindle book includes a free PDF eBook

Description

Build Your Own Metaverse with Unity is a practical guide for developers with step-by-step explanations, practical examples, and screenshots to guide them through essential concepts. You’ll start by learning how to install Unity and create a character that can move around the virtual world. Next, you’ll learn how to integrate multiplayer functionality, user identification, and registration in your game universe. You’ll explore how to create interactive characters dealing with different attributes of the virtual world, including acquiring homes and decorating them with various features. Additionally, you’ll understand how to use Firebase tools to make your Metaverse unique. By following the expert advice, you’ll be able to create a feature rich Metaverse where players can interact with NPCs, acquire homes, and interact with other players online. Finally, you’ll discover how to compile and distribute the finished project to various platforms, while acquiring with necessary skills and knowledge to create a unique and groundbreaking Metaverse. By the end of this book, you’ll be well-equipped to create a Metaverse with infinite possibilities.

What you will learn

Master Photon SDK to construct a universe with multiplayer capabilities Familiarize yourself with the benefits of Firebase services when working with Unity3D Store data and settings in a cloud-based database Acquire the skills to create NFTs using Unity3D and C# Explore dynamic object generation from cloud data and manage game character mechanics Incorporate Oculus quest 2 into your Metaverse Allow gamers to register or log in through Firebase authentication for safe gameplay

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Sep 15, 2023
Length 586 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781837631735
Vendor :
Unity Technologies
Category :
Concepts :

Table of Contents

20 Chapters
Preface Chevron down icon Chevron up icon
Part 1: Getting Started Chevron down icon Chevron up icon
Chapter 1: Getting Started with Unity and Firebase Chevron down icon Chevron up icon
Chapter 2: Preparing Our Player Chevron down icon Chevron up icon
Chapter 3: Preparing Our Home Sweet Home: Part 1 Chevron down icon Chevron up icon
Chapter 4: Preparing Our Home Sweet Home: Part 2 Chevron down icon Chevron up icon
Chapter 5: Preparing a New World for Travel Chevron down icon Chevron up icon
Part 2: And Now, Make It Metaverse! Chevron down icon Chevron up icon
Chapter 6: Adding a Registration and Login Form for Our Users Chevron down icon Chevron up icon
Chapter 7: Building an NPC That Allows Us to Travel Chevron down icon Chevron up icon
Chapter 8: Acquiring a House Chevron down icon Chevron up icon
Chapter 9: Turning Our World into a Multiplayer Room Chevron down icon Chevron up icon
Chapter 10: Adding Text and a Voice Chat to the Room Chevron down icon Chevron up icon
Part 3: Adding Fun Features Before Compiling Chevron down icon Chevron up icon
Chapter 11: Creating an NPC That Allows Us to Change Our Appearance Chevron down icon Chevron up icon
Chapter 12: Streaming Video Like a Cinema Chevron down icon Chevron up icon
Chapter 13: Adding Compatibility for the Meta Quest 2 Chevron down icon Chevron up icon
Chapter 14: Distributing Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.