Back to Blog

Adding Unity Voice Chat to a Multiplayer Cross-Platform Game

Adding Unity Voice Chat to a Multiplayer Cross-Platform Game

Ever built a cross-platform mobile multiplayer game in Unity and wanted to add in some custom group video chat capabilities? If you answered yes, first let me tip my hat to you because that's a big task! If you answered no (or yes but are still interested in Unity voice chat as you play), today is your lucky day! I will spend some time explaining how to easily add cross-platform video communication to your existing Unity project.

For those that don’t have a multiplayer game but are still interested in Unity multiplayer chat? Well, don’t worry because you’ve come to the right place. I chose to use a pre-builtĀ Tanks!!! reference projectĀ (based on Unity’s popularĀ Tanks tutorial) which has a niceĀ set of video tutorialsĀ for those interested in learning more about the game build.

Before we get started on building Unity voice chat for multiplayer platforms, there are a few prerequisites for anyone reading this article.

Prerequisites

Project Setup

If you plan to use your own existing project to build your Unity voice chat, go ahead and open it now and skip down to ā€œIntegrating Group Voice Chatā€.

For those readers that don’t have an existing project, keep reading (the next few sections are for you).

New UnityĀ Project

Please bear with me as the basic setup has a few steps and I’ll do my best to cover it swiftly with lots of images. Let’s start by opening Unity, creating a blank project.

I named my project Tanks!!! withĀ Agora

If you’ve previously downloaded the Tanks!!! asset package, you can selectĀ Tanks!!!Ā using the ā€œAdd Asset Packageā€ button.

If you don’t have the asset downloaded, create a new project andĀ download it from the Unity Store.

Download and import theĀ project.

There are a couple more steps to getting the Tanks!!! reference project ready for building on mobile. First, we need to enable Unity Live Mode for the project through the Unity dashboard. (select project → Multiplayer → Unet Config).

Set max players to 6 even though Tanks!!! limits games to 4 players and clickĀ save.

Once Unity Live Mode isĀ enabled.

Building forĀ iOS

Now that we have Unity’s multiplayer enabled, we are ready to build the iOS version of the online video chat. Let’s start by opening our Build Settings and switch our platform to iOS and build the project for testing.

After switching platforms to iOS make sure to update the Bundle Identifier.

Please note: You need to have Xcode installed and set up before attempting to build the Unity voice chat project for iOS.

When building for the first time, I create a new folder ā€œBuildsā€ and save the build asĀ iOS.

After the project has successfully been built for iOS, we will see the project in theĀ BuildsĀ folder.

Let’s openĀ Unity-iPhone.xcodeproj, sign, and build / run on our test device.

I enabled automatic signing to simplify the system signingĀ process.

Don’t start celebrating just yet. Now that we have a working iOS build, we still need to get the Android build running.

Building forĀ Android

Android is a bit simpler than iOS since Unity can build, sign, and deploy to Android without the need to open Android Studio. For this section I’m going to assume everyone reading this has already linked Unity with their Android SDK folder. Let’s start by opening our Build Settings and switching our platform to Android.

Before we try to ā€œBuild and Runā€ the project on Android we need to make a couple adjustments to the code. Don’t worry, this part is really simple: we only need to comment out a few lines of code, add a simpleĀ returnĀ statement, and replace one file.

Some background:Ā The Tanks!!! Android build contains theĀ EveryplayĀ plugin for screen recording and sharing your game session. UnfortunatelyĀ Everyplay shut downĀ in October 2018 and the plugin contains some issues that if not addressed will cause the project to fail to compile and to quit unexpectedly once it compiles.

The first change we need to make is to correct a mistake in the syntax within the Everplay plugin’sĀ build.gradleĀ file. Start by navigating to our project’sĀ PluginsĀ folder and click into theĀ AndroidĀ folder and then go into theĀ everyplayĀ folder and open theĀ build.gradleĀ file in your favorite code editor.

Now that we have the Gradle file open, select all and replace the default with the code below. The team that built Tanks!!! updated the code on GitHub, but for some reason it didn’t make its way into the Unity Store plugin.

view raw on GitHub

The last change we need to implement is to disable EveryPlay. Why would we want to disable EveryPlay, you may ask. That's because when the plugin tries to initialize itself it causes the Android app to crash. The fastest way I found was to update a couple lines within theĀ EveryPlaySettings.cs, (Assets → Plugins → EveryPlay → Scripts) so that whenever EveryPlay attempts to check if it’s supported or enabled, we returnĀ false.

view raw on GitHub

(Assets → Plugins → EveryPlay → Scripts → EveryPlaySettings.cs)

