Adding Video Communication to A Multiplayer Unity Game
Tags: unity3d, agora, game, videochat
Published: 2020-06-04 02:15:22

Do you ever imagine when you play against your friends on a multiplayer game on your lovely mobile device, you want to see each other’s facial expression or tease each other with jokes and funny faces? You’ve found a solution here without leaving the game itself to another chat App. In this tutorial we are going to take Unity’s popular Tanks game to the next level and make it into a game with live video chats!

Before we get started, there are a few prerequisites for anyone reading this article.

## Prerequisites

* [Unity](https://store.unity.com/) (2018) and a [Unity Developer account](https://developer.cloud.unity3d.com/)

* Knowledge of how to build your Unity project for [iOS](https://unity3d.com/learn/tutorials/topics/mobile-touch/building-your-unity-game-ios-device-testing) and [Android](https://unity3d.com/learn/tutorials/topics/mobile-touch/building-your-unity-game-android-device-testing)

* A cross-platform mobile multiplayer Unity game (I chose to use [Tanks!!!](https://assetstore.unity.com/packages/essentials/tutorial-projects/tanks-reference-project-80165))

* An understanding of C# and [scripting within Unity](https://unity3d.com/learn/tutorials/topics/scripting/coding-unity-absolute-beginner)

* An [Agora.io developer account](https://dashboard.agora.io/)

* At least two mobile devices (*one iOS & one Android is ideal*)

## Project Setup

If you plan to use your own existing Unity project, go ahead and open it now and skip down to “[Integrating Group Video Chat](https://medium.com/p/73d8f8e3b2f9#2d94)”.

For those readers that don’t have an existing project, keep reading; *the next few sections are for you*. (Note, this step is exactly the same to the Project Setup you may find in or have done most of the set up by following Hermes’ “Adding Voice Chat to a Multiplayer Cross-Platform Unity game” tutorial.)

## 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 recommend starting this project with the latest Unity 2018 LTS version. Note that the networking module UNet has been deprecated in 2019. So 2018 LTS is essentially the best option here.

![Create a new project from Unity Hub](https://cdn-images-1.medium.com/max/2490/1*aprQAZ6TxUUbBwZ5Q1MBOQ.png)*Create a new project from Unity Hub*

[Download and import the “Tanks!!! Reference Project” from the Unity Store](https://assetstore.unity.com/packages/essentials/tutorial-projects/tanks-reference-project-80165):

![Searched by “Tanks Reference” and download this asset](https://cdn-images-1.medium.com/max/2000/1*y89AICjom4yxtr4t5BMqAQ.png)*Searched by “Tanks Reference” and download this asset*

When Unity prompts for if you want to overwrite the existing project with the new asset, click Yes. Furthermore, accept the API update prompt that will come up next.

![](https://cdn-images-1.medium.com/max/2000/1*8NmzhRsXlv7sc4IvV17idg.png)

Theres 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 the game to 4 players and click save](https://cdn-images-1.medium.com/max/3200/0*yvKSiawHUWa29aks.png)*Set max players to 6 even though Tanks!!! limits the game to 4 players and click save*

![Once Unity Live Mode is enabled](https://cdn-images-1.medium.com/max/3200/0*2D9iBiu9Kvu7b9_5.png)*Once Unity Live Mode is enabled*

## Building for iOS

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

![Update the Bundle id and Usage Descriptions](https://cdn-images-1.medium.com/max/2000/1*PsK-_l3BsAbFGFru0tKV9g.png)*Update the Bundle id and Usage Descriptions*
> *Please note: you need to have Xcode installed and setup before attempting to build the project for iOS.*

![When building for the first time, create a new folder “Builds” and save the build as iOS](https://cdn-images-1.medium.com/max/3200/0*E4Bevgp6Vdm6cUiF.png)*When building for the first time, create a new folder “Builds” and save the build as iOS*

![After the project has successfully built for iOS, we will see the project in the **Builds** folder](https://cdn-images-1.medium.com/max/3068/0*ZpTF_SY7jYtvfQF9.png)*After the project has successfully 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.

![Enable automatic signing to simplify the signing process. Remove In-App-Purchase if it shows up.](https://cdn-images-1.medium.com/max/3200/0*Q-xj06zaHzfrRA8-.png)*Enable automatic signing to simplify the signing process. Remove In-App-Purchase if it shows up.*

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](https://everyplay.com/) plugin for screen recording and sharing your game session. Unfortunately [Everyplay shutdown](https://everyplay.com/shutdown-notice.html) 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.

![](https://cdn-images-1.medium.com/max/3200/0*_TJDAAREk7U6HpOg.png)

Now that we have the Gradle file open, select all and replace it 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.

{% gist https://gist.github.com/digitallysavvy/dfa6805f71eb081873fbe1db914f4fc7 %}

The last change we need to make 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.

{% gist https://gist.github.com/digitallysavvy/a7570dc8d6232353d7b821a3916672ed %}

Now 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 *Package Name*, I chose com.agora.tanks.videodemo.

You may also need to create a key store for the Android app. See this section of the PlayerSettings in Unity Editor:

![Android KeyStore setting](https://cdn-images-1.medium.com/max/2000/1*aaDpzcD4UJ9Fa9YEyYDEZQ.png)*Android KeyStore setting*

## Integrating Video Chat

For this project Agora.io Video SDK for Unity was chosen, because it makes implementation into our cross-platform mobile project, really simple.

Let’s open up the Unity Store and search for “Agora Video SDK”.

![](https://cdn-images-1.medium.com/max/3200/0*UYMr47aXj2DMQgfS.png)

![You only download the asset once, and then you can import it to different projects.](https://cdn-images-1.medium.com/max/2000/1*yXsvPxEq1v4Up6S76O18Ww.png)*You only download the asset once, and then you can import it to different projects.*

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

![Uncheck the last four items before import](https://cdn-images-1.medium.com/max/2000/1*tbySbQz0_bYqH7HuTW7VmQ.png)*Uncheck the last four items before import*

You should then open the Lobby as your main scene. The following shows the also how the service page would look like for the multiplayer settings:

![](https://cdn-images-1.medium.com/max/2628/1*yEqSW8yUnHIrzwW6EWeJPA.png)

**Discussion**: in the following sections we will go through how the project to be updated with new code and prefabs changes. For those just want to quickly try out everything. Here is a [plugin file](https://apprtcio-my.sharepoint.com/:u:/g/personal/rick_agora_io/ETAvJX_kPoxBj1ZsFFpng_wBI4PU4nqmM3TFt9vdRUym-g?e=AKy8hg) to import all the changes. You will just need to enter the AppId to the GameSettings object as described after importing.

## Modify the Tank Prefab

Let add a plane on to the top of the tank to render the video display. Find the CompleteTank prefab from the project. Add a 3D object Plane to the prefab. Make sure the follow values are updated for the best result:

* Y =8 for position; Scale to 0.7. Rotate -45 degrees on X, 45 degrees on Y.

* Do not cast shadow

* Disable the Mesh Collider script

![Plane Prefab values](https://cdn-images-1.medium.com/max/2000/1*Xt6wCF1nFl7ukB-2XXxTHQ.png)*Plane Prefab values*

Attach VideoSurface.cs script from the Agora SDK to the Plane game object.

Save the change, and test the prefab in the game by going to Training to see the outcome. You should see a tank similar to the following screen:

![Tank with Plane attached](https://cdn-images-1.medium.com/max/2000/1*ArFBPJupw34SwaiJSegZQA.png)*Tank with Plane attached*

## Create UI for Mic/Camera Controls

Next, open the GameManager prefab and create a container game object and add three toggles under it:

* Mic On

* Cam On

* FrontCam

![GameManager UI Canvas](https://cdn-images-1.medium.com/max/4628/1*J0jZZ4x7vV_ydxpmiFXiiA.png)*GameManager UI Canvas*

That’s basically all the UI changes we need for this project. The controller script will be added to the prefab later in the following sections.

## Controller Scripts

Next we will go over some scripts to make the video chat to work for this game. Before adding new scripts, we will modify a script to allow the input for the Agora AppId.

### GameSettings

Two updates to the scripts to make the game to work with Agora SDK.

(1) Add a SerializedField here for the AppId.

Go to your Agora developer’s account and get the AppId (you may need to following the instruction to create the project first):

![Agora AppId](https://cdn-images-1.medium.com/max/5488/1*TFdma4NjCzmMx1R6lPK-WA.png)*Agora AppId*

In the Lobby scene of the Unity Editor, paste the value of the App ID there and save:

![Set the App ID for Agora API](https://cdn-images-1.medium.com/max/4332/1*G5p1IRgv0eirsjdAuYO_gg.png)*Set the App ID for Agora API*

(2) Add support for Android devices by asking for Microphone and Camera permissions.

{% gist https://gist.github.com/icywind/4f9ea33ec3f7264a195a0025f95eca4b %}

### Agora Controller Scripts

Before jump right into the code, let’s understand what capabilities are needed. They are:

* Interface to the Agora Video SDK to do join channel, show video, mute microphone, flip camera, etc.

* Actual implementation for the Agora SDK event callbacks.

* Mapping from the Unity Multiplayer’s id to Agora user’s id.

* A manager to respond to the UI Toggle actions that we created earlier.

The following script capture shows the corresponding scripts hierarchy. A discussion about the four classes follows.

![Controller Script Hierarchy](https://cdn-images-1.medium.com/max/3296/1*rtYiKK9fFjka9qfPtvzBOg.png)*Controller Script Hierarchy*

**AgoraApiHandlerImpl**: this class implements most of the Agora video SDK event callbacks. Many of them are placeholders. To support the minimum capability in this game, the following handlers are of the most interest:

* **JoinChannelSuccessHandler** — when local user joins a channel, this will corrospond to create a game and start a server. The server name is the same as the channel number for Agora SDK.

* **UserJoinedHandler** — when a remote user joins the game.

* **UserOfflineHandler** — when a remote user leaves the game.

SDKWarningHandler is commented out to reduce the noise in the debugging log. But it is recommended to enable it for an actual project.

{% gist https://gist.github.com/icywind/cf92a9f3fb7a04abfa1b0e4120b36b88 %}

**AgoraVideoController**: this singleton class is the main entry point for the Tanks project to interact with the Agora SDK. It will create the AgoraApiHandlerImpl** **instance and handles interface call to join channel, mute functions, etc. The code also checks for Camera and Microphone access permission for Android devices.

{% gist https://gist.github.com/icywind/f7b80bd4a213d9c648e4d2594d3fbb6c %}

 **AgoraPlayerController**: while Unity Unet library maintains the Network player’s profile, the Agora user’s id is created asynchronously. We will maintain a list of network player and a list of Agora user ids. When the game scene actually starts, we will bind the two list together into a dictionary so the Agora id can be looked up by using a Networkplayer’s profile. (We don’t need this binding mechanism if user id is known. In an actual production project, it is recommended to let the game server to provide the user ids to set for JoinChannel() call.)

{% gist https://gist.github.com/icywind/e51a12af151f730a3e1eb222acb400ff %}

* **AgoraUIManager**: position the container game object to top right location of the game screen. It provides three toggling functions:

1. *Mic On* : mute the audio input.

1. *Cam On*: mute the local camera streaming and turn off the display of the local player.

1. *CamSwitch*: switch between the front camera or the back camera on the mobile device.

{% gist https://gist.github.com/icywind/e64b7336cd364d3fd35dec24b74ef11c %}

## Tanks Code Modifications

We will interact the above controllers code into the existing project by updating the Tanks code in the following classes:

### TankManager

1. Add a field to bring in the VideoSurface instance that we added to the Plane and drag the Plane game object from the children to the field.

![Setting up project setting](https://dev-to-uploads.s3.amazonaws.com/i/cikn9zmenmn1tvc928ek.png)

2. Add a constant to name the video-surface.

    ```
    public const string **LocalTankVideoName **= "Video-Local";
    ```

3. Change code near the end of the *initialize*() method, where it looked like this before:

![old code](https://cdn-images-1.medium.com/max/2000/1*Q892mta4lDDOsvj0f9VtWQ.png)*old code*

The new code:

{% gist https://gist.github.com/icywind/9804224c53eee56580fc37ca18f60ff6 %}

Discussion: here is the code to associate the plane display that we created earlier to render the video feed. The VideoSurface script handles this work. The only thing it needs is the Agora Id. If it is the local player, the Agora Id will be default to 0, and the SDK will automatically render the device’s camera video onto the hosting plane. If this is a remote player, then the non-zero Agora Id is required to get the stream to render.

### Calls JoinChannel()

The *JoinChannel*() function calls in **AgoraVideoController** class establish the local player status and starts a channel server. There are three places to initiate the call.

* **CreateGame.cs: **add a line to the *StartMatchmakingGame*() function inside the callback. It will look like this:
```C#
    private void StartMatchmakingGame()
    {
       GameSettings settings = GameSettings.s_Instance;
       settings.SetMapIndex(m_MapSelect.currentIndex);
       settings.SetModeIndex(m_ModeSelect.currentIndex);
    
       m_MenuUi.ShowConnectingModal(false);
    
       Debug.Log(GetGameName());
       m_NetManager.StartMatchmakingGame(GetGameName(), (success, matchInfo) =>
          {
             if (!success)
             {
                m_MenuUi.ShowInfoPopup("Failed to create game.", null);
             }
             else
             {
                m_MenuUi.HideInfoPopup();
                m_MenuUi.ShowLobbyPanel();
                AgoraVideoController.instance.JoinChannel(m_MatchNameInput.text);
             }
          });
    }
```
* **LevelSelect.cs**: add the call in *OnStartClick*(). And the function will look like this:
```C#
    public void OnStartClick()
    {
       SinglePlayerMapDetails details = m_MapList[m_CurrentIndex];
       if (details.medalCountRequired > m_TotalMedalCount)
       {
          return;
       }
       
       GameSettings settings = GameSettings.s_Instance;
       settings.SetupSinglePlayer(m_CurrentIndex, new ModeDetails(details.name, details.description, details.rulesProcessor));
    
       m_NetManager.ProgressToGameScene();
      AgoraVideoController.instance.JoinChannel(details.name);
    }
```
* **LobbyServerEntry.cs**: add the call in *JoinMatch*(). Modify the function signature to add string channelName. And the function will look like this:

   ```C#
    private void JoinMatch(NetworkID networkId, string channelName)
    {
       MainMenuUI menuUi = MainMenuUI.s_Instance;
    
       menuUi.ShowConnectingModal(true);
    
       m_NetManager.JoinMatchmakingGame(networkId, (success, matchInfo) =>
          {
             //Failure flow
             if (!success)
             {
                menuUi.ShowInfoPopup("Failed to join game.", null);
             }
             //Success flow
             else
             {
                menuUi.HideInfoPopup();
                menuUi.ShowInfoPopup("Entering lobby...");
                m_NetManager.gameModeUpdated += menuUi.ShowLobbyPanelForConnection;
                
                AgoraVideoController.instance.JoinChannel(channelName);
             }
          });
    }

   public void Populate(MatchInfoSnapshot match, Color c)
    {
       string[] split = match.name.Split(new char[1] { '|' }, StringSplitOptions.RemoveEmptyEntries);
       string channel_name = split[1].Replace(" ", string.Empty);
       m_ServerInfoText.text = channel_name**;
    
       m_ModeText.text = split[0];
    
       m_SlotInfo.text = string.Format("{0}/{1}", match.currentSize, match.maxSize);
    
       NetworkID networkId = match.networkId;
    
       m_JoinButton.onClick.RemoveAllListeners();
       m_JoinButton.onClick.AddListener(() => JoinMatch(networkId, **channel_name**));
    
       m_JoinButton.interactable = match.currentSize < match.maxSize;
   }
   ```

   
### NetworkManager.cs

Insert code for player leaving the channel in Disconnect():
```C#
    public void Disconnect()
    {
       switch (gameType)
       {
          case NetworkGameType.Direct:
             StopDirectMultiplayerGame();
             break;
          case NetworkGameType.Matchmaking:
             StopMatchmakingGame();
             break;
          case NetworkGameType.Singleplayer:
             StopSingleplayerGame();
             break;
       }
       AgoraVideoController.instance.LeaveChannel();
    }
```
That’s basically all code changes we need to get the video streaming on local and remote player working! But wait, there is a catch we missed. The plane changes rotation with the tank when moving! See one of the tilted position:

![Tank Moved with Plane](https://cdn-images-1.medium.com/max/2000/1*N7enCXKwxSp786SMFNqkNg.png)*Tank Moved with Plane*

We will need another script to fix the rotation:

{% gist https://gist.github.com/icywind/b1a7c2b5898f70559fd323f51458b944 %}

Build the project deploy the game to iOS or Android devices and start playing to a friend! You may see other person’s face (and yours) on top of the tanks and you can yell to each other now!

### So, we are done building a fun project!

![Bear vs Duck in a Tank Battle!](https://cdn-images-1.medium.com/max/2000/1*jKNriRlh4Ly22MMzAgKQAA.png)*Bear vs Duck in a Tank Battle!*

The current, complete code is hosted on [Github](https://github.com/icywind/AgoraTanks).


###  Other Resources 
* The complete API documentation is available in the Document Center.
* For technical support, submit a ticket using the Agora Dashboard or reach out directly to our Developer Relations team devrel@agora.io
* Come join the Slack community: https://agoraiodev.slack.com/messages/unity-help-me