</>Demo our Sample Unity Chat App
The Unity chat lobby shown here is an example of what you’ll be building with this tutorial. Open the demo in two or more windows to simulate transmitting messages in realtime between players in the game lobby. Export the demo app to any device or platform Unity supports and you’ll still be able to send messages between players in realtime. PubNub powers this realtime communication between clients and will work on all devices and platforms that Unity exports too.
Using PubNub with Unity is so easy that this code is all you need to publish a new chat message:
Unity is a cross-platform game engine open to the developer community, enabling the building and publishing of content created within the game engine. Unity is easy to use, but creating multiplayer games and experiences has always been one of the most challenging hurdles on the platform. Using PubNub with Unity enables creating realtime multiplayer games significantly easier.
PubNub empowers developers to build low-latency, realtime applications that perform reliably and securely, at a global scale. All developers need to do is write the front-end code with PubNub's easy-to-use API's and PubNub handles all the infrastructure for you. This is especially important in the game development industry, as it mitigates the risk of multiplayer server downtime in the event that your game grows faster than expected.
In this tutorial, we'll walk through how to build a basic multiplayer game lobby chat room using the Unity 3D game engine. This tutorial will work across iOS, Android, AR/VR, Web, and Video Game Consoles. No matter what platform you are exporting to all users will be able to chat between devices. PubNub can also power the realtime interactions between players in your game and not just chat. This makes the possibilities endless with the PubNub API and Unity for game development.
Download the Unity Hub. The Unity Hub is used to manage Unity versions and Unity projects you’re developing.
Once you’ve downloaded Unity Hub, add any Unity Version equal to or older than 2018 LTS. The 2018 LTS version was used to create the tutorial app, but upgrade it if you wish.
Create a new project by opening Unity Hub, go to the projects tap, and click ‘NEW’. Configure the project how you’d like. Then import the PubNub SDK into your Unity project. Do so by downloading the latest PubNub.unitypackage. Import that package into Unity by going to Assets -> Import Package -> Custom Package.
Once imported there may be errors in your console window. You may see:
error CS0234: The type or namespace name 'TestTools' does not exist.
Fix this problem by going to: Window -> General -> Test Runner. Click on the drop-down menu in the top right corner and enable playmode tests for all assemblies.
Note: If you download or clone the GitHub repo, you won't have to re-import the PubNub package. You will only have to enable the Test Runner. Download the Unity Chat lobby repo here.
The GitHub repo includes four folders. Assets, PubNub, Scenes and Scripts. In the Assets folder, there is a background image, submit button, font files and a loading circle. In the Scripts folder, there are the two scripts: SendMessage and LoadingCircle. The SendMessage script is where we will be writing the code to send and receive messages from clients in realtime.
In your applications hierarchy, there are a few essential elements that display chat messages on the screen. The RealtimeChat Scene includes a Main Camera, and a Canvas that renders all of the input fields, buttons, and text objects on the screen. In order for the input fields to be interactive, an EventSystem object is automatically created and displayed in the Hierarchy. Additionally, there are assets such as a background, which is a simple PNG; and a line to separate the bottom and top sections of the screen.
If you are starting the project from scratch, you will need to create a Main Camera, which should have the SendMessage script attached to it.
Design your user interface however you would like. In the Unity Chat Tutorial example app the loadingCircle is placed on the top right of the screen, and the input fields at the bottom of the screen with the submit button. The chat messages will appear in the middle of the screen and will be removed from the UI once the screen is completely full of messages.
Now that the scene has been designed, let's add a script to the Main Camera to develop the chat room interface for the application.
TThe first step is to require the PubNub API and other UnityEngine libraries, including UnityEngine.ui. The class JSONInformation is where input field data will be formatted to be published via PubNub.
The class SendMessage is where all of the application’s chatroom logic will be written. We start off by creating some public variables and creating a chatMessageQueue to handle message creation and deletion.
Now that you have created your public variables, make sure to return to your Unity Inspector and drag each element from your Hierarchy into the appropriate fields on your SendMessage Script.
Next, let's create the Start() function. First fill out and submit the form below to generate your own PubNub Publish and Subscribe keys: you will need them in order to get your application to publish and receive messages.
In the beginning of the Start() function, initialize PubNub and create a listener on the button that will run the TaskOnClick() function when pressed. Next, fetch the last 12 messages that were sent on the PubNub Channel "chatchannel" (name this channel whatever you wish). If there is an error, print the error in the Unity console, however if there isn't one, iterate through the last 12 messages and insert them into the class JSONInformation. Then run the function CreateChat() and pass the chat message to that function to print the data on the screen.
Create a counter for formating the messages on screen so that messages aren't created past the black line. Next, we subscribe to the PubNub channel "chatchannel". When an event is published on that channel it will trigger the subscribe callback function. The callback takes the value from the channel event, writes it to the end of the chat history for view, and removes the oldest message if there are more than 12 (ensuring no more than 12 messages are on screen at the same time)
Next, let's create the CreateChat function which will be responsible for placing new text objects onto the screen whenever the function is called. The payload information from the PubNub subscribe is passed through the function, and then is assigned to the text object’s Text. Additionally, the message is added to the queue and the index counter is iterated.
Now that the CreateChat() function has been created, we’ll add functionality that translates all existing messages up on the screen to make room for the new message. Additionally, let's delete the first message on the screen to make room for the new message.
The final step is to add the functionality that allows a user to send a message and reset the text field to be blank once the message has been submitted. In clicking the submit button, the user is publishing the message through PubNub. When a message is published, all subscribed clients with the application open will receive that message in realtime.
Save your work in the Unity editor and click the “run” button. You should now be able to send and receive messages from your Unity Chat lobby. It's really that easy to make your first realtime chat application in the Unity 3D game engine.
If you ran into any roadblocks with the tutorial, feel free to reach out at firstname.lastname@example.org. Download the source code here!