Thanks a lot!
The Network View did the trick. I set the Observed property to Transform and that did it.
And I noticed that I created the cube inside the OnServerInitialized() which was wrong!
After calling it (btw why is it called twice?) the client can connect but the remote object is not created (at least so it seemed). If i create it later (for now in a GUIbutton) all works as expected.
Last question: how to set Observed to a scripts’ .
Code sofar:
- OnSerializeNetworkView is not used,
- Observed is set to Transform of the Prefab,
- I’m also not sure about the ownercode working. The Jscript sample had @RPC but that does not work in C#:
using UnityEngine;
using System;
using System.Diagnostics;
using System.Collections;
public class ServerScript : MonoBehaviour {
private NetworkPlayer networkPlayer;
private int currentPlayerID;
private GameObject avatar;
private GameObject go;
//Debug Code
public static DateTime FirstTick = DateTime.Now;
public static void Trace() {
_Trace(new StackFrame(1, true).GetMethod().ToString(), null);
}
public static void Trace(string msg) {
_Trace(new StackFrame(1, true).GetMethod().ToString() , msg);
}
public static void _Trace(string method, string msg) {
string Duration = ((TimeSpan)(DateTime.Now - FirstTick)).ToString();
Duration = Duration.Substring(0, Duration.Length - 4); //Removed some detail (msec resolution).
if (msg==null || msg.Length==0) {
print (Duration + ": "+method);
} else {
print (Duration + ": "+method + " - " +msg);
}
}
//Debug Code
// Use this for initialization, called after Awake().
public void Start () {
Trace();
}
// Update is called once per frame.
public void Update () {
//
}
//Put global code here (whats put outside of functions in JScript.
public void Awake () {
Trace();
}
public void OnServerInitialized() {
Trace();
//Where does this end up?
//UnityEngine.Debug.Log("OnServerInitialized");
}
public void OnConnectedToServer() {
Trace();
}
public void OnGUI () {
if (GUI.Button (new Rect (10,10,100,50), "Start server")) {
Trace("Start Server");
Network.InitializeServer(32, 25000);
}
if (GUI.Button (new Rect (10,70,100,50), "Connect")) {
Trace("Connect");
Network.Connect("127.0.0.1", 25000);
}
if (GUI.Button (new Rect (10,140,100,50), "Create")) {
Trace("Create");
GameObject avatar = Resources.Load("MyAvatar") as GameObject;
Vector3 baseposition = new Vector3(10,1,10);
go = Network.Instantiate(avatar, baseposition, transform.rotation, 0) as GameObject;
go.name = "server_object";
}
if (GUI.Button (new Rect (10,200,100,50), "Move")) {
Trace("Move");
go.transform.Translate(0,0,1);
//Network.Connect("127.0.0.1", 25000, "HolyMoly");
}
}
public void RequestOwner(NetworkMessageInfo info) {
Trace();
networkView.RPC( "SetOwner", info.sender, info.sender );
}
public void SetOwner(NetworkPlayer a_owner) {
networkPlayer = a_owner;
currentPlayerID = int.Parse( networkPlayer.ToString() );
Trace("Current PlayerID = "+currentPlayerID.ToString());
}
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info) {
// Always send transform (depending on reliability of the network view)
if (stream.isWriting)
{
Vector3 pos = transform.localPosition;
Quaternion rot = transform.localRotation;
stream.Serialize(ref pos);
stream.Serialize(ref rot);
}
// When receiving, buffer the information
else
{
// Receive latest state information
Vector3 pos = Vector3.zero;
Quaternion rot = Quaternion.identity;
stream.Serialize(ref pos);
stream.Serialize(ref rot);
transform.localRotation = rot;
transform.localPosition = pos;
}
}
}