# Unity NGO

Découvrez comment héberger des projets Unity sur Edgegap en utilisant des techniques du [Exemple de salle de boss NGO](https://unity.com/demos/small-scale-coop-sample).

{% hint style="info" %}
Cet exemple **ne nécessite aucun 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 Unity NGO \[Netcode for Game Objects] Boss Room Sample (modifié pour Edgegap) :
  * [télécharger depuis Github](https://github.com/edgegap/netcode-sample-unity-ngo-bossroom).

## ⚡ Déployer et se 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éez 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 et temporaires :

* **Port externe** au format `30854`  (5 chiffres).

☑️ Naviguez vers l'onglet Logs et trouvez (CTRL+F) vos informations de connexion uniques et temporaires :

* `ARBITRIUM_PUBLIC_IP` au format `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 démarrer votre client de jeu :

* appuyez sur **DÉMARRER AVEC IP DIRECTE** bouton,
* sélectionnez l'onglet **REJOINDRE AVEC IP.**

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

☑️ Appuyez sur **REJOINDRE** 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](/docs.edgegap.com-fr/unity.md) pour Unity afin de **apprendre comment construire et personnaliser des serveurs**.
{% endhint %}

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

Pour que cet exemple fonctionne 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 nouvel objet `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 du 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($"Impossible de trouver le mappage de port, assurez-vous que le nom du port de votre version d'application correspond à \"{portMapName}\"");
                }

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

{% endtab %}

{% tab title="Gestion des connexions" %}
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); // Nécessaire de définir la charge utile de connexion pour l'hôte aussi, 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                // Le 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émarrer en tant que serveur dédié à la place

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.edgegap.com/docs.edgegap.com-fr/docs/sample-projects/unity-netcodes/unity-netcode-on-edgegap.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
