Displays text in the right kind of chat bubble inside a ScrollRect, for your game's chat or messaging system.
In MVC terms (or a similar architecture), it is the View component.
- Package name:
com.basementexperiments.chatboxbubbles - Repository: https://github.com/gamesbyBAE/unity-chatbox-bubbles
- Video Guide / Walkthrough: coming soon
- Requirements
- Installation
- Importing the Sample
- Quick Start
- API
- Customising the Bubbles
- Package Layout
- Credits
- Author
- License
- Unity 2019.1 or newer
- TextMeshPro (
com.unity.textmeshpro), which the Package Manager installs automatically as a dependency
-
Open Window > Package Manager.
-
Click + (top-left) and choose Add package from git URL...
-
Paste the URL below and click Add:
https://github.com/gamesbyBAE/unity-chatbox-bubbles.git
Add the package to Packages/manifest.json in your project:
{
"dependencies": {
"com.basementexperiments.chatboxbubbles": "https://github.com/gamesbyBAE/unity-chatbox-bubbles.git"
}
}Unity locks a Git package to the commit it first downloaded. To update:
- click Update for the package in the Package Manager window, or
- delete the package's entry from the
locksection ofPackages/packages-lock.json.
- Open Window > Package Manager and select Chatbox Bubbles.
- Open the Samples tab and click Import next to Chatbox Bubbles Sample.
- Open the
ChatBubblesSamplescene fromAssets/Samples/Chatbox Bubbles/<version>/Chatbox Bubbles Sample/.
In the scene:
BASIC_SETUP(under the Canvas) is the minimum setup you need to get the system running.FANCIER_SETUPgives you an idea of what else you can build with it.
- Add a ScrollRect to your Canvas.
- Add the
ChatBubblesViewcomponent to a GameObject and assign:- Scroll: the ScrollRect
- Sent Prefab, Received Prefab, Awaiting Response Prefab: bubble prefabs, for example the ones in
Runtime/Prefabs/DefaultBubbles
- Call it from your own controller:
using BAE.ChatBubbles;
using UnityEngine;
public class MyChatController : MonoBehaviour
{
[SerializeField] private ChatBubblesView chatView;
public void OnPlayerSentMessage(string text)
{
chatView.AppendMessage(text, ChatBubblesView.BubbleType.SENT);
// Show the "typing..." bubble while waiting for a reply.
chatView.AppendMessage("", ChatBubblesView.BubbleType.AWAITING_RESPONSE);
}
public void OnReplyReceived(string text)
{
// Appending any message removes the awaiting-response bubble automatically.
chatView.AppendMessage(text, ChatBubblesView.BubbleType.RECEIVED);
}
}All types are in the BAE.ChatBubbles namespace.
| Member | Description |
|---|---|
AppendMessage(string message, BubbleType bubbleType) |
Adds a new bubble at the bottom and scrolls to it. If the previous bubble was an awaiting-response bubble, it is removed first. |
ClearMessages() |
Destroys all bubbles and resets the scroll area. |
| Value | Bubble used |
|---|---|
SENT |
Sent Prefab |
RECEIVED |
Received Prefab |
AWAITING_RESPONSE |
Awaiting Response Prefab (for example, a "typing..." indicator) |
AnimateAwaitBubble: animates the awaiting-response bubble.InputButtonSwitcher: switches between a microphone button and a send button, like WhatsApp does. The mic button shows while the input field is empty, and the send button replaces it once the user types a character.
- Edit
SentMessage_Default.prefab.ReceivedMessage_Default.prefabis a prefab variant of it, so your changes carry over, and you can still override properties on the variant. - The padding of the Horizontal and Vertical Layout Groups on the prefab sets the gap between bubbles.
- A 9-sliced sprite is recommended for the Image component's
Source Image.
Tip: Prefabs inside an installed package are read-only. Copy them into your
Assetsfolder (or import the sample) before you edit them, then assign the copies toChatBubblesView.
- Select the GameObject that has the
ChatBubblesViewcomponent. - In the Inspector, right-click the component header (or click its ⋮ menu).
- Choose:
- Add Test Chats: adds bubbles with placeholder text of different lengths, plus an awaiting-response bubble.
- Clear Test Chats: removes all spawned bubbles.
These options also work in Play mode.
unity-chatbox-bubbles/ (repository root)
├── package.json
├── README.md
├── CHANGELOG.md
├── LICENSE.md
├── Runtime/
│ ├── basementexperiments.chatboxbubbles.asmdef
│ ├── Scripts/ ChatBubblesView, AnimateAwaitBubble, InputButtonSwitcher
│ ├── Prefabs/ DefaultBubbles, FancyBubbles
│ └── Sprites/
└── Samples~/
└── GettingStarted/ ChatBubblesSample scene
The runtime code compiles into the basementexperiments.chatboxbubbles assembly, which has Auto Referenced enabled.
If your own code uses an assembly definition, add basementexperiments.chatboxbubbles to its Assembly Definition References.
- Mic icon: Maxim Basinski Premium, Flaticon (https://www.flaticon.com/free-icons/mic)
- Send icon: Tanah Basah, Flaticon (https://www.flaticon.com/free-icons/send)
Basement Experiments
See LICENSE.md