void Awake() { BSMultiplayerUI ui = BSMultiplayerUI._instance; playerPlaceText = ui.CreateWorldText(transform, ""); playerPlaceText.rectTransform.anchoredPosition = new Vector2(2.5f, 0f); playerPlaceText.fontSize = 8f; playerNameText = ui.CreateWorldText(transform, ""); playerNameText.rectTransform.anchoredPosition = new Vector2(4f, 0f); playerNameText.fontSize = 7f; playerScoreText = ui.CreateWorldText(transform, ""); playerScoreText.rectTransform.anchoredPosition = new Vector2(15f, 0f); playerScoreText.fontSize = 8f; }
private void ReceivedFromServer(string[] _data) { foreach (string data in _data) { try { ServerCommand command = JsonUtility.FromJson <ServerCommand>(data); if (command.commandType == ServerCommandType.SetPlayerInfos) { _playerInfos.Clear(); foreach (string playerStr in command.playerInfos) { PlayerInfo player = JsonUtility.FromJson <PlayerInfo>(playerStr); _playerInfos.Add(player); } foreach (PlayerInfoDisplay display in scoreDisplays) { display.UpdatePlayerInfo(null, 0); } lastLocalPlayerIndex = localPlayerIndex; localPlayerIndex = FindIndexInList(playerInfo); if (_playerInfos.Count <= 5) { for (int i = 0; i < _playerInfos.Count; i++) { scoreDisplays[i].UpdatePlayerInfo(_playerInfos[i], FindIndexInList(_playerInfos[i])); } } else { if (localPlayerIndex < 3) { for (int i = 0; i < 5; i++) { scoreDisplays[i].UpdatePlayerInfo(_playerInfos[i], FindIndexInList(_playerInfos[i])); } } else if (localPlayerIndex > _playerInfos.Count - 3) { for (int i = _playerInfos.Count - 5; i < _playerInfos.Count; i++) { scoreDisplays[i - (_playerInfos.Count - 5)].UpdatePlayerInfo(_playerInfos[i], FindIndexInList(_playerInfos[i])); } } else { for (int i = localPlayerIndex - 2; i < localPlayerIndex + 3; i++) { scoreDisplays[i - (localPlayerIndex - 2)].UpdatePlayerInfo(_playerInfos[i], FindIndexInList(_playerInfos[i])); } } } if (lastLocalPlayerIndex != 0 && localPlayerIndex == 0) { TextMeshPro player1stPlaceText = ui.CreateWorldText(transform, "You are number one!"); player1stPlaceText.transform.position = new Vector3(0f, 1f, 12f); player1stPlaceText.fontSize = 10f; Destroy(player1stPlaceText.gameObject, 2f); } } } catch (Exception e) { Console.WriteLine("EXCEPTION ON RECEIVED: " + e); } } StartCoroutine(ReceiveFromServerCoroutine()); }
private void ReceivedFromServer(string[] _data) { foreach (string data in _data) { try { ServerCommand command = JsonUtility.FromJson <ServerCommand>(data); if (command.commandType == ServerCommandType.SetPlayerInfos) { _playerInfos.Clear(); foreach (string playerStr in command.playerInfos) { PlayerInfo player = JsonUtility.FromJson <PlayerInfo>(playerStr); if (!String.IsNullOrEmpty(player.playerAvatar)) { byte[] avatar = Convert.FromBase64String(player.playerAvatar); player.rightHandPos = Serialization.ToVector3(avatar.Take(12).ToArray()); player.leftHandPos = Serialization.ToVector3(avatar.Skip(12).Take(12).ToArray()); player.headPos = Serialization.ToVector3(avatar.Skip(24).Take(12).ToArray()); player.rightHandRot = Serialization.ToQuaternion(avatar.Skip(36).Take(16).ToArray()); player.leftHandRot = Serialization.ToQuaternion(avatar.Skip(52).Take(16).ToArray()); player.headRot = Serialization.ToQuaternion(avatar.Skip(68).Take(16).ToArray()); } _playerInfos.Add(player); } lastLocalPlayerIndex = localPlayerIndex; localPlayerIndex = FindIndexInList(_playerInfos, localPlayerInfo); try { if (_avatars.Count > _playerInfos.Count) { List <AvatarController> avatarsToRemove = new List <AvatarController>(); for (int i = _playerInfos.Count; i < _avatars.Count; i++) { avatarsToRemove.Add(_avatars[i]); } foreach (AvatarController avatar in avatarsToRemove) { _avatars.Remove(avatar); Destroy(avatar.gameObject); } } else if (_avatars.Count < _playerInfos.Count) { for (int i = 0; i < (_playerInfos.Count - _avatars.Count); i++) { _avatars.Add(new GameObject("Avatar").AddComponent <AvatarController>()); } } List <PlayerInfo> _playerInfosByID = _playerInfos.OrderBy(x => x.playerId).ToList(); for (int i = 0; i < _playerInfos.Count; i++) { _avatars[i].SetPlayerInfo(_playerInfosByID[i], (i - FindIndexInList(_playerInfosByID, localPlayerInfo)) * 3f, localPlayerInfo.Equals(_playerInfosByID[i])); } }catch (Exception e) { Console.WriteLine($"AVATARS EXCEPTION: {e}"); } if (_playerInfos.Count <= 5) { for (int i = 0; i < _playerInfos.Count; i++) { scoreDisplays[i].UpdatePlayerInfo(_playerInfos[i], FindIndexInList(_playerInfos, _playerInfos[i])); } for (int i = _playerInfos.Count; i < scoreDisplays.Count; i++) { scoreDisplays[i].UpdatePlayerInfo(null, 0); } } else { if (localPlayerIndex < 3) { for (int i = 0; i < 5; i++) { scoreDisplays[i].UpdatePlayerInfo(_playerInfos[i], FindIndexInList(_playerInfos, _playerInfos[i])); } } else if (localPlayerIndex > _playerInfos.Count - 3) { for (int i = _playerInfos.Count - 5; i < _playerInfos.Count; i++) { scoreDisplays[i - (_playerInfos.Count - 5)].UpdatePlayerInfo(_playerInfos[i], FindIndexInList(_playerInfos, _playerInfos[i])); } } else { for (int i = localPlayerIndex - 2; i < localPlayerIndex + 3; i++) { scoreDisplays[i - (localPlayerIndex - 2)].UpdatePlayerInfo(_playerInfos[i], FindIndexInList(_playerInfos, _playerInfos[i])); } } } if (lastLocalPlayerIndex != 0 && localPlayerIndex == 0) { TextMeshPro player1stPlaceText = ui.CreateWorldText(transform, "You are number one!"); player1stPlaceText.transform.position = new Vector3(0f, 1f, 12f); player1stPlaceText.fontSize = 10f; Destroy(player1stPlaceText.gameObject, 2f); } if (PlayerInfosReceived != null) { PlayerInfosReceived.Invoke(_playerInfos); } } } catch (Exception e) { Console.WriteLine("EXCEPTION ON RECEIVED: " + e); } } StartCoroutine(ReceiveFromServerCoroutine()); }