public bool HandleConnect(int connectionId) { //these first 2 lines need rectifying due to obvious errors clientsConnected++; GameObject player = Instantiate(playerModel, new Vector3(0, 0, 0), new Quaternion(0, 0, 0, 0)); ARPlayers.Add(connectionId, player); byte error2; DemoCoder encoder = new DemoCoder(1024); NetworkIdentity[] networkIdentities = getNetworkIdentities(); foreach (NetworkIdentity identity in networkIdentities) { //Debug.Log("Sending spawn state of enemies"); int assetid; spawnAssets.TryGetValue(NetworkTransport.GetAssetId(identity.gameObject), out assetid); encoder.addSerial((Byte)MessageIdentity.Type.Initialise, identity.getObjectId(), assetid, identity.gameObject.transform); if (encoder.isFull()) { NetworkTransport.Send(socketId, connectionId, myReliableChannelId, encoder.getArray(), 1024, out error2); encoder = new DemoCoder(1024); } } NetworkTransport.Send(socketId, connectionId, myReliableChannelId, encoder.getArray(), 1024, out error2); //update all players on game state BroadcastGameStateInfo(gameManager.GetGameState()); return(true); }
// Function for handling a new client connecting to the server public bool HandleConnect(int connectionId) { clientsConnected++; //if the player model is set, then create a new object for the player if (playerModel) { GameObject player = Instantiate(playerModel, new Vector3(0, 0, 0), new Quaternion(0, 0, 0, 0)); } ARPlayers.Add(connectionId, player); //add new player to dictionary, with connection Id as the key and gameobject as the value byte error; /************************************************************************************/ // code for sending the state of the scene when a new client connects Coder encoder = new Coder(1024); NetworkIdentity[] networkIdentities = getNetworkIdentities(); foreach (NetworkIdentity identity in networkIdentities) { Debug.Log("Sending spawn state of enemies"); int assetid; spawnAssets.TryGetValue(NetworkTransport.GetAssetId(identity.gameObject), out assetid); encoder.addSerial((Byte)MessageIdentity.Type.Initialise, identity.getObjectId(), assetid, identity.gameObject.transform); // If the encoder is full then send a message and then refresh the encoder if (encoder.isFull()) { Send(connectionId, myReliableChannelId, encoder.getArray()); encoder = new Coder(1024); } } /************************************************************************************/ Send(connectionId, myReliableChannelId, encoder.getArray()); // Send initialsation packet return(true); }
//on awake, find all the components that should be tracked by checking the spawnables array void Awake() { for (int i = 0; i < spawnables.Length; i++) { String assetId = NetworkTransport.GetAssetId(spawnables[i]); spawnAssets.Add(assetId, i); } }
public void SendSpawn(GameObject gameObject) { byte error2; DemoCoder encoder = new DemoCoder(50); int objectId = gameObject.GetComponent <NetworkIdentity>().getObjectId(); String assetId = NetworkTransport.GetAssetId(gameObject); int assetIdNum; spawnAssets.TryGetValue(assetId, out assetIdNum); encoder.addSerial((byte)MessageIdentity.Type.Spawn, objectId, assetIdNum, gameObject.transform); foreach (KeyValuePair <int, GameObject> kvp in ARPlayers) { NetworkTransport.Send(socketId, kvp.Key, myReliableChannelId, encoder.getArray(), 50, out error2); } }
// Send spawn command to AR players public void SendSpawn(GameObject gameObject) { byte error2; Coder encoder = new Coder(50); int objectId = gameObject.GetComponent <NetworkIdentity>().getObjectId(); // Get the object Id of the object to send String assetId = NetworkTransport.GetAssetId(gameObject); // Get the asset Id of the object to send int assetIdNum; spawnAssets.TryGetValue(assetId, out assetIdNum); // Convert the asset Id into asset number Id encoder.addSerial((byte)MessageIdentity.Type.Spawn, objectId, assetIdNum, gameObject.transform); // Encode the informtaion into a "spawn" message foreach (KeyValuePair <int, GameObject> player in ARPlayers) { Send(player.Key, myReliableChannelId, encoder.getArray()); // Send serialized information down the reliable channel } }