unity-game-engineunity3d-unet

Implementing a spawn method in UNET


I wanted to implement a spawn method of my own in NetworkManager. The code for spawning players is shown below. Can someone tell me how to implement this in NetworkManager by overriding the normal spawn function.

public void Spawn()
 {
         int i = 1;
         foreach (Transform child in PlayerFormation) 
         {
             var player = ObjectPooler.GetPooledObject(PLAYER_PREFAB_PATH);
             player.name = "Player ("+i+")";
             player.transform.SetParent(child);
             player.SetActive(true);                          
             i++;
         }
         i=0;       
 }     

The position to be spawned are shown in screenshot. enter image description here


Solution

  • The function "OnServerAddPlayer()" is virtual and can therefore be overridden. Just make a class derive from NetworkManager, override the method, and put your custom NetworkManager in the script slot in the NetworkManager component in unity.

    To see the base method see the following unity documentation.