Now that EveryPlay works, we are finally ready to build the project for Android! Within Unity open theĀ Build SettingsĀ (File > Build Settings), selectĀ AndroidĀ from theĀ Platform list, and clickĀ Switch Platform. Once Unity finishes its setup process, open theĀ Player Settings. We need to make sure our Android app also has a unique bundle id, I choseĀ com.agora.tanks.voicedemo.

Integrating VoiceĀ Chat

For this project I chose to use the Agora Voice SDK for Unity, because it makes implementation into our cross-platform mobile project really simple.

Let’s open up the Unity Store and search for ā€œAgora voice SDKā€.

Once the plugin page has loaded, go ahead and clickĀ Download.Ā Once the download is complete, click andĀ ImportĀ the assets into your project.

We need to create a script to interface between our game and the Agora Voice SDK. Open the LobbyScene, select the PersistantUIAudio Game Object, and add a C# Script component, AgoraInterfaceScript.cs. Next, let’s open the C# file in Visual Studio.

There are two important variables that we need to declare:

static IRtcEngine mRtcEngine;
public static string appId = "Your Agora AppId Here";

Be sure to replaceĀ Your Agora AppID HereĀ with anĀ Agora AppIDĀ fromĀ your Agora developer dashboard. TheĀ mRtcEngineĀ needs to be a static so the reference doesn’t get lost when it gets called duringĀ OnUpdate.Ā Also theĀ appId should beĀ publicĀ andĀ staticĀ so that we can reference it from other scripts within the game.

In the interest of time and keeping this article concise, I’ve included the code of theĀ AgoraInterface.csĀ below. Let's skim through it together quickly.

First we notice that on Start, we include some logic to check/request the Permissions for Android. Next we initialize the Agora RTC Engine using ourĀ appIdĀ and then we attach some callback functions for the various engine events (pretty straightforward and self explanatory). The main one we care about is mRtcEngine.OnJoinChannelSuccess, as this one will log to the console when we’ve successfully joined the channel.

The final critical function is theĀ Update. While the Agora RTC Engine is active (not null) we want to call the engine’sĀ .Poll()Ā method. This is crucial for the plugin to work!

view raw on GitHub

This code is generic and can be applied to any Unity project that includes the Agora Voice SDK.

You may have noticed that we are missing methods to actuallyĀ Join/LeaveĀ the channel. Let’s start with leaving the channel, create a new C# script,Ā LeaveHandler.cs,Ā and we want theLeaveHandlerĀ to get called whenever the user returns to theĀ Main Menu.Ā The simplest way is to tap into theĀ OnEnablemethod for one of theĀ Game ObjectsĀ that gets enabled when that part of theĀ LobbySceneĀ is activated.

view raw on GitHub

TheĀ Game ObjectĀ we are looking for isĀ LeftSubPanel. It's a child ofĀ MainPanel, which is a child ofĀ MenuUI.

Within the Tanks!!! game there are two ways to join a multiplayer game, eitherĀ Create a GameĀ orĀ Find a Game. These are the two areas where we want to add in our command to join the channel.

Let’s start in order, navigate into the UI Scripts folder (Assets → Scripts → UI) and openĀ CreateGame.cs. If we scroll down toĀ line 61Ā we can see that this is where the game does the matchmaking, and we can add some logic to connect to the channel. Start by including the Agora SDK library.

using agora_gaming_rtc;

Within theĀ StartMatchmakingGame()Ā starting onĀ line 78Ā we’ll add in some logic to get a reference to the Agora RTC Engine (that's currently running), and then use the user input (m_MatchNameInput.text) as the channel name.

view raw on GitHub

StartMatchmakingGame() including call to join channel

Now we need to open theĀ LobbyServerEntry.csĀ (Assets → Scripts → UI) and add logic for users thatĀ Find a Game to also be able to join the same channel.

Let’s openĀ LobbyServerEntry.csĀ in Visual Studio and scroll down toĀ line 63, where we can see theĀ JoinMatch(). Starting at line 80 let's add some code similar to that above.

view raw on GitHub

So, that'sĀ it!

We’ve integrated the Agora SDK, and now we are ready to build and test the multiple user game with Unity chat on both iOS and Android. We can follow the same steps we did above, for building and deploying!

Thanks for following along, feel free to leave a comment or question below!

Other Resources

RTE Telehealth 2023
Join us for RTE Telehealth - a virtual webinar where we’ll explore how AI and AR/VR technologies are shaping the future of healthcare delivery.

Learn more about Agora's video and voice solutions

Ready to chat through your real-time video and voice needs? We're here to help! Current Twilio customers get up to 2 months FREE.

Complete the form, and one of our experts will be in touch.

Try Agora for Free

Sign up and start building! You don’t pay until you scale.
Try for Free