# Unity Project Integration

#### <mark style="color:blue;">What will you achieve</mark>

{% hint style="info" %}
At the end of this tutorial you will be able to send a message from the Client to the Server and from the Server to the client
{% endhint %}

#### <mark style="color:blue;">Create a new Unity Project</mark>

Open your Unity Hub and create a new Project, for the sake of simplicity in this tutorial we have chosen a 2D project, however same logic will apply to 3D.&#x20;

![Create a new project in Unity](https://2106246176-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAal11zmTbHWEnDONeJej%2Fuploads%2FTd2YMYzG3uRI1sbQf8J6%2FScreenshot%202022-05-03%20at%2016.03.47.png?alt=media\&token=180068f4-7a43-4456-84d4-26923e2c86ec) ![Choose the 2D or 3D template](https://2106246176-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAal11zmTbHWEnDONeJej%2Fuploads%2FQRbzEJlxWS5nnUZDjagu%2FScreenshot%202022-05-03%20at%2016.10.23.png?alt=media\&token=f5a4a8f9-9451-47a8-aac1-c8fa91daca41)

#### <mark style="color:blue;">Import Scripts and Server into the project</mark>

Next from the `ColyseusUnity.zip` file downloaded in the previous lecture extract the `Scripts` folder and import it in the Unity Project at the same level where your Asset folder is.

![Import Script folder from ColyseusUnity.zip](https://2106246176-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAal11zmTbHWEnDONeJej%2Fuploads%2FLds0I6rpHKbh59oBmlu2%2FScreenshot%202022-05-03%20at%2016.16.41.png?alt=media\&token=c3bf9b0d-c75f-46a0-b690-b738a84ab3ff)

Copy and paste the `Server` folder into the root of the project, this can be done from file explorer. Here below I am using macOS, but same logic for Windows or Linux

![Added the Server into the root of the Unity Project](https://2106246176-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAal11zmTbHWEnDONeJej%2Fuploads%2FEclkFPSW7pXuVuv9xU97%2FScreenshot%202022-05-03%20at%2016.21.41.png?alt=media\&token=8e4689af-12ef-4809-aba7-2bf46279d632)

> We have given you a simplified version of the Server files that you need for Colyseus, the bare minimum to understand how the system works. In our opinion these files will be sufficient to integrate the game and interact with the OPArcade website and use the Colyseus server, however if you want to use the full implementation these can be downloaded directly from their [Github repo](https://github.com/colyseus/colyseus-unity-sdk#upm)

#### <mark style="color:blue;">Add Colyseus SDK to Unity</mark>

Go back to your Unity Project to install the Colyseus SDK. Open Window -> Package Manager

![go to Window Package Manager](https://2106246176-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAal11zmTbHWEnDONeJej%2Fuploads%2F4SlzGx4iLNgtHytJXmOt%2FScreenshot%202022-05-03%20at%2016.38.51.png?alt=media\&token=cede2dc9-586f-4b3c-8504-1d7d59bf0fab)

Now let's add the SDK by copy-pasting the gitURL, see the [documentation](https://docs.colyseus.io/colyseus/getting-started/unity3d-client/) for the most updated url

{% hint style="info" %}
<https://github.com/colyseus/colyseus-unity3d.git#upm>
{% endhint %}

Select the + icon at the top left of the Package Manager and choose the ***Add package from git URL*** option. Paste the above URL and the Colyseus SDK will be installed in the project

![Colyseus SDK installed](https://2106246176-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAal11zmTbHWEnDONeJej%2Fuploads%2F3z3yYBdPOUDrbkOkU0u0%2FScreenshot%202022-05-03%20at%2016.46.03.png?alt=media\&token=a1e4d65f-cd68-4888-a6b5-352bee3e5e68)

#### <mark style="color:blue;">Game Objects Script</mark>

We will need to create two new separate game objects in order to manage the scripts correctly and also a special Colyseus asset. Do not worry, the process is very simple! 🦁 You have made it this far, almost there!

* Create **ServerManager** and **GameClient** object in the scene

![ServerManager and GameClient created](https://2106246176-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAal11zmTbHWEnDONeJej%2Fuploads%2FP9xJBDz1F6pYURDfTDPG%2FScreenshot%202022-05-03%20at%2017.12.30.png?alt=media\&token=174bca30-d556-4329-bb55-c0b0ba4c6554)

* Assign the `Colyseus Server Manager` script to the **ServerManager** object&#x20;

![](https://2106246176-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAal11zmTbHWEnDONeJej%2Fuploads%2FpfDMypdzqWW2QQfIAjCo%2FScreenshot%202022-05-03%20at%2017.20.26.png?alt=media\&token=cb019d9c-3e60-4d05-95b7-be8760745450)

* Assign the `Game Client` script to the **GameClient** object

![](https://2106246176-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAal11zmTbHWEnDONeJej%2Fuploads%2F9MGiuKtYgzIcLngsyiMz%2FScreenshot%202022-05-03%20at%2017.21.45.png?alt=media\&token=5737a9ae-d85f-46c2-aa49-e27d4eee19ea)

* Create a **ColiseusSettings Scriptable Object** in a new Settings folder&#x20;

![](https://2106246176-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAal11zmTbHWEnDONeJej%2Fuploads%2F7Y0Cj8sUPyPbV66dNpJi%2FScreenshot%202022-05-03%20at%2017.26.10.png?alt=media\&token=955d42cc-84c9-4d2e-8b21-e0d955d11c36)

* Assign to the **ServerManager** `script` both the **GameClient** and the newly created **ColiseusSettings Scriptable Object**

![](https://2106246176-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAal11zmTbHWEnDONeJej%2Fuploads%2FFq1GPdfXuU2MVADBajqG%2FScreenshot%202022-05-03%20at%2017.14.41.png?alt=media\&token=9c90e749-67d2-474d-8f86-ec6648bb37c2)

#### <mark style="color:blue;">Terminal installation and check</mark>

Now that we have created all the necessary files and steps for Unity, all we need to do is to install the files locally and check that everything is running properly 🕵️‍♂️

Run `npm install` into the server folder from the terminal window

![](https://2106246176-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAal11zmTbHWEnDONeJej%2Fuploads%2FjVbJiEUFm2UjjsbRIGrq%2FScreenshot%202022-05-03%20at%2015.10.04.png?alt=media\&token=a96da8ea-b49d-4c43-b252-6063a3f6c53b)

Run `npm start` in the local server, if doesnt run correctly try to trash node files and reinstall

![](https://2106246176-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAal11zmTbHWEnDONeJej%2Fuploads%2F7oX2jc1YsWYoLxbAHSmV%2FScreenshot%202022-05-03%20at%2015.47.26.png?alt=media\&token=1c1b48c3-6116-4159-b12f-61d8b3c68e90)

**You should now see the server logging in a message to the client and the client logging in a message to the server! All done for the basic setup!**\
\
From here on you can implement more complex functions between the two

{% hint style="info" %}
Disable secure protocol while testing locally on localhost:2567 in your Unity
{% endhint %}
