I’m working on a multiplayer game, in which players should be able to pick a class that they want to play. But in the network manager, it is only possible to assign just one player prefab, and I assume I would need more, sinsce classes would all have different models. I’m completely new to unity networking, so if you could help me, I would really eppreciate that. To clarify what I mean with classes, think of Star Wars Battlefront 2, where you can pick what kind of clone you want to be for example.
Here is my code.I use playerPrefabIndex and change this value with button.
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using UnityEngine.UI;
using System;
public class MsgTypes
{
public const short PlayerPrefab = MsgType.Highest + 1;
public class PlayerPrefabMsg : MessageBase
{
public short controllerID;
public short prefabIndex;
}
}
public class NetworkManager_Custom : NetworkManager
{
[SerializeField]
Vector3 playerSpawnPos;
short playerPrefabIndex = 0;
// etc.
public void StartUpHost()
{
Debug.Log(playerPrefabIndex + "is host");
SetPort();
NetworkManager.singleton.StartHost();
}
public void JoinGame()
{
Debug.Log(playerPrefabIndex + "is joined");
SetIpAddress();
SetPort();
NetworkManager.singleton.StartClient();
}
private void SetIpAddress()
{
string ipAddress = GameObject.Find("InputFieldIPAddress").transform.FindChild("Text").GetComponent<Text>().text;
NetworkManager.singleton.networkAddress = ipAddress;
}
void SetPort()
{
NetworkManager.singleton.networkPort = 7777;
}
public void ChangeVRMode()
{
if (playerPrefabIndex == 0)
{
playerPrefabIndex = 1;
}
else
{
playerPrefabIndex = 0;
}
Debug.Log(playerPrefabIndex + "new index");
}
void OnLevelWasLoaded(int level)
{
if (level == 0)
{
SetupMenuSceneButtons();
}
else
{
SetupOtherSceneButtons();
}
}
private void SetupMenuSceneButtons()
{
// GameObject.Find("ButtonStartHost").GetComponent<Button>().onClick.RemoveAllListeners();
// GameObject.Find("ButtonStartHost").GetComponent<Button>().onClick.AddListener(StartUpHost);
}
private void SetupOtherSceneButtons()
{
}
public override void OnStartServer()
{
NetworkServer.RegisterHandler(MsgTypes.PlayerPrefab, OnResponsePrefab);
base.OnStartServer();
}
public override void OnClientConnect(NetworkConnection conn)
{
client.RegisterHandler(MsgTypes.PlayerPrefab, OnRequestPrefab);
base.OnClientConnect(conn);
}
private void OnRequestPrefab(NetworkMessage netMsg)
{
MsgTypes.PlayerPrefabMsg msg = new MsgTypes.PlayerPrefabMsg();
msg.controllerID = netMsg.ReadMessage<MsgTypes.PlayerPrefabMsg>().controllerID;
msg.prefabIndex = playerPrefabIndex;
client.Send(MsgTypes.PlayerPrefab, msg);
}
private void OnResponsePrefab(NetworkMessage netMsg)
{
MsgTypes.PlayerPrefabMsg msg = netMsg.ReadMessage<MsgTypes.PlayerPrefabMsg>();
playerPrefab = spawnPrefabs[msg.prefabIndex];
Debug.Log(playerPrefab.name + " spawned!" + msg.prefabIndex);
base.OnServerAddPlayer(netMsg.conn, msg.controllerID);
}
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
MsgTypes.PlayerPrefabMsg msg = new MsgTypes.PlayerPrefabMsg();
msg.controllerID = playerControllerId;
NetworkServer.SendToClient(conn.connectionId, MsgTypes.PlayerPrefab, msg);
}
// I have put a toggle UI on gameObjects called PC1 and PC2 to select two different character types.
// on toggle, this function is called, which updates the playerPrefabIndex
// The index will be the number from the registered spawnable prefabs that
// you want for your player
//public void UpdatePC()
//{
// if (GameObject.Find("PC1").GetComponent<Toggle>().isOn)
// {
// playerPrefabIndex = 0;
// }
// else if (GameObject.Find("PC2").GetComponent<Toggle>().isOn)
// {
// playerPrefabIndex = 1;
// }
//}
}