Test de communication de paquets entre un client et un serveur
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ClientHandle.cs 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO.Pipes;
  4. using System.Net;
  5. using UnityEngine;
  6. public class ClientHandle : MonoBehaviour
  7. {
  8. public static void Welcome(Packet _packet)
  9. {
  10. string _msg = _packet.ReadString();
  11. int _myId = _packet.ReadInt();
  12. Debug.Log($"Message from the server: {_msg}");
  13. Client.instance.myId = _myId;
  14. ClientSend.WelcomeReceived();
  15. Client.instance.udp.Connect(((IPEndPoint) Client.instance.tcp.socket.Client.LocalEndPoint).Port);
  16. }
  17. public static void SpawnPlayer(Packet _packet)
  18. {
  19. int _id = _packet.ReadInt();
  20. string _username = _packet.ReadString();
  21. Vector3 _position = _packet.ReadVector3();
  22. Quaternion _rotation = _packet.ReadQuaternion();
  23. GameManager.instance.SpawnPlayer(_id, _username, _position, _rotation);
  24. }
  25. public static void PlayerPosition(Packet _packet)
  26. {
  27. int _id = _packet.ReadInt();
  28. Vector3 _position = _packet.ReadVector3();
  29. GameManager.players[_id].transform.position = _position;
  30. }
  31. public static void PlayerRotation(Packet _packet)
  32. {
  33. int _id = _packet.ReadInt();
  34. Quaternion _rotation = _packet.ReadQuaternion();
  35. GameManager.players[_id].transform.rotation = _rotation;
  36. }
  37. public static void PlayerDisconnected(Packet _packet)
  38. {
  39. int _id = _packet.ReadInt();
  40. Destroy(GameManager.players[_id].gameObject);
  41. GameManager.players.Remove(_id);
  42. }
  43. public static void PlayerHealth(Packet _packet)
  44. {
  45. int _id = _packet.ReadInt();
  46. float _health = _packet.ReadFloat();
  47. GameManager.players[_id].SetHealth(_health);
  48. }
  49. public static void PlayerRespawned(Packet _packet)
  50. {
  51. int _id = _packet.ReadInt();
  52. GameManager.players[_id].Respawn();
  53. }
  54. }