GameObject Spawner
Has a Network View. State synchronization = Reliable Delta Compressed. Observed = Spawner (ScriptSpawn)
Has ScriptSpawn component.
GameObject NetworkObject
Has a Network View. State synchronization = Reliable Delta Compressed. Observed = NetworkObject (ScriptObject)
Has ScriptObject component.
Has Box collider component.
Has Rigidbody component.
ScriptSpawn.js
// VARIABLES
public var userPrefab : GameObject;
// EVENTS
function OnServerInitialized () // A USER BECOMES SERVER
{
var name : String = GenerateRandomName ();
var nvid : NetworkViewID = Network.AllocateViewID ();
networkView.RPC ("SpawnUser", RPCMode.AllBuffered, name, nvid);
}
function OnConnectedToServer () // A USER CONNECTS TO THE SERVER
{
var name : String = GenerateRandomName ();
var nvid : NetworkViewID = Network.AllocateViewID ();
networkView.RPC ("SpawnUser", RPCMode.AllBuffered, name, nvid);
}
function OnPlayerDisconnected (player: NetworkPlayer) // A USER DISCONNECTS FROM SERVER
{
Debug.Log("Clean up after player " + player);
Network.RemoveRPCs(player);
Network.DestroyPlayerObjects(player); // DO NOT WORK, MUST FIND SOMETHING ELSE
}
function OnDisconnectedFromServer () // A USER STOPS THE CONNECTION
{
Application.LoadLevel ("Network Ownership Menu");
}
// FUNCTIONS RPC
@RPC
function SpawnUser (name : String, nvid : NetworkViewID, info : NetworkMessageInfo)
{
var clone : GameObject;
clone = Instantiate (userPrefab, transform.position, transform.rotation);
clone.name += name;
clone.networkView.viewID = nvid;
Debug.Log ("Clone created:" + clone.name);
}
// FUNCTIONS
function GenerateRandomName ()
{
return "Clone_" + Random.Range (1,1000);
}
ScriptObject.js
private var screenWidth : float;
private var screenHeight : float;
private var showText : boolean = false;
function Start ()
{
screenWidth = Screen.width;
screenHeight = Screen.height;
}
function OnMouseEnter ()
{
showText = true;
}
function OnMouseExit ()
{
showText = false;
}
function OnGUI ()
{
if (showText)
{
var mousePos : Vector3;
mousePos = Input.mousePosition;
var textToDisplay : String;
textToDisplay = "Object:" + gameObject.name + " // NetworkPlayer:" + networkView.owner + " // Network View ID:" + networkView.viewID;
GUI.Label (Rect (mousePos.x + 15, screenHeight - mousePos.y, 300, 100), textToDisplay);
}
}
function OnSerializeNetworkView (stream : BitStream, info : NetworkMessageInfo)
{
var position : Vector3;
var rotation : Quaternion;
if (stream.isWriting)
{
position = transform.position;
rotation = transform.rotation;
stream.Serialize (position);
stream.Serialize (rotation);
}
else
{
stream.Serialize (position);
stream.Serialize (rotation);
transform.position = position;
transform.rotation = rotation;
}
}
When a user initialize the server, the prefab NetworkObject (a cube) is instantiated.
But when a client connects to the server, there are troubles. Unity running in background stay with one cube, the client (windows stand alone) has two cubes.
ScriptObject shows that the common gameObject in both server and client (at least the cube with same name) do NOT have the same Network View ID.
What is wrong ?
The path to simulate Network.Instantiate is really tough (feel free to tell me it is worthless, if it really is the case, I would nearly glady give up !)…