Unity games that are connected online need social features such as in-game chat for players to communicate with players and teammates, friendly competitions with live leaderboard updates and scores, and notifications to update players on missed messages, in-game events, and news about the game.
While developers can set up this online infrastructure themselves, online features take time, money, and resources to create, secure, maintain, and update constantly. The resources used in this endeavor could be used instead to add more features to the game itself.
This is where PubNub, a developer API platform that enables applications to receive real-time updates at a massive, global scale, can handle this online functionality infrastructure. PubNub serves as the foundation for over 2000 customers in diverse industries, including gaming. Game developers can depend on PubNub’s scalability and reliability to power their online features for games and tools for in-game chat, live leaderboard updates, and alerts and notifications to bring players back to the game.
To begin integrating PubNub functionality into a Unity game, you’ll need to first create a PubNub account and import the PubNub Unity SDK Package.
Sign in or create an account to create an app on the Admin Portal and get the keys to use in your application. Learn how to do so by following this how-to on creating keys.
Download the PubNub Unity package.
Import the package to your Unity project by going to Assets > Import Package > Custom Package.
Configure your keys.
Follow the Unity SDK documentation to implement PubNub functionality in your game.
Game developers such as Panzerdog, Beamable, and Mayhem use PubNub as the infrastructure to power their in-app chat, live-leaderboard updates, and notifications to bring players back to their games. But how do these and other developers actually use PubNub to drive their online features in their games?
PubNub offers features in its real-time data API that allows developers to fully integrate PubNub into their application for their specific needs, even if the tools and games may differ.
When a player performs an action such as typing indicators, sending text, or updating a score, you need to send updates to other players to reflect that changes are being made.
Messages are sent via channels, which are used to transmit data from one device to another. When you're sending a message, you must specify a channel to send the message to. PubNub will then automatically create the channel for you when you send the message. You can think of channels as separate instances, which is useful for organizing the different groups using your application.
You can capture when players begin typing, text, emoji reactions, leaderboard updates, files, and other complex metadata, and then publish the message to the PubNub network. Any client subscribed to this specified channel will then receive this message in real time.
Once a player has performed an action and the other connected players need to be updated, you need a way to receive and handle those changes being made.
PubNub’s Subscribe functionality is the second key pillar of the publish-subscribe model. You would subscribe players to a channel (or multiple channels), and handle incoming messages using an event listener. Any clients subscribed to the channel(s) will receive all messages sent to it, and handle them based on the received update.
Players can receive new chat messages and leaderboard updates from other players in real time.
Some Unity games are designed with thousands of concurrent daily players or at the very least have social features such as a friends list. You’ll need to know when players join or leave your game, and display their online status.
PubNub Presence monitors the subscribers of channels. This means that you can look up who’s currently online, when a player has joined/left a channel, or which channels a player is subscribed to. Checking presence is based on receiving presence events, which are sent automatically from the PubNub servers when a player’s state changes. You would then add a presence event listener to listen for these events, and handle them based on the received action (join, leave, timeout, state-change, or interval).
Once a client has come online, the presence event listener would detect this new join action. Subscribers to the channel would then know to update that player’s online status to something such as a green online circle, generate a notification for other players to see that one of their friends coming online, or some other kind of online indicator. Once players leave the game or timeout, you would then change that player’s online status to a grey online circle.
Even though your online players will receive data nearly instantaneously, you’ll need a way to store previous chat messages, leaderboard scores, and other complex metadata for not only historical evidence, but to display this information to your offline players once they come online.
PubNub’s Message Persistence feature, also known as History, allows you to retain and retrieve historical messages. You need to know the channel name the message was sent to and the message’s timetoken. You can control how long messages are stored, how far back you want to retrieve messages, the number of entries, and influence the return order.
Your application can then retrieve previous messages sent by players, and display them to players when they come online. You can also use Message Persistence to leaderboard scores and other personal settings for your game.
PubNub’s History function is great to store and obtain previous information sent on that channel for players that were not online. While the information will be waiting for them as soon as they come online (caught by Presence detection), what if you wanted to notify offline players about missed messages, or to notify players about new in-game updates, events, and news?
PubNub’s Push Notifications feature provides a method to deliver messages quickly and reliably, regardless of whether the application is in the foreground or background on iOS and Android devices. The feature bridges native PubNub publish with different notification service providers like Firebase Cloud Messaging and Apple Push Notification Service by sending updates in one message call - this means you don’t need to use and handle the SDK for each device platform.
Most applications support web and mobile devices. Players who are playing your game on Android and iOS devices can benefit from mobile push notifications to alert them whenever new information is available. This can be missed messages from other players or game updates, in-game events, and news.
Most applications display players’ information to them in the way of application settings. This information could include the player’s name, nickname, username, email address, notification updates, friend list, and other account information.
PubNub’s Object feature provides easy-to-use, serverless storage to store metadata for players, channels, and memberships without the need of setting up your own database. You can optionally store metadata, including custom properties, for your players in your game. PubNub triggers events when object data is set or removed from the database, which means that you can receive these events in real time and update your front-end application accordingly.
You can use the Object feature to store any attributes about your players in one place and can then easily present this data in your game, without the need of setting up your own database. You can use membership metadata to display channel membership lists for a player or even give them the possibility to join or leave groups, rooms, or organizations.
Although you want your players to be able to join or leave groups, rooms, or channels to be able to chat with other players, you need to ensure you limit a player’s access depending on their permission rights (you wouldn’t want a new player to have admin privileges!). Perhaps they need a password to join a specific group or need to be able to purchase a separate access key to access special events or content.
PubNub’s Access Manager controls client access to resources using restricted permission tokens and allows you to regulate clients’ access to PubNub resources, such as channels, channel groups, and UUIDs.
If you have a special event that’s only meant to be shared by a certain group of players, you would use the Access Manager to restrict access to only these specified clients. You can make a one-to-one chat room private by only allowing two players read and write access, or make a room public by giving all players access to it. You could also deny access to players who are no longer allowed to access your application, such as if they were banned for misconduct.
At this point, you can send and receive messages in real-time, notify offline players, and track player state. But what if you want to do something with a message before or after a player sends it?
PubNub’s Functions feature is used to capture events happening in your PubNub channels and enables you to perform an action based on that event. It enables you to write code to address your business needs and allows you to capture events before and after publishing messages.
This means that you don’t need to create and set up your own servers or incur increased latency to external servers to translate or censor inappropriate messages. You can notify other players as soon as they are mentioned (i.e. @Jane.Doe) or announce to a channel when someone joins/leaves. You can connect players to chatbots or have a system add letters to a dictionary for automatic suggestion/completion functionality. You can even route messages and files to a moderator channel if the service marks them as inappropriate.
You’ve learned about PubNub’s key features and how you can use them to enhance your Unity game's online features for your players. Whether you’re creating a community for your players with in-app chat and social features, generating competition amongst players with a real-time leaderboard, or bringing players back to your game through notifications, PubNub can deliver real-time communication updates, so you can focus on your unique game needs. If you would like to learn more about how to add online features to your Unity game, take a look at our growing collection of updates, demos, tutorials, and documentation.
Explore PubNub's Unity Developer Path, a guided journey in how you can use PubNub's Unity SDK to add real-time, online features to games with ease.
Updates from PubNub and our partners regarding exciting updates and features, including an overview of building Unity games.
Interact with our real-time demos covering our diverse industry use cases, including our Unity game PubNub Prix, a racing game where you can chat with other players in and view leaderboard updates in real time.
How-to guides on how to create or handle different situations when building applications using PubNub.
Follow our step-by-step tutorials to create different applications powered by PubNub, including the Unity game PubNub Prix.
Dive into our documentation for the PubNub Unity SDK, to learn how to customize PubNub's features that uniquely fit your application.
Feel free to reach out to the Developer Relations Team at firstname.lastname@example.org for any questions or concerns.