Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Chatbox Bubbles

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.


Contents


Requirements

  • Unity 2019.1 or newer
  • TextMeshPro (com.unity.textmeshpro), which the Package Manager installs automatically as a dependency

Installation

Option A: Package Manager window

  1. Open Window > Package Manager.

  2. Click + (top-left) and choose Add package from git URL...

  3. Paste the URL below and click Add:

    https://github.com/gamesbyBAE/unity-chatbox-bubbles.git
    

Option B: manifest.json

Add the package to Packages/manifest.json in your project:

{
  "dependencies": {
    "com.basementexperiments.chatboxbubbles": "https://github.com/gamesbyBAE/unity-chatbox-bubbles.git"
  }
}

Updating

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 lock section of Packages/packages-lock.json.

Importing the Sample

  1. Open Window > Package Manager and select Chatbox Bubbles.
  2. Open the Samples tab and click Import next to Chatbox Bubbles Sample.
  3. Open the ChatBubblesSample scene from Assets/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_SETUP gives you an idea of what else you can build with it.

Quick Start

  1. Add a ScrollRect to your Canvas.
  2. Add the ChatBubblesView component 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
  3. 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);
    }
}

API

All types are in the BAE.ChatBubbles namespace.

ChatBubblesView

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.

ChatBubblesView.BubbleType

Value Bubble used
SENT Sent Prefab
RECEIVED Received Prefab
AWAITING_RESPONSE Awaiting Response Prefab (for example, a "typing..." indicator)

Other components

  • 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.

Customising the Bubbles

  • Edit SentMessage_Default.prefab. ReceivedMessage_Default.prefab is 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 Assets folder (or import the sample) before you edit them, then assign the copies to ChatBubblesView.

Testing without entering Play mode

  1. Select the GameObject that has the ChatBubblesView component.
  2. In the Inspector, right-click the component header (or click its ⋮ menu).
  3. 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.


Package Layout

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

Assembly Definition

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.


Credits

Author

Basement Experiments

License

See LICENSE.md

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages