angelpenguin

networkmanager

Jul 20th, 2025
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. using Unity.Entities;
  2. using Unity.NetCode;
  3. using Unity.Networking.Transport;
  4. using UnityEngine;
  5. using UnityEngine.SceneManagement;
  6.  
  7. public class NetworkManager : MonoBehaviour
  8. {
  9. public static NetworkManager Instance { get; private set; }
  10. public ushort Port = 7979;
  11. [SerializeField] private string gameSceneName = "World";
  12.  
  13. void Awake()
  14. {
  15. if (Instance != null && Instance != this)
  16. {
  17. Destroy(gameObject);
  18. return;
  19. }
  20. Instance = this;
  21. DontDestroyOnLoad(gameObject);
  22. }
  23.  
  24. public void StartServer(bool anyIpv4 = false)
  25. {
  26. var endpoint = anyIpv4
  27. ? NetworkEndpoint.AnyIpv4.WithPort(Port)
  28. : NetworkEndpoint.LoopbackIpv4.WithPort(Port);
  29.  
  30. CleanupOldWorlds();
  31. var serverWorld = ClientServerBootstrap.CreateServerWorld("ServerWorld");
  32. var clientWorld = ClientServerBootstrap.CreateClientWorld("ClientWorld");
  33. World.DefaultGameObjectInjectionWorld = clientWorld;
  34. ScriptBehaviourUpdateOrder.AppendWorldToCurrentPlayerLoop(serverWorld);
  35. ScriptBehaviourUpdateOrder.AppendWorldToCurrentPlayerLoop(clientWorld);
  36.  
  37. // listen
  38. var serverDriver = serverWorld.EntityManager
  39. .CreateEntityQuery(typeof(NetworkStreamDriver))
  40. .GetSingletonRW<NetworkStreamDriver>();
  41. serverDriver.ValueRW.Listen(endpoint);
  42.  
  43. // client always connects loopback for same‑machine play
  44. var clientDriver = clientWorld.EntityManager
  45. .CreateEntityQuery(typeof(NetworkStreamDriver))
  46. .GetSingletonRW<NetworkStreamDriver>();
  47. clientDriver.ValueRW.Connect(clientWorld.EntityManager,
  48. NetworkEndpoint.LoopbackIpv4.WithPort(Port));
  49.  
  50. SceneManager.LoadScene(gameSceneName, LoadSceneMode.Single);
  51.  
  52. Debug.Log($"Server listening on {(anyIpv4 ? GetLocalIp() : "127.0.0.1")}:{Port}");
  53. }
  54.  
  55. public void JoinServer(string ip, ushort port)
  56. {
  57. CleanupOldWorlds();
  58. var clientWorld = ClientServerBootstrap.CreateClientWorld("ClientWorld");
  59. World.DefaultGameObjectInjectionWorld = clientWorld;
  60. ScriptBehaviourUpdateOrder.AppendWorldToCurrentPlayerLoop(clientWorld);
  61.  
  62. var endpoint = NetworkEndpoint.Parse(ip, port);
  63. var clientDriver = clientWorld.EntityManager
  64. .CreateEntityQuery(typeof(NetworkStreamDriver))
  65. .GetSingletonRW<NetworkStreamDriver>();
  66. clientDriver.ValueRW.Connect(clientWorld.EntityManager, endpoint);
  67.  
  68. SceneManager.LoadScene(gameSceneName, LoadSceneMode.Single);
  69. }
  70.  
  71. private void CleanupOldWorlds()
  72. {
  73. for (int i = World.All.Count - 1; i >= 0; i--)
  74. {
  75. var w = World.All[i];
  76. if ((w.Flags & (WorldFlags.Game | WorldFlags.GameServer)) != 0)
  77. w.Dispose();
  78. }
  79. }
  80. string GetLocalIp()
  81. {
  82. foreach (var ip in System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList)
  83. {
  84. if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
  85. return ip.ToString();
  86. }
  87. return "127.0.0.1";
  88. }
  89.  
  90.  
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment