Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Adding Real-Time Communication in Unity3D Multiplayer Games

DZone's Guide to

Adding Real-Time Communication in Unity3D Multiplayer Games

If you're interested in getting started in game dev, read on to learn how to use the AppWarp game engine to get started creating a web-based game.

· Web Dev Zone
Free Resource

Tips, tricks and tools for creating your own data-driven app, brought to you in partnership with Qlik.

AppWarp has been here for quite some time now. Thousands of game developers in more than 150 countries have built amazing multiplayer games on multiple platforms using AppWarp. The most consistent and popular of them all remains Unity3D. In this post, I will simply talk about how game developers can add real-time communication in their Unity3D games to make them rich and engaging. Real-time implementation is not rocket science, there is no server-side coding or socket level programming required—everything, including the real-time communication, is handled by AppWarp Cloud.

Should you need to learn how to get the pieces together and set up the basic code required, here is our tutorial video that has stood against the tests of time and still assists game developers across the world with a sample project we have created.

The entire source code of this sample is available in our Git Repo, but I am mentioning some of the relevant code snippets below. Get AppWarp SDK for Unity3D here—to initialize it, simply enter your API and Secret Keys which you will get once you sign up for AppWarp and create your first project.

WarpClient.initialize(apiKey,secretKey);

Now create and add a Listener which implements the relevant WarpClient callback interfaces and connects with the server.

Listener warpLayer = new Listener();
WarpClient.GetInstance().AddConnectionRequestListener(warpLayer);
WarpClient.GetInstance().AddNotificationListener(warpLayer);
WarpClient.GetInstance().AddRoomRequestListener(warpLayer);

Now simply call Connect() to establish your connection with the server.

WarpClient.GetInstance().Connect();

Once your connection is established and authenticated, simply join a room and subscribe to it receive its notifications.

WarpClient.GetInstance().SubscribeRoom(roomid);
WarpClient.GetInstance().JoinRoom(roomid);

After you have joined the room, you can start sending your user’s coordinates to other users in the room. In this example, we will use our chat API to do that.

string json = "{\"x\":\""+transform.position.x+"\",\"y\":\""+transform.position.y+"\",\"z\":\""+transform.position.z+"\"}";
WarpClient.GetInstance().SendChat(msg);


Once the remote player also joins, you will start receiving messages from it. Handle them and move the cylinder representing it:

public void onChatReceived (ChatEvent eventObj)
{
Log(eventObj.getSender() + " sent " + eventObj.getMessage());
SimpleJSON.JSONNode msg = SimpleJSON.JSON.Parse(eventObj.getMessage());
if(eventObj.getSender() != id)
{
appwarp.movePlayer(msg["x"].AsFloat,msg["y"].AsFloat,msg["z"].AsFloat);
}
}

And that's it! Just build your Unity3D app on two different endpoints and you should be able to see two endpoint movements in real-time (a simple white cylinder)! 

unity multiplayer

Also read: Bridging the Gap – Plugin for Unity & iOS

Furthermore, AppWarp can always be clubbed with gaming backend APIs that will not only help in user engagement but also will earn you long lasting loyalty. Some of the top use cases that will enable you to grow your game are:

  1. Implement in-app referrals with our growth hacking toolkit to boost game downloads.
  2. Enable social sharing of all the moments, levels unlocked or completing any major event.
  3. Implement social leaderboards to induce competitive spirit and improve user engagement.
  4. Never lose the interest of your users by launching campaigns on all channels including push, email, and in-app messages.
  5. Predict the propensity of the players to uninstall the game and send personalized notifications to re-engage them.

Also read: How to Implement In-App Purchase in Your Game

Explore data-driven apps with less coding and query writing, brought to you in partnership with Qlik.

Topics:
web dev ,game development ,unity 3d

Published at DZone with permission of Naman Kapur. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}