On this page

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.prefab
Unity prefab
Canvas 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.cs
MonoBehaviour
Initializes 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.cs
MonoBehaviour
Renders 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
ParameterDescription
publishKey *
Type: string
Default:
n/a
Publish Key from your PubNub keyset.
subscribeKey *
Type: string
Default:
n/a
Subscribe Key from your PubNub keyset.
secretKey
Type: string
Default:
n/a
Secret Key used for server-side operations. Leave empty for client builds.
userId *
Type: string
Default:
n/a
Unique User ID that identifies the current user.
channelId *
Type: string
Default:
n/a
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 *
Type: PubnubMessageUIController
Default:
n/a
Prefab used to render each message bubble. Pre-wired in the sample to a bubble prefab under the same folder.
scrollView *
Type: ScrollRect
Default:
n/a
Scroll view that contains the message list.
scrollViewContent *
Type: RectTransform
Default:
n/a
Content transform under the scroll view where message instances are parented.
information *
Type: TextMeshProUGUI
Default:
n/a
Header label that displays the active channel ID.
messageInput *
Type: TMP_InputField
Default:
n/a
Input field where the user types new messages.
sendButton *
Type: Button
Default:
n/a
Button that publishes the text currently in messageInput.

PubnubMessageUIController

* required
ParameterDescription
TheirBubble *
Type: GameObject
Default:
n/a
Container shown when the message was sent by another user.
TheirText *
Type: TextMeshProUGUI
Default:
n/a
Text component inside TheirBubble that displays the message body.
TheirMeta *
Type: TextMeshProUGUI
Default:
n/a
Metadata line (user ID and timestamp) for messages received from others.
MyBubble *
Type: GameObject
Default:
n/a
Container shown when the current user sent the message.
MyText *
Type: TextMeshProUGUI
Default:
n/a
Text component inside MyBubble that displays the message body.
MyMeta *
Type: TextMeshProUGUI
Default:
n/a
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.

1

Customize

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

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