How to Make Online Chat in Unity for Multiplayer Games

How to Make Online Chat in Unity for Multiplayer Games
Source Code & Advanced Videos ►
Forge Networking GitHub ►
Twitter ►

# Online Chat System in Unity
Today we will be going over how to make a online (multiplayer) chat system in the Unity game engine. This will be a simple full screen chat application that you can send to your friends to begin chatting. This will probably be the easiest networking application that you can make. If you check out the advanced tutorials listed in the description below you will learn how to block bad words and make chat commands so you can kick players and play chat games like rolling dice or flipping coins.

## Installing Forge Networking
We will be using the freely available Forge Networking multiplayer codebase. We are using Forge because it is the only fully supported, open sourced, networking system you can use for free. You never have to worry about CCU or hidden charges, just build, launch and go with full source code. Disclaimer, Bearded Man Studios, Inc. are the creators of Forge Networking and I am the creator of Forge Networking. However, me being the creator of Forge Networking doesn’t mean there are strings attached, it is still free and open source!

To begin the installation process you will need to go to the link in the video description to get Forge Networking from GitHub (or download the source code from the description which includes the Forge Networking source code). Next, just open up a new project in Unity and import Forge Networking into the project. That is all that you need to do in order to get Forge Networking running in your project.

## Setup Project
We need to setup our project correctly inside of Unity so that it will run even when backgrounded. We are also going to use the helper Forge Networking scene to get a multiplayer menu for free.
– Open your project build settings
– Click on “Player Settings…”
– Turn on “Run in Background*”
– Search for “Multiplayer Menu” in your project
– Add the “Multiplayer Menu” scene as the first scene in your build settings
– Create a new scene and name it “Chat” and save it into a folder named “Chat” in the “Assets” directory
– Make the “Chat” scene you just created as the second scene in the build settings
– Save your project and open the “Chat” scene in Unity

## Setup User Interface
Now that we have setup our networking system, let’s make our user interface. The interface is just going to be a simple input box to type our message, an area to view messages, and a scrollbar.
– Right click on the hierarchy and create an “InputField”
– Make the input field bottom aligned with 5, 5, 0, 5, 30 for the Left, Pos Y, Pos Z, Right, and Height respectively
– Right click on the generated Canvas element and add a new “Scroll View”
– Make the Scroll View center aligned with 5, 5, 0, 5, 40 for the Left, Pos Y, Pos Z, Right, and Height respectively
– Select the Scroll View and uncheck “Horizontal”
– Open the Scroll View children and delete the “Horizontal Scrollbar”
– Open the Scroll View “Viewport” child to find the “Content” object and add a “Content Size Fitter” component to it as well as a “Vertical Layout Group” component
– Make the Content object have a padding of 5 on all sides
– Make Vertical Layout Group have “Control Child Size” on for Width and Height
– Make Vertical Layout Group have “Child Force Expand” on for Width “only”
– Make “Content Size Fitter” have “Unconstrained” for Horizontal Fit and “Preferred Size” for the Vertical Fit
– Right click on the Content and add a new “Text” object
– Click on the Text object and add a “Layout Element” component to it
– Change the text on the Text object to be “{0}: {1}”
– Save the Text object as a prefab and then delete it from the scene
– Save the scene

## Create Network Code
Write the code found in the video or copy the source code files form the source code package on the website (link in the descrption). The three source code files that you will need are the `ChatInputField.cs` file and the `ChatManager.cs` file found in the “Chat” directory of the source files.

## Hook Up User Interface and Network Code
– Create a “ChatManager” object in the Hierarchy somewhere and add the `ChatManager` script onto it
– Set the “Content” of the Scroll View as the “Chat Content” field on Chat Manager script
– Set the “Message” prefab we created as the “Chat Message” field on Chat Manager script
– Put the `ChatInputField` script onto the “InputField” that you created in the UI
– Drag the “ChatManager” object into the “Chat Manager” field on the Chat Input Field script

## Advanced Features and Source Code
Now you have completed this tutorial you just need to build and run your new chat multiplayer game! We have a few awesome advanced tutorials and source code on the site listed in the description!

Please visit us at:
Chat City Melbourne

About the author: cadmin