private void OnBecameInvisible() { IsDie = true; Destroy(this.gameObject); if (_diePanel != null) { _diePanel.SetActive(true); } DiePlayer?.Invoke(); }
void OnReceived(IAsyncResult result) { // this is what had been passed into BeginReceive as the second parameter: UdpClient socket = result.AsyncState as UdpClient; // points towards whoever had sent the message: IPEndPoint source = new IPEndPoint(0, 0); // get the actual message and fill out the source: byte[] message = socket.EndReceive(result, ref source); // do what you'd like with `message` here: string returnData = Encoding.ASCII.GetString(message); Debug.Log("Got this: " + returnData); latestMessage = JsonUtility.FromJson <Message>(returnData); try { switch (latestMessage.cmd) { case commands.NEW_CLIENT: lastestNewPlayer = JsonUtility.FromJson <NewPlayer>(returnData); newPlayerSpawned = true; break; case commands.UPDATE: lastestGameState = JsonUtility.FromJson <GameState>(returnData); break; case commands.LOST_CLIENT: lastestLostPlayer = JsonUtility.FromJson <DiePlayer>(returnData); break; case commands.UNIQUE_CLIENT_ID: uniqueID = JsonUtility.FromJson <playerUniqueID>(returnData); break; default: Debug.Log("Error"); break; } } catch (Exception e) { Debug.Log(e.ToString()); } // schedule the next receive operation once reading is done: socket.BeginReceive(new AsyncCallback(OnReceived), socket); }