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:
| File | Type | Purpose |
|---|---|---|
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:
- In Unity Editor, open Window -> Package Manager.
- Select the Pubnub Chat package from the list.
- Expand the Samples section and click Import next to Pubnub Chat - Example UI Prefab.
- Unity copies the sample into
Assets/Samples/Pubnub Chat/<version>/Pubnub Chat - Example UI Prefab/.
Use the sample
- 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). - Drag
PubnubChatUI.prefabfrom the imported sample folder into the scene hierarchy. - 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.
- 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
| Parameter | Description |
|---|---|
publishKey *Type: stringDefault: n/a | Publish Key from your PubNub keyset. |
subscribeKey *Type: stringDefault: n/a | Subscribe Key from your PubNub keyset. |
secretKeyType: stringDefault: n/a | Secret Key used for server-side operations. Leave empty for client builds. |
userId *Type: stringDefault: n/a | Unique User ID that identifies the current user. |
channelId *Type: stringDefault: n/a | Identifier of the public channel the sample joins on start. If the channel already exists, the controller reuses it. |
fetchChannelHistoryOnStartType: boolDefault: false | When true, the controller loads the last five hours of messages (up to displayedMessagesLimit) into the scroll view on start. |
displayedMessagesLimitType: intDefault: 50 | Maximum number of messages shown at once. Older messages are removed from the UI when this limit is reached. |
messageUIPrefab *Type: PubnubMessageUIControllerDefault: n/a | Prefab used to render each message bubble. Pre-wired in the sample to a bubble prefab under the same folder. |
scrollView *Type: ScrollRectDefault: n/a | Scroll view that contains the message list. |
scrollViewContent *Type: RectTransformDefault: n/a | Content transform under the scroll view where message instances are parented. |
information *Type: TextMeshProUGUIDefault: n/a | Header label that displays the active channel ID. |
messageInput *Type: TMP_InputFieldDefault: n/a | Input field where the user types new messages. |
sendButton *Type: ButtonDefault: n/a | Button that publishes the text currently in messageInput. |
PubnubMessageUIController
| Parameter | Description |
|---|---|
TheirBubble *Type: GameObjectDefault: n/a | Container shown when the message was sent by another user. |
TheirText *Type: TextMeshProUGUIDefault: n/a | Text component inside TheirBubble that displays the message body. |
TheirMeta *Type: TextMeshProUGUIDefault: n/a | Metadata line (user ID and timestamp) for messages received from others. |
MyBubble *Type: GameObjectDefault: n/a | Container shown when the current user sent the message. |
MyText *Type: TextMeshProUGUIDefault: n/a | Text component inside MyBubble that displays the message body. |
MyMeta *Type: TextMeshProUGUIDefault: 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:
| Customization | Description |
|---|---|
| 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. |