Question by
Kalixio · May 11, 2018 at 07:45 AM ·
networkinglobbynetwork instantiate
Spawn prefabs according to player platform with LobbyManager
I'm trying to spawn different prefabs depending on the player platform. So I'm overriding the NetworkLobbyManager to spawn objects the way I want. So I create a Dictionnary that associates a connectionId to an index of a prefab and then I instantiate a prefab according to this index.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class LobbyManager : NetworkLobbyManager
{
private Dictionary<int, int> m_currentPlayers;
void Start()
{
m_currentPlayers = new Dictionary<int, int>();
}
void AddPlayer(NetworkConnection conn)
{
if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
{
m_currentPlayers.Add(conn.connectionId, 0);
}
else if (Application.platform == RuntimePlatform.Android)
{
m_currentPlayers.Add(conn.connectionId, 1);
}
}
public override GameObject OnLobbyServerCreateLobbyPlayer(NetworkConnection conn, short playerControllerId)
{
AddPlayer(conn);
return base.OnLobbyServerCreateLobbyPlayer(conn, playerControllerId);
}
public override GameObject OnLobbyServerCreateGamePlayer(NetworkConnection conn, short playerControllerId)
{
GameObject go = Instantiate(spawnPrefabs[m_currentPlayers[conn.connectionId]]);
NetworkServer.AddPlayerForConnection(conn, go, playerControllerId);
return go;
}
}
But with this code the spawn player on the clients is always the same as the host and I don't know why because it seems like I'm overriding the correct functions and I saw here http://abrgame.blogspot.fr/2016/01/using-unet-to-spawn-different-player.html a guy who used the same technique and that works for him...
Comment