//constructs the JoinMessage for the client by reading its device settings private JoinMessage GetJoinMessage() { JoinMessage message = new JoinMessage(); message.prefabId = short.Parse(Encryptor.Decrypt(PlayerPrefs.GetString(PrefsKeys.activeTank))); message.playerName = PlayerPrefs.GetString(PrefsKeys.playerName); return(message); }
/// <summary> /// Override for the callback received on the server when a client requests creating its player prefab. /// Nearly the same as in the UNET source OnServerAddPlayerInternal method, but reading out the message passed in, /// effectively handling user player prefab selection, assignment to a team and spawning it at the team area. /// </summary> public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId, NetworkReader extraMessageReader) { //read the user message JoinMessage message = null; if (extraMessageReader != null) { message = extraMessageReader.ReadMessage <JoinMessage>(); //Debug.Log("The extra string value is: " + message.prefabId + " " + message.playerName); } //read prefab index to spawn out of the JoinMessage the client sent along with its request //then try to get prefab of the registered spawnable prefabs in the NetworkManager inspector (Spawn Info section) GameObject playerObj = null; if (spawnPrefabs.Count > message.prefabId) { playerObj = spawnPrefabs[message.prefabId]; } //debug some errors with incorrect configuration if (playerObj == null) { if (LogFilter.logError) { Debug.LogError("The Player is empty on the NetworkManager. Please check Registered Prefabs + StringMessage object."); } return; } if (playerObj.GetComponent <NetworkIdentity>() == null) { if (LogFilter.logError) { Debug.LogError("The Player does not have a NetworkIdentity. Please add a NetworkIdentity to the player prefab."); } return; } if (playerControllerId < conn.playerControllers.Count && conn.playerControllers[playerControllerId].IsValid && conn.playerControllers[playerControllerId].gameObject != null) { if (LogFilter.logError) { Debug.LogError("There is already a player at that playerControllerId for this connection."); } return; } //get the team value for this player int teamIndex = GameManager.GetInstance().GetTeamFill(); //get spawn position for this team and instantiate the player there Vector3 startPos = GameManager.GetInstance().GetSpawnPosition(teamIndex); playerObj = (GameObject)Instantiate(playerObj, startPos, Quaternion.identity); //assign name (also in JoinMessage) and team to Player component BasePlayer p = playerObj.GetComponent <BasePlayer>(); p.myName = message.playerName; p.teamIndex = teamIndex; //update the game UI to correctly display the increased team size GameManager.GetInstance().size[p.teamIndex]++; GameManager.GetInstance().names[p.teamIndex] = message.playerName; //GameManager.GetInstance().ui.OnTeamSizeChanged(SyncListInt.Operation.OP_DIRTY, p.teamIndex); GameManager.GetInstance().ui.OnTeamNameChanged(SyncListString.Operation.OP_DIRTY, p.teamIndex); //finally map the player gameobject to the connection requesting it NetworkServer.AddPlayerForConnection(conn, playerObj, playerControllerId); //TODO:temporary add default control }