Image for post
Image for post

Escaping the Cold: Migrating from Frozen Mountain to Agora in Unity

Joel Thomas
Nov 20, 2020 · 5 min read

Hello developers! In this tutorial you’ll see just how easy it is to switch from using Frozen Mountain to Agora for your video chat purposes in Unity.

First, some context is necessary as to why you’d want to migrate from Frozen Mountain. You are accomplishing the same functionality using ~200 lines of Agora SDK code, compared to the ~1500 lines required in the Frozen Mountain example. Agora puts the developers first, and we are continuously improving the developer experience and streamlining our products.

When using Frozen Mountain, you have three standard WebRTC protocols to choose from. When you use Agora, we simplify this process by connecting your users to our own SD-RTN (software-defined real-time network), which is more scalable and reliable than standard WebRTC video chat.

I’ve inserted Agora SDK code in the locations where Frozen Mountain implements comparable functionality, so you’re able to compare how simple it is to use Agora vs. Frozen Mountain.

To get started, you need an Agora account. If you don’t have one, here is a guide to setting up an account.

This project implements the Agora SDK into the Frozen Mountain starter demo for Unity. You can find the example project here on our community GitHub.

Image for post
Image for post

Join Screen

You will reuse the assets provided in the Join level inside Assets > Scenes.

Inside of JoinBehaviour.cs:
You need a variable reference to the mRtcEngine and your app ID retrieved from the Agora Console.

Image for post
Image for post

Next, you initialize the Agora engine and make sure the JoinBehaviour object persists between scenes.

DontDestroyOnLoad is called to maintain the reference to the Agora engine when the scenes change. One initialization is necessary for the duration of the application.

When you either join a channel or leave a channel and return to the Join Screen, OnEnable() will fire and make sure your references are connected.

Image for post
Image for post

A bit of housekeeping: Inside Unity’s native OnApplicationQuit(), clean up the Agora engine when you exit the demo.

Image for post
Image for post
Image for post

Note: This project is meant for reference purposes and development environments, it is not intended for production environments. Token authentication is recommended for all RTE apps running in production environments. For more information about token based authentication within the Agora platform please refer to this guide: https://bit.ly/3sNiFRs

The updated JoinBehaviour.cs script demonstrates how simple it is to create a connection to the Agora network (SD-RTN) and to set up a video chat by calling JoinChannel().

Channel Screen

In the channel screen, implement the functionality for leaving the channel and returning to the Join screen, the callbacks for when users join and leave, and the code to create new video screens for joining users.

I’ve changed the Unity Start() function to OnEnable(), and left Frozen Mountain’s code intact yet commented out.

Image for post
Image for post

From Start Method to OnEnable()

Initialize the callbacks for users joining and leaving. There are many other callbacks you can subscribe to, but you are aiming for simplicity here. Frozen Mountain implements the callbacks in a function called Register() which is called in OnEnable().

Image for post
Image for post
Image for post
Image for post

OnJoinChannelSuccessHandler Callback

Get the audio and webcam devices to display them in the bottom panel, and get the channel name from the callback to display in the top-right panel.

Image for post
Image for post

OnUserJoinedHandler()

Create a game object and attach the Agora VideoSurface.cs script to it dynamically. Initialize it with the incoming UID.

Image for post
Image for post

OnUserOfflineHandler()

Here, you clean up the video panels of users who’ve left by searching for the UID from the callback.

Image for post
Image for post

Make ImageSurface()

In this function, you instantiate the objects that receive video frames from the Agora engine, and set their position in the scene.

Image for post
Image for post

Leave

When a user presses the Leave button, the Agora channel is left and the scene switches back to the opening Join scene, allowing the user to join a different channel.

Image for post
Image for post

Toggle Video Button

To replicate the “audio only” setting in Frozen Mountain, you can call mRtcEngine.MuteLocalVideoStream() to enable or disable the video display and allow only audio to pass through the channel. Create a new button along the top bar and assign this function into the OnClick event inside the Inspector.

Image for post
Image for post

Summary

You now have everything you need to create a basic web chat app comparable to the Frozen Mountain demo. The power of Agora is the simplicity of use, coupled with the SD-RTN delivering world-class connection results that outperform WebRTC protocols.

I hope this guide was helpful. If you learned something, make sure to teach someone else!

Have fun making the migration from Frozen Mountain to Agora!

-Joel

Agora.io

Agora delivers cutting edge APIs for developers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store