DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Data Engineering
  3. Databases
  4. Adding Real-Time Communication in Unity3D Multiplayer Games

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.

Naman Kapur user avatar by
Naman Kapur
·
May. 20, 17 · Tutorial
Like (14)
Save
Tweet
Share
5.31K Views

Join the DZone community and get the full member experience.

Join For Free

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

Database dev Git Joins (concurrency library) Connection (dance) Moment app Snippet (programming) Interface (computing) Game engine

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DeveloperWeek 2023: The Enterprise Community Sharing Security Best Practices
  • Integrating AWS Secrets Manager With Spring Boot
  • UUID: Coordination-Free Unique Keys
  • How to Engineer Your Technical Debt Response

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: