So I’m trying to learn the ropes of UNet after years of using legacy networking, I’ll be looking into other solutions soon after I give UNet its fair shot
I’m used to the minimal component setup and prefer doing it all through script. I’ve been referencing the MasterServer example and what I can from the documentation.
I have a basic scene setup:
NetworkHandler script on a gameobject
CubeUpdate script on a cube, along with a NetworkIdentity checked as ‘server only’
This cube needs to be a scene object. I heard there were bugs with [ClientRpc] on scene objects, but then I also read it was fixed in 5.1.3, which I am on.
I believe the reason it isn’t working is I see no observers listed when connected and viewing the inspector of the cube. Most likely related to NetworkServer.SpawnObjects() doing nothing on the client, I have to manually turn the game objects on.
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class NetworkHandler : NetworkBehaviour
{
NetworkClient myClient;
//called by ui button
public void startServer()
{
NetworkServer.Listen(4444);
print("Server started");
NetworkServer.RegisterHandler(MsgType.Connect, OnServerConnect);
//only working on server?
NetworkServer.SpawnObjects();
}
//called by ui button
public void connect()
{
myClient = new NetworkClient();
myClient.Connect("localhost", 4444);
print("connected");
myClient.RegisterHandler(MsgType.Connect, OnClientConnect);
}
void OnServerConnect(NetworkMessage netMsg)
{
//works
print("Client connected");
}
void OnClientConnect(NetworkMessage netMsg)
{
//manually turn on scene objects...
//no observers though
NetworkIdentity[] nis = Resources.FindObjectsOfTypeAll<NetworkIdentity>();
for (int i = 0; i < nis.Length; i++) {
nis[i].gameObject.SetActive(true);
nis[i].RebuildObservers(true);
}
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class CubeUpdate : NetworkBehaviour {
//goal move a cube in the editor on the server and
//see it move on the client window...
[ServerCallback]
void Update () {
print("sending");
RpcUpdateCubePos(transform.position);
}
[ClientRpc]
void RpcUpdateCubePos(Vector3 pos)
{
//doesn't print
print("got update");
gameObject.transform.position = pos;
}
}