> For the complete documentation index, see [llms.txt](https://docs.edgegap.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.edgegap.com/fr/docs/sample-projects/unity-netcodes/unity-netcode-on-edgegap.md).

# Unity NGO

Hébergez un projet Unity Netcode for GameObjects sur Edgegap, en suivant l’exemple NGO Boss Room du build au déploiement en direct.

Découvrez comment héberger des projets Unity sur Edgegap à l'aide des techniques du [l’exemple NGO Boss Room](https://unity.com/demos/small-scale-coop-sample).

{% hint style="info" %}
Cet exemple **ne nécessite aucun service Unity Gaming Services (UGS)** , Multiplay ou Relays pour fonctionner.
{% endhint %}

{% embed url="<https://youtu.be/8Qi-wXswjkw>" %}

## ✔️ Préparation

Avant de commencer, vous aurez besoin de :

* Unity 6 - [à télécharger via Unity Hub](https://unity.com/releases/unity-6),
* Projet d’exemple Unity NGO \[Netcode for Game Objects] Boss Room (modifié pour Edgegap) :
  * [à télécharger depuis GitHub](https://github.com/edgegap/netcode-sample-unity-ngo-bossroom).

## ⚡ Déployer et connecter

### 1. Déployer un serveur sur Edgegap

☑️ Pour commencer, vous devrez [créer un compte gratuit chez Edgegap](https://app.edgegap.com/auth/register). Aucune carte de crédit requise.

☑️ [Créer une nouvelle version d’application pour votre application](https://app.edgegap.com/application-management/applications/unity-ngo-bossroom-sample/versions/create), choisissez NGO Boss Room Sample.

☑️ [Déployez un serveur avec la version de votre application NGO Boss Room Sample](https://app.edgegap.com/deployment-management/deployments/list).

☑️ [Ouvrez les détails de votre nouveau déploiement](https://app.edgegap.com/deployment-management/deployments/list) et trouvez vos informations de connexion uniques, à usage unique :

* **Port externe**, par exemple `30854`  (choisi aléatoirement).

☑️ Allez dans l’onglet Logs et trouvez (CTRL+F) vos informations de connexion uniques, à usage unique :

* `ARBITRIUM_PUBLIC_IP` , par exemple IPv4 : `172.234.244.38` .

✅ Vous pouvez maintenant passer à l’étape suivante.

### 2. Se connecter depuis l’éditeur

☑️ Ouvrez votre nouveau projet dans Unity.

☑️ Vérifiez que vous avez ouvert la scène : `Assets/Scenes/MainMenu.unity`.

☑️ Appuyez sur le bouton ▶️ Play pour lancer votre client de jeu :

* appuyez sur **START WITH DIRECT IP** bouton,
* sélectionnez l’onglet **JOIN WITH IP.**

☑️ Saisissez les informations de connexion de l’étape précédente.

☑️ Appuyez sur **JOIN** bouton pour vous connecter à votre serveur.

☑️ Connectez un deuxième joueur virtuel avec [Multiplayer Play Mode](https://docs-multiplayer.unity3d.com/mppm/current/about/) ou [ParrelSync](https://github.com/VeriorPies/ParrelSync).

🙌 Félicitations pour votre premier déploiement sur Edgegap !

## ✏️ Personnaliser la build du serveur

{% hint style="success" %}
Voir [Unity](/fr/unity.md) pour Unity afin de **apprendre à créer et personnaliser des serveurs**.
{% endhint %}

### Exécuter en tant que serveur dédié

Pour que cet exemple s’exécute en tant que serveur dédié, nous avons apporté les modifications suivantes :

{% tabs %}
{% tab title="Initialisation du serveur" %}
Nouveau script (ajouté à votre `MainMenu` scène dans un nouveau `GameObject vide`):

<pre class="language-csharp" data-title="Assets/Scripts/EdgegapServerStarter.cs" data-line-numbers><code class="lang-csharp">using System;
using System.Collections;
using System.Collections.Generic;
using Unity.BossRoom.ConnectionManagement;
using UnityEngine;

namespace Unity.Multiplayer.Samples.BossRoom
{
    public class EdgegapServerStarter : MonoBehaviour
    {
<strong>        public string portMapName = <a data-footnote-ref href="#user-content-fn-1">"gameport"</a>;
</strong>
        // Start est appelé avant la première mise à jour de frame
        void Start()
        {
            if (Application.isBatchMode)
            {
                ConnectionManager connectionManager = GameObject.Find("ConnectionManager").GetComponent&#x3C;ConnectionManager>();
<strong>                string internalPortAsStr = Environment.GetEnvironmentVariable($"ARBITRIUM_PORT_{portMapName.ToUpper()}_INTERNAL");
</strong>
                if (internalPortAsStr == null || !ushort.TryParse(internalPortAsStr, out ushort port))
                {
                    throw new Exception($"Could not find port mapping, make sure your app version port name matches with \"{portMapName}\"");
                }

<strong>                connectionManager.StartHostIp("<a data-footnote-ref href="#user-content-fn-2">SERVER</a>", "0.0.0.0", port);
</strong>            }
        }
    }
}
</code></pre>

{% endtab %}

{% tab title="Gestion de la connexion" %}
Fichiers modifiés :

<pre class="language-csharp" data-title="Assets/Scripts/ConnectionManagement/ConnectionMethod.cs"><code class="lang-csharp"><strong>116        public override async Task SetupHostConnectionAsync()
</strong>117        {
<strong>118            <a data-footnote-ref href="#user-content-fn-3">//SetConnectionPayload(GetPlayerId(), m_PlayerName); // Il faut aussi définir la charge utile de connexion pour l’hôte, car l’hôte est aussi un client</a>
</strong>119            var utp = (UnityTransport)m_ConnectionManager.NetworkManager.NetworkConfig.NetworkTransport;
120            utp.SetConnectionData(m_Ipaddress, m_Port);
121        }
</code></pre>

<pre class="language-csharp" data-title="Assets/Scripts/ConnectionManagement/ConnectionState/StartingHostState.cs"><code class="lang-csharp"><strong>66        async void StartHost()
</strong>67        {
              ...

72                // StartHost de NGO lance tout
<strong>--                <a data-footnote-ref href="#user-content-fn-3">//if (!m_ConnectionManager.NetworkManager.StartHost())</a>
</strong><strong>73                <a data-footnote-ref href="#user-content-fn-4">if (!m_ConnectionManager.NetworkManager.StartServer())</a>
</strong>74                {
75                    StartHostFailed();
76                }

              ...
</code></pre>

<pre class="language-csharp" data-title="Assets/Scripts/Gameplay/GameplayObjects/Character/ClientCharacter.cs"><code class="lang-csharp"><strong>112        public override void OnNetworkSpawn()
</strong>113        {
               ...

125            m_ServerCharacter.IsStealthy.OnValueChanged += OnStealthyChanged;
126            m_ServerCharacter.MovementStatus.OnValueChanged += OnMovementStatusChanged;
<strong>127            <a data-footnote-ref href="#user-content-fn-3">//OnMovementStatusChanged(MovementStatus.Normal, m_ServerCharacter.MovementStatus.Value);</a>
</strong> 
               ...
</code></pre>

{% endtab %}

{% tab title="Compatibilité éditeur/serveur" %}
Modifier les fichiers :

<pre class="language-csharp" data-title="Assets/Scripts/ConnectionManagement/ConnectionState/HostingState.cs"><code class="lang-csharp"><strong>148        ConnectStatus GetConnectStatus(ConnectionPayload connectionPayload)
</strong>149        {
               ...
                
<strong>155            <a data-footnote-ref href="#user-content-fn-5">//if (connectionPayload.isDebug != Debug.isDebugBuild)</a>
</strong><strong>156            //{
</strong><strong>157            //    return ConnectStatus.IncompatibleBuildType;
</strong><strong>158            //}
</strong>
               ...
</code></pre>

{% endtab %}
{% endtabs %}

[^1]: nom de port par défaut sur Edgegap

[^2]: le nom du joueur pour le serveur peut être n’importe quelle valeur

[^3]: p2p uniquement ⇒ commentez ceci

[^4]: démarrez plutôt en tant que serveur dédié

[^5]: Compatibilité avec l’éditeur ⇒ commentez ceci
