Unity Introduction

LAST EDIT Nov 21 2022

The Unity SDK helps you build chat or messaging experiences for platforms the Unity Engine supports. The SDK is a C# library for Unity, which includes:

  • A low-level client to access the Stream Chat service API.

  • A sample project demonstrating an implementation using Unity's uGUI UI toolkit.

Before reading the docs, consider trying our online API tour. The tour is a fast way to learn how the API works. It's in-browser (and Javascript-based) but the high-level concepts are the same across all our SDKs. You can also check out our Unity Introduction Tutorial that covers the most basic features of the SDK.

Before you start

Copied!

When you connect your application to the Stream Chat service, you connect as a specific user for which you'll require: api key, user id, and the user token. In this tutorial, we'll focus on a regular authenticated user (guests and anonymous users are also possible). Follow these steps to acquire authentication credentials:

  1. Register your account here and enter the dashboard in order to read the api key of your application.

  2. In your dashboard, click application name to enter the application panel and go the Explorer tab, from which you can create new user and get the user id.

  3. Once you have the user id, you can use our online token generator to generate the user token.

Getting started

Copied!

This guide will quickly help you get up to speed on Stream’s Chat API. First, download the latest Stream Chat SDK from the Releases page of the GitHub repository and add it to your project.

Troubleshooting

Copied!

The Stream Chat SDK requires 2 dependencies to be satisfied:

Text Mesh Pro

Copied!

If you did not have a Text Mesh Pro package in your project before, Unity will automatically prompt you with the following popup:

Go ahead and click "Import TMP Essentials" in order to import Text Mesh Pro package into the project.

Newtonsoft.Json

Copied!

In case you already had a Newtonsoft.Json library in your project you may encounter the following error in the console

In such case, you can go ahead and remove the StreamChat\Libs\Serialization\Newtonsoft.Json.dll from the SDK.

Chat client

Copied!

In order to run the chat client you need to:

  1. Create chat client instance and provide valid authorization credentials

  2. Update the client per frame

  3. Call Dispose() on the client after you're done using it

Here you can see a full example showing MonoBehaviour wrapper that handles StreamChatClient. You'll need to attach it to a gameObject in your scene.

Since it's common practice to destroy objects on scene change, you should exempt the chat client from this. You may also need to add a DontDestroyOnLoad attribute to this MonoBehaviour. The user token is usually provided by your backend when you login or register in the app. If authentication is disabled for your app, you can also use a Client->DevToken(UserId) to generate an insecure token for development. You should never launch into production with authentication disabled. For more complex token generation and expiration examples have a look at the token provider documentation.

Watching a Channel

Copied!

Watching a channel causes the client to begin emitting events whenever changes in the watched channel (usually new messages) occur.

Send message

Copied!

Sending a message will add a message from the logged in user into the channel.

Querying channels

Copied!

Query Channels methods let you retrieve a list of channels. You can specify a filter and sort order.

Delete channel

Copied!

Stream chat supports deleting channels to remove them from an app.

Deleting Many Channels

Copied!

If you need to delete a group of channels without making a large number of repetitive connections to the API, you can use the batch deletion message.

Hide channel

Copied!

Hiding a channel is a softer alternative to deleting a channel. If the channel is hidden, it can be restored and data from it can be accessed at a later date.