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.
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 AppID retrieved from the Agora Console.
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.
A bit of housekeeping: Inside Unity’s native OnApplicationQuit(), clean up the Agora engine when you exit the demo.
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().
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.
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().
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.
Create a game object and attach the Agora VideoSurface.cs script to it dynamically. Initialize it with the incoming UID.
Here, you clean up the video panels of users who’ve left by searching for the UID from the callback.
In this function, you instantiate the objects that receive video frames from the Agora engine, and set their position in the scene.
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.
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.
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!