Hi, I’m trying to build my own NetworkManager, and to do this I’ve been looking at the source through ILSpy, but I’ve been having trouble with ClientScene.ConnectLocalServer() and I can’t find many resources on it.
As far as I can tell, on calling this method the client will connect locally to the server, making this user the host. By doing this I should be able to treat the local client exactly like a remote client. I’m having a few issues setting this up.
Here is the current network manager I am using.
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class NetworkingManager : MonoBehaviour {
[SerializeField]
GameObject playerPrefab;
public string host { get; set; }
public string playerName { get; set; }
private NetworkClient client;
void Awake () {
host = "localhost";
playerName = "Iron Warrior";
DontDestroyOnLoad(gameObject);
}
public void Host()
{
Debug.Log("Hosting game...");
NetworkServer.Listen(7777);
NetworkServer.RegisterHandler(MsgType.Connect, OnServerConnect);
Application.LoadLevel(1);
ClientScene.RegisterPrefab(playerPrefab);
client = ClientScene.ConnectLocalServer();
client.connection.isReady = true;
}
public void Join()
{
Debug.Log("Joining game...");
ClientScene.RegisterPrefab(playerPrefab);
client = new NetworkClient();
client.Connect(host, 7777);
Application.LoadLevel(1);
}
void OnServerConnect(NetworkMessage netMsg)
{
Debug.Log("Client has connected");
NetworkServer.SetClientReady(netMsg.conn);
GameObject player = SpawnPlayer();
NetworkServer.Spawn(player);
}
GameObject SpawnPlayer()
{
Vector3 spawnPoint = GameObject.FindObjectOfType<SpawnHandler>().GetRoundRobinSpawnPoint();
GameObject player = (GameObject)Instantiate(playerPrefab, spawnPoint, Quaternion.identity);
return player;
}
}
To test if everything works I have a short script attached to the playerPrefab GameObject:
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class TestingRPCs : NetworkBehaviour
{
public void Start()
{
if (isServer)
RpcTest();
}
[ClientRpc]
void RpcTest()
{
Debug.Log("RPC call done");
GetComponent<Renderer>().material.color = Color.red;
}
}
What I thought should happen would be that the local client would connect, triggering the OnClientConnect message, which would spawn a player across the network, which would then trigger the RpcTest method. Unfortunately the OnClientConnect does not get called. I’ve tried bypassing that method and just spawning a player directly in the Host() method, but the Rpc is not called, so it doesn’t seem to be aware of the local client.
How are the local clients supposed to be setup? I can’t find any examples in the documentation, so I’m not sure what I’m doing wrong.
Thanks for any help,
Erik