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.
Join the DZone community and get the full member experience.
Join For Freeappwarp 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)!
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:
- implement in-app referrals with our growth hacking toolkit to boost game downloads.
- enable social sharing of all the moments, levels unlocked or completing any major event.
- implement social leaderboards to induce competitive spirit and improve user engagement.
- never lose the interest of your users by launching campaigns on all channels including push, email, and in-app messages.
- predict the propensity of the players to uninstall the game and send personalized notifications to re-engage them.
Published at DZone with permission of Naman Kapur. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments