Sample chat UI

The Unity Chat SDK ships with a ready-made chat UI prefab so you can bring up a working chat screen without writing any UI code.

Starter sample

This prefab is a starter sample you copy into your project and customize. Treat it as a reference implementation, not a production UI component.

Key security

The controller exposes publishKey, subscribeKey, and secretKey as serialized inspector fields, which means they are saved into the scene file. For production, load keys from secure storage or use an Access Manager token issued by your server, and never commit secret keys to source control.

What is included​

The sample lives under Assets/PubnubChat/Samples~/UIChatPrefab/ in the Unity Chat SDK repository and contains three files:

FileTypePurpose
PubnubChatUI.prefabUnity prefabCanvas with a scroll view for messages, a text input, and a send button. Drop it into a scene to get a full chat layout.
PubnubChatUIController.csMonoBehaviourInitializes the Chat SDK, creates a public conversation, optionally fetches recent message history, listens for incoming messages, and sends new messages from the input field.
PubnubMessageUIController.csMonoBehaviourRenders a single message as a left- or right-aligned bubble with a metadata line (user ID and timestamp).

Prerequisites​

Install and initialize the Unity Chat SDK before using the sample. Refer to Initial configuration.

Import the sample​

The prefab is distributed as a UPM package sample:

  1. In Unity Editor, open Window -> Package Manager.
  2. Select the Pubnub Chat package from the list.
  3. Expand the Samples section and click Import next to Pubnub Chat - Example UI Prefab.
  4. Unity copies the sample into Assets/Samples/Pubnub Chat/<version>/Pubnub Chat - Example UI Prefab/.

Use the sample​

  1. Open the scene where you want the chat to appear. Make sure it contains an EventSystem (Unity adds one automatically when you drop the prefab into an empty scene).
  2. Drag PubnubChatUI.prefab from the imported sample folder into the scene hierarchy.
  3. Select the prefab root object and fill in the inspector fields under Pubnub settings with your PubNub keys, a user ID, and the channel ID you want to join. For the field reference, see Inspector reference.
  4. Press Play. The controller creates a public conversation, wires up the send button and input field, and starts receiving messages on the channel.

Inspector reference​

PubnubChatUIController​

required
publishKey required#
Type: string
Default: no default
Publish Key from your PubNub keyset.
subscribeKey required#
Type: string
Default: no default
Subscribe Key from your PubNub keyset.
secretKey#
Type: string
Default: no default
Secret Key used for server-side operations. Leave empty for client builds.
userId required#
Type: string
Default: no default
Unique User ID that identifies the current user.
channelId required#
Type: string
Default: no default
Identifier of the public channel the sample joins on start. If the channel already exists, the controller reuses it.
fetchChannelHistoryOnStart#
Type: bool
Default: false
When true, the controller loads the last five hours of messages (up to displayedMessagesLimit) into the scroll view on start.
displayedMessagesLimit#
Type: int
Default: 50
Maximum number of messages shown at once. Older messages are removed from the UI when this limit is reached.
messageUIPrefab required#
Type: PubnubMessageUIController
Default: no default
Prefab used to render each message bubble. Pre-wired in the sample to a bubble prefab under the same folder.
scrollView required#
Type: ScrollRect
Default: no default
Scroll view that contains the message list.
scrollViewContent required#
Type: RectTransform
Default: no default
Content transform under the scroll view where message instances are parented.
information required#
Type: TextMeshProUGUI
Default: no default
Header label that displays the active channel ID.
messageInput required#
Type: TMP_InputField
Default: no default
Input field where the user types new messages.
sendButton required#
Type: Button
Default: no default
Button that publishes the text currently in messageInput.

PubnubMessageUIController​

required
TheirBubble required#
Type: GameObject
Default: no default
Container shown when the message was sent by another user.
TheirText required#
Type: TextMeshProUGUI
Default: no default
Text component inside TheirBubble that displays the message body.
TheirMeta required#
Type: TextMeshProUGUI
Default: no default
Metadata line (user ID and timestamp) for messages received from others.
MyBubble required#
Type: GameObject
Default: no default
Container shown when the current user sent the message.
MyText required#
Type: TextMeshProUGUI
Default: no default
Text component inside MyBubble that displays the message body.
MyMeta required#
Type: TextMeshProUGUI
Default: no default
Metadata line for messages sent by the current user.

Controller source​

The controller handles the full lifecycle: it creates a Chat instance, creates or reuses a public Channel with the configured ID, optionally loads history, binds OnMessageReceived, and calls Connect() to start receiving live messages. On destroy, it removes the button, input, and message listeners so scene transitions leave no dangling subscriptions.

1using System;
2using System.Collections.Generic;
3using PubnubApi;
4using PubnubChatApi;
5using TMPro;
6using UnityEngine;
7using UnityEngine.UI;
8using Button = UnityEngine.UI.Button;
9using Channel = PubnubChatApi.Channel;
10
11/// <summary>
12/// A simple UI implementation of a Pubnub Chat.
13/// </summary>
14public class PubnubChatUIController : MonoBehaviour
15{
show all 138 lines

Customize​

Use the prefab as a starting point and adapt it to your game:

CustomizationDescription
ThemingCreate prefab variants of PubnubChatUI.prefab to swap backgrounds, fonts, and colors without losing upstream updates.
Message bubblesEdit the PubnubMessageUIController prefab to restyle MyBubble and TheirBubble, change TextMeshPro fonts, or add avatar slots. Extend Initialize() to accept the data you need.
Channel typesReplace chat.CreatePublicConversation(channelId) with CreateGroupConversation() or CreateDirectConversation() to target private rooms.
More real-time eventsSubscribe to additional channel events like typing indicators, presence, and read receipts.
Secure keysMove publishKey, subscribeKey, and secretKey out of the inspector and into a runtime source such as a server-issued Access Manager token.

Was this page useful?

Last updated on