Learn how to:

  1. Set up Mirror in Unity
  2. Create a chat box which resembles natural messaging capabilities such as infinite scrolling, send message on enter, etc.
  3. Develop a C# script to facilitate server client communication

Requirements:

  1. Basic knowledge of Unity editor and C# scripting
  2. Know at a high level what server client system is

Introduction

One of my Unity projects utilizes UNet which is a low level multiplayer API which is now deprecated. I needed a good networking solution that supports basic server-client communication and data transport. In search for a replacement, I quickly found Mirror.

Image for post

Mirror Logo. Image source

Image for post

Good reviews and seems like it’s beginner friendly. Image source

Until now it seems to work really well. Everything is good to understand after using a beginner tutorial and the documentation. — Acdia, Unity Asset Store

One way I experimented with Mirror to make sure it fits my application’s needs was to create a chat system. I found several helpful tutorials online for setting up a chat box and network manager. Here I will aggregate those tutorials and show a comprehensive source for creating a chat system in Unity. This tutorial assumes that you have already installed Unity (preferably updated to the latest version or 2018.4 LTS and higher for compatibility with Mirror).


Installations

The first step is to install Mirror. It is a Unity Package so installation is very simple. Go to the Mirror page on Unity asset store (create a Unity account if you haven’t) and click on “Add to My Assets”. Follow the installation process to open with Unity, download the package, and install into your app. In order for the Mirror _GameObjects _to show up, restart Unity.

Setting up Network Manager

Once you have restarted Unity, create an Empty GameObject and name it “NetworkManager”. This will contain Mirror components needed to establish our server and player connections. To NetworkManager, add a _NetworkManager _component (which will also add Telepathy Transport) and a NetworkManagerHUD. Leave all fields as their presets for now.

Setting up the Chat Box

If you are developing a game, the client could be an object or character in your game. When players join the game, they are represented as that object or character. Since we are creating a standalone chat, any of the default shapes should work. I chose a cube, but feel free to pick one of the other standard 2D or 3D shapes. Add a _Cube _object to the scene.

#chat #unity #c-sharp-programming

How to Create a Chat System in Unity With Mirror
15.35 GEEK