Reader small image

You're reading from  Build Your Own Metaverse with Unity

Product typeBook
Published inSep 2023
PublisherPackt
ISBN-139781837631735
Edition1st Edition
Tools
Concepts
Right arrow
Author (1)
David Cantón Nadales
David Cantón Nadales
author image
David Cantón Nadales

David Cantón Nadales, software engineer from Seville, Spain, with more than 15 years of experience. He is currently a Technical Leader at Grupo Viajes El Corte Inglés, a leading travel company in Europe. He has done a multitude of projects and video games with Unity, VR/AR with Oculus, Hololens, HTC Vive, DayDream and LeapMotion. He was ambassador of the Samsung community "Samsung Dev Spain", and organizer of "Google Developers Group Sevilla". He led more than 100 projects throughout his career. As a social entrepreneur stands out Grita, a social network that emerged during the confinement of COVID-19 that allowed to talk to other people and help each other psychologically. In 2022 he won the Top Developers Awards organized by Samsung.
Read more about David Cantón Nadales

Right arrow

Preparing a New World for Travel

After having designed an impressive meeting point in Chapter 3, Preparing Our Home Sweet Home: Part 1, we will need another scene for our users to travel to. In this second world we are going to design, we will focus on creating a neighborhood-type scene, with houses, sidewalks, lampposts, and some more details. The future intention of this scene is that the players will be able to purchase these houses. We will see this in Chapter 8, Acquiring a House.

The big difference in this chapter, compared to Chapters 3 and 4, is that the buildings will be loaded dynamically in the scene; that is to say, we will create records in the Firebase database with references to Prefabs in the project, and when executing the scene, they will be created.

This offers us great versatility to have a dynamic world. Loading one building Prefab or another will be customizable from the database.

We will cover the following topics:

  • Designing the scene
  • Adding...

Technical requirements

This chapter does not have any special technical requirements, but as we will start programming scripts in C#, it would be advisable to have a basic knowledge of this programming language. We need an internet connection to browse and download an asset from the Unity Asset Store.

We will continue with the project we created in Chapter 1, Getting Started with Unity and Firebase. Remember that we have a GitHub repository, https://github.com/PacktPublishing/Build-Your-Own-Metaverse-with-Unity/tree/main/UnityProject, which contains the complete project that we will work on here.

You can also find the complete code for this chapter on GitHub at: https://github.com/PacktPublishing/Build-Your-Own-Metaverse-with-Unity/tree/main/Chapter05

Designing the Scene

We have finally arrived at the chapter where we will start programming. In addition to designing a nice scene that will serve to attach houses to our world, we will add functionalities through C# scripts. These functionalities will give the scene the power to obtain buildings dynamically from our Firestore database. But what does this mean?

We will create functionality that will help us to manage the slots in the scene that can contain a building. This way, we will not have to perform the tedious task of creating dozens of records in the database.

We, as creators of worlds, will design a scene resembling a neighborhood. In the holes where the houses will go, we will create a special object with a script in C# that will register and update these slots, linking them to Firebase.

Whether we press Play in the scene or when the metaverse is compiled and published, when the user enters the game, these scripts will connect to the database, consult the building...

Adding the world to the Firebase database

I know you are eager to start programming; I promise to be quick in this section. We will simply do the same as in Chapter 4, Preparing Our Home Sweet Home: Part 2, we will add a new world to our Worlds collection.

To do this, follow these steps:

  1. Go to Firebase Console at https://console.firebase.google.com.
  2. Click on the Firestore Database link.
  3. In Firestore Console, select the Worlds collection and click on the Add Document button.
Figure 5.31 – Adding a new world document to the Worlds collection

Figure 5.31 – Adding a new world document to the Worlds collection

  1. In the document creation window, click on the Auto-ID button to obtain a unique ID and add the Description property with the value The first neighborhood of my metaverse, the Name property with the value World1, and the Scene property with the value World1, as shown in the following screenshot.
Figure 5.32 – Filling in the new document form

Figure 5.32 – Filling in the new document form

    ...

Adding objects dynamically

It is time to get into programming. In this section, we will create C# scripts that will allow us to connect to the database and create or update records. The intention of these scripts is to make our scene dynamic. How can it benefit us to have a scene with dynamic access to a database?

Consider that, in the future, when your Metaverse is in production, with hundreds of users, you may want to implement a system that allows your users to improve their houses. To achieve this, the scripts we are going to create in the next pages will follow this flow:

  1. They will connect to the database and query the building we want instances of in the scene.
  2. If our user improves their building, we will update the property where the Prefab is stored, for the new building type.
  3. In this way, we can dynamically load one Prefab or another in the scene depending on the modifiable value that is outside the project in Firebase.

Impressive, isn’t it...

Optimizing a dynamic Scene

We are coming to the end of the chapter, but we can’t end without recalling the benefits of optimizing the scene. This scene is not very different from the one we designed in Chapter 4, Preparing Our Home Sweet Home: Part 2. It has static elements too. The only difference is that the Prefabs are added at runtime.

It is important that you apply the same optimization techniques that we saw in Chapter 4, Preparing Our Home Sweet Home: Part 2. We have programmed these dynamic elements to be instantiated statically, so we are already applying optimization in terms of lights.

Although these buildings are added later, when the scene is run, the Camera Occlusion Culling optimization will remove and display them depending on whether or not they are in the viewing area.

This type of scene may result in less FPS due to not having baked shadows on the dynamic buildings, but it is not a concern. I recommend that you analyze the FPS as your scene grows...

Summary

In my humble opinion, I think this has been a very fruitful chapter. We have learned a lot in these pages. After designing a new scene from scratch and registering it as a new record in the Firestore database, we (finally) learned how to program several scripts.

In these scripts, and in a very didactic way, we learned how to use the Firebase Authentication SDK to identify ourselves as administrators and later create another script that, through the Firestore SDK, we have been able to read and write from the Unity Editor.

The lesson learned here is that there are infinite possibilities in front of us, having learned how to program scripts that communicate with Firebase services, which will have awakened in your mind a storm of ideas and new possibilities.

In the next chapter, we will learn how to integrate another great feature of Firebase, Firebase Authentication. We will include a system that will allow users to log in or create a new account. For this, we will design...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Build Your Own Metaverse with Unity
Published in: Sep 2023Publisher: PacktISBN-13: 9781837631735
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.
undefined
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 $15.99/month. Cancel anytime

Author (1)

author image
David Cantón Nadales

David Cantón Nadales, software engineer from Seville, Spain, with more than 15 years of experience. He is currently a Technical Leader at Grupo Viajes El Corte Inglés, a leading travel company in Europe. He has done a multitude of projects and video games with Unity, VR/AR with Oculus, Hololens, HTC Vive, DayDream and LeapMotion. He was ambassador of the Samsung community "Samsung Dev Spain", and organizer of "Google Developers Group Sevilla". He led more than 100 projects throughout his career. As a social entrepreneur stands out Grita, a social network that emerged during the confinement of COVID-19 that allowed to talk to other people and help each other psychologically. In 2022 he won the Top Developers Awards organized by Samsung.
Read more about David Cantón Nadales