Overview
Playing games together is the best thing ever. Agora provides an easy to use SDK for unity games. People used to have a static profile picture but, nowadays, it would be more fun if there is a video rendering instead of a static image. Agora is a scalable and reliable solution to enhance the gaming experience.
Agora account setup
You need an App ID to start working with agora.io SDK.
Create an account with Agora.io.
Create a new project (e.g. “Test”)
Open the project from the dashboard and you will find the App ID hidden, copy it for future usage.
Project setup with Agora SDK
Open unity hub and create a new project.
Agora supports unity 2017.4.35 or higher, I’m using 2019.2.0.
Download and Import “agora video chat sdk for unity” from the Asset Store into the project
For this tutorial, I’m assuming that you are not a noob to unity editor, C# and mobile development.
Scripting Agora functionality extended
Now that you have set up the SDK. Either you can use default test scripts available with SDK or you can extend the functionality the way you want.
You can refer to the basic implementation from Github Link or refer the documentation.
Basic method setup in order to connect with Agora.
Create AgoraEngineHandler.cs to manage agora engine functionalities in a sort of decoupled way.
// instance of agora engine
private IRtcEngine mRtcEngine;
Load/Unload Engine are same function as available with the package script TestHelloUnityVideo.cs
// load agora engine
public void loadEngine(string appId){ … }
// unload agora engine
public void unloadEngine(){ … }
Join and Leave channel functions are also similar as defined in TestHelloUnityVideo. But here, there are changes to the event callback function because we are managing user functionality separately.
// Join the channel public void Join(string channel) { ... mRtcEngine.OnJoinChannelSuccess = AppEvents.onJoinChannelSuccess; mRtcEngine.OnUserJoined = AppEvents.onUserJoined; mRtcEngine.OnUserOffline = AppEvents.onUserOffline; mRtcEngine.OnRemoteAudioStateChanged = AppEvents.onRemoteAudioStateChanged; mRtcEngine.OnRemoteVideoStateChanged = AppEvents.onRemoteVideoStateChanged; mRtcEngine.OnLeaveChannel = AppEvents.onLeaveChannel; ... // join channel int result = mRtcEngine.JoinChannel(channel, null, 0); if(result >= 0) { AppEvents.onLocalUserJoined?.Invoke(); } … } public void Leave() { mRtcEngine.LeaveChannel(); mRtcEngine.DisableVideoObserver(); }
AppEvents class has defined all the events that are going to be used across the application.
public class AppEvents { public static OnJoinChannelSuccessHandler onJoinChannelSuccess; public static OnUserJoinedHandler onUserJoined; public static OnUserOfflineHandler onUserOffline; public static OnRemoteAudioStateChangedHandler onRemoteAudioStateChanged; public static OnRemoteVideoStateChangedHandler onRemoteVideoStateChanged; public static OnLeaveChannelHandler onLeaveChannel; public static Action onLocalUserJoined; }
UserManager.cs
Here we are managing user callbacks from agora engine.
Subscribe all the AppEvents to manage user callbacks.
Callbacks / Event Listeners
OnJoinChannelSuccess : When channel is successfully joined.
OnUserJoined : When a user Joins the channel successfully.
OnUserOffline : When user goes offline.
OnRemoteAudioStateChanged : When remote user changes audio settings.
OnRemoteVideoStateChanged : When remote user changes video settings.
onLocalUserJoined : When local user joins the channel successfully
Callback methods.
private void onJoinChannelSuccess(string channelName, uint uid, int elapsed) { … } private void onUserJoined(uint uid, int elapsed) { … } private void onUserOffline(uint uid, USER_OFFLINE_REASON reason) { … } private void OnRemoteAudioStateChanged(uint uid, REMOTE_AUDIO_STATE state, REMOTE_AUDIO_STATE_REASON reason, int elapsed) { … } private void OnRemoteVideoStateChanged(uint uid, REMOTE_VIDEO_STATE state, REMOTE_VIDEO_STATE_REASON reason, int elapsed) { … }
GameManager.cs : This is to manage the overall functionality of the game or an application.
AppId : you can find the application ID from the agora developer dashboard.
public const string AppId; public AgoraEngineHandler agoraHandler; private ArrayList _permissionList = new ArrayList(); private void Awake() { _permissionList.Add(Permission.Microphone); _permissionList.Add(Permission.Camera); } private void CheckPermissions() { foreach (string permission in _permissionList) { if (!Permission.HasUserAuthorizedPermission(permission)) { Permission.RequestUserPermission(permission); } } } private void Update() { CheckPermissions(); } public void onJoinButtonClicked() { agoraHandler.loadEngine(AppId); string channelName = SessionData.channelName; agoraHandler.Join(channelName); } public void onLeaveButtonClicked() { agoraHandler.Leave(); agoraHandler.unloadEngine(); }
SessionData is shared across UI and managing scripts. That can be used to reflect any changes without any dependencies.
public class SessionData : ScriptableObject { public string channelName; public userData userData; public Dictionary<uint, User> userDictionary; public static System.Action onUpdate; public void Update() { onUpdate?.Invoke(); } }
User.cs : This class is having user’s data.
public struct userData { public uint UID; public string Name; public bool AudioStatus, VideoStatus; } public class User : MonoBehaviour { public userData data; public Toggle AudioToggle, VideoToggle; public VideoSurface videoSurface; public void SetAudio(bool state) { AudioToggle.isOn = data.AudioStatus = state; } public void SetVideo(bool state) { VideoToggle.isOn = data.VideoStatus = state; } }
Reference UI
User UI : Below is just a reference hierarchy of a user which is attached with User.cs. You can create a prefab out of it and manage the same for all the users.
Example of an prototype UI for a Card Game (Rummy,Poker, etc.).
Example of a Video chat application in a Grid View.
Conclusion
Hence, Audio and Video chat are one of the essential features which are helpful in increasing the engagement among players and hence it will play a vital role for a successful game.
This was just a basic setup to start working with agora.io in unity. There are many more features available by Agora for user interactive gaming. Audio and Video chat is an example for an engaging gaming experience.
This tutorial can be used to work with any existing game or application. Just grab the package and extend as per your flexibility and experience the Agora in action.