Unity Chat Tutorial:
In-Game Messaging

In the following tutorial you'll learn the first steps in integrating the Unity Chat SDK into your project. We'll cover user authorization, creating channels, sending messages and receiving server events. Once you've got the basics, you can customize the features and complexity of your chat system to suit your game.

Set up your development environment

In this tutorial, we will use the Stream Chat API and the Unity SDK Plugin to add in-game chat to a new Unity Engine project.

Before you start: Make sure you have Unity Editor installed on your system. If not you can download it here.

We'll be using the latest at the time of writing version - 2021.2.10f1 but any newer version you have available should work fine as well.

Create a new Unity project

Open Unity Hub and create a new project using the 3D template.

Create a new unity project preview

Name the project Stream Chat Tutorial.

Download the Unity Chat Assets

The Unity Chat SDK includes the core functionality you need to interact with the Stream Chat API, with support for messages, channels, real-time events and more.

First, you want to make sure you've downloaded the latest release of the Stream Chat Unity Package from the Releases page of the GitHub repository and imported it into the project.

Import Stream SDK preview

Create new Scene

Open the Scenes folder, create a new scene and rename it to Stream Chat Tutorial

Open new scene preview

Now open the newly created Stream Chat Tutorial scene.

Add new Game Object

Go to scene hierarchy window, create a new Game Object and rename it to StreamChatClient. This Game Object will later run the Stream Chat SDK Client.

Create new game object

Add new Script

Go to project window, create a new C# script and rename it to StreamChatBehaviour.cs. Open StreamChatBehaviour.cs in an editor of your choice, In this tutorial we'll be using JetBrain's Rider and paste the following code:

In this script we define user authorization credentials and inject them into Stream Chat Client Factory method. Once the client is instantiated we use _client.Connect() method to connect user with the server and subscribe to _client.Connected event in order to print log confirming that we have indeed successfully connected with the server.

We also call _client.Update() each frame in order to execute communication with the server and once the client is no longer needed we call _client.Dispose() in order to cleanup internal connection.

In a production scenario, user tokens should be generated using a backend and our server SDK.

Now add the StreamChatBehaviour.cs to the StreamChatClient Game Object as a component:

Attached script to game object

Test Connection!

Go ahead and play the scene. You should now see logs confirming that we have established a valid connection with the Stream Server.

Valid connection with the Stream Server

Create a new channel

Now that our connection is established we can create a new channel to which our logged user will send messages.

In the StreamChatBehaviour.cs script replace OnStreamChatConnected() method:

with the:

You'll also have to update the usings section to the following:

Once you save the script and play the scene you should now see a log confirming that we have received a channel state from the server.

Confirmed new channel creation

This ensures that the requested channel is created on the server and we start sending messages.

Send message

First, let's subscribe to the IStreamChatClient.MessageReceived event so we get notified when the server has received a new message for a watched channel.

In our StreamChatBehaviour.cs script in the Awake method, right after the line:

add the following code:

Next, change the OnStreamChatConnected() method to the following:

And finally, add this 2 methods at the end of the file:

Final result

Your final StreamChatBehaviour.cs script should look as follows:

After playing the scene, you should now see a log confirming that we have successfully sent a message to the channel. You can also see this message in the Stream Dashboard's Chat Explorer.

New message confirmation


We hope that you've enjoyed this tutorial on gaming chat with Unity. By using Stream’s chat components, you and your team will be able to get your Unity Game or Application up and running with in-game chat in minutes.

Now that you’ve completed the tutorial on Unity Chat, you can build anything chat related with our components. If you have a use-case that doesn’t quite seem to work, or simply have questions, please don’t hesitate to reach out here.

Final Thoughts

In this chat tutorial we've covered the most basic interaction with the low-level SDK.

The API has plenty more features available to support more advanced use-cases such as push notifications, content moderation, rich messages and more, and the chat SDK for Unity is undergoing heavy development to include all functionality.

Give us Feedback!

Did you find this tutorial helpful in getting you up and running with Unity for adding chat to your project? Either good or bad, we're looking for your honest feedback so we can improve.

Kiddom logo

We’ve been going at lightspeed to build more communication functionality into Kiddom. The only way to achieve this in four months was to do a chat integration with Stream because we needed to do it reliably and at scale.

Head of Product, Kiddom profile picture

Nick Chen

Head of Product, Kiddom

Next Steps

Start your 30-Day Chat trial to try out all our Chat product has to offer. No commitment or credit card required. If you want a custom plan or have questions, we are eager to talk with you.

Activity Feeds

Build any kind of feed without the headache of scalability or reliability of your feeds.

Learn more →

99.999% Uptime SLA, Industry leading compliance and security best practices.

Learn more →