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

Adding Text and a Voice Chat to the Room

In the previous chapter, we discovered what Photon offers us to turn our project into a multiplayer world. But the Photon SDK has much more to offer.

Integrating voice and audio chat with Unity is very easy thanks to its SDK. Throughout this chapter, we will learn how to create a voice chat and text chat project in the Photon dashboard, which we will then connect to our Unity project for further development.

By the end of this chapter, your wonderful metaverse will have two amazing new features for your users, a text chat and a voice chat, which will allow connected users to chat with each other.

We will cover the following topics:

  • Getting started with Photon Chat
  • Creating a chat window
  • Getting started with Photon Voice
  • Transmitting your voice to the network

Technical requirements

This chapter does not have any special technical requirements, but as we will work with 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 working on 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/Chapter10

Getting started with Photon Chat

Now, we will discover Photon Chat; we will learn practically how to create a chat window that is connected to the room where the player is and we will test its functioning later.

Introduction to the Photon Chat SDK

The Photon Chat SDK is a real-time chat solution developed by Photon Engine. It is a set of tools and services that allows developers to easily add real-time chat functionality to their Unity 3D games.

This SDK provides essential features for real-time chat, such as public and private messaging. It is surprisingly easy to integrate into a Unity 3D project, as Photon provides a Unity 3D plugin and detailed documentation to guide developers.

By using the Photon Chat SDK in your Unity 3D project, you can enhance your users’ gaming experience by allowing them to interact and communicate in real time. This can be especially useful for online multiplayer games, where communication is essential for a smooth and successful gaming...

Creating a chat window

Personally, I like to optimize tasks as much as possible. A lot of the time, we waste a lot of time creating things from scratch when they already exist or we can simply reuse something else. This is the case with the chat feature we are going to implement.

We could spend more than 100 pages creating a chat interface from scratch, but what if Photon already gave us a good basis to start with?

Figure 10.4 – Photon Chat example scene

Figure 10.4 – Photon Chat example scene

As you can see in the preceding screenshot, Photon has done much of the work for us. This screenshot is of the chat interface that can be found in one of their example scenes. Specifically, you can test it by opening the DemoChat scene found in Assets | Photon | Photon Chat | Demos | DemoChat.

The strategy we are going to follow is to reuse this wonderful window that Photon offers us. We will remove the functionalities we don’t need and, in this way, we will have the chat screen finished...

Getting started with Photon Voice

We have managed to integrate a chat system into our project, but in this book, we want to go further. Now, we also want our users to be able to communicate by voice, which is also provided by Photon with its Photon Voice SDK.

As with text chat, we will enable a special key to activate the microphone and be able to talk. Are you ready? First of all, let’s have a short introduction to the Photon Voice SDK.

Introduction to the Photon Voice SDK

The Photon Voice SDK is a Unity tool that enables real-time voice integration in multi-user applications and games. With the Photon Voice SDK, developers can add voice communication to their Unity projects with ease, allowing users to talk to each other during gameplay.

Some of the advantages of using the Photon Voice SDK in a Unity 3D project are as follows:

  • Easy integration: The Photon Voice SDK is easy to integrate into any Unity 3D project. Developers can quickly add real-time voice...

Transmitting your voice to the network

Transmitting our voice to the multiplayer server of our metaverse is really easy with Photon. The SDK has ready-to-use components that will make all the hard work easier.

To start, we need to find the Prefab we’ve used for our character. Find the PlayerInstance Prefab, which is located in the Assets | _App | Photon | PhotonUnityNetworking | Resources path, and double-click to edit it.

With the PlayerInstance Prefab in Edit Mode, we will follow these steps to add voice functionality:

  1. Select the PlayerArmature GameObject and display its Inspector panel.
  2. Add a new component called Audio Source.
  3. Add another component, called Photon Voice View.
  4. We will also need another component, called Recorder.
  5. Finally, add the Speaker component.

Great, now the Inspector panel of the PlayerArmature GameObject will look as in the following screenshot:

Figure 10.15 – PlayerArmature Inspector panel

Figure 10.15 – PlayerArmature Inspector...

Summary

Throughout this exciting chapter, we learned a lot about what Photon can offer us in terms of multiplayer communication. With Photon Chat, we discovered how easy it is to provide a professional chat service in our project, from downloading the SDK to configuring and programming the scripts needed for operation. We also learned a lot about the Photon Voice SDK and added, in a very few steps, fast and effective communication functionality to our project.

In the next chapter, we will continue to add exciting features to our metaverse, expanding its characteristics. We will add another NPC that will allow us to modify our appearance, that is, choose another avatar.

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