diff --git a/Assets/Scripts/Runtime/Networking/Server.cs b/Assets/Scripts/Runtime/Networking/Server.cs new file mode 100644 index 0000000..cf36904 --- /dev/null +++ b/Assets/Scripts/Runtime/Networking/Server.cs @@ -0,0 +1,81 @@ +using Unity.Netcode; +using UnityEngine; +using UnityEngine.SceneManagement; +using System; +using System.Collections; + +public class Server : MonoBehaviour +{ + [SerializeField] private string multiplayerSceneName = "MultiplayerScene"; // Name of the scene to load + + void Start() + { + // Start the server automatically in a server build + if (IsServerBuild()) + { + StartServer(); + } + } + + void StartServer() + { + // Ensure NetworkManager is initialized + if (NetworkManager.Singleton == null) + { + Log("NetworkManager is not initialized. Cannot start server."); + return; + } + + // Subscribe to the client connected event + NetworkManager.Singleton.OnClientConnectedCallback += OnClientConnected; + + // Start the server + NetworkManager.Singleton.StartServer(); + Log("Server started!"); + + // Start the coroutine to load the MultiplayerScene after 3 seconds + StartCoroutine(LoadMultiplayerSceneAfterDelay(3)); + } + + IEnumerator LoadMultiplayerSceneAfterDelay(float delay) + { + Log($"Waiting for {delay} seconds before loading the MultiplayerScene..."); + yield return new WaitForSeconds(delay); // Wait for the specified delay + + // Load the MultiplayerScene + Log($"Loading scene: {multiplayerSceneName}"); + NetworkManager.Singleton.SceneManager.LoadScene(multiplayerSceneName, LoadSceneMode.Single); + } + + void OnClientConnected(ulong clientId) + { + string message = $"Player with Client ID {clientId} connected."; + Log(message); + } + + void OnApplicationQuit() + { + // Unsubscribe from the event to avoid memory leaks + if (NetworkManager.Singleton != null) + { + NetworkManager.Singleton.OnClientConnectedCallback -= OnClientConnected; + } + Log("Server is quitting..."); + } + + bool IsServerBuild() + { +#if UNITY_SERVER + return true; +#else + return false; +#endif + } + + // Log to terminal + void Log(string message) + { + string logMessage = $"{DateTime.Now}: {message}"; + Console.WriteLine(logMessage); // Log to terminal + } +} \ No newline at end of file diff --git a/Assets/Scripts/Runtime/Networking/Server.cs.meta b/Assets/Scripts/Runtime/Networking/Server.cs.meta new file mode 100644 index 0000000..17d3dae --- /dev/null +++ b/Assets/Scripts/Runtime/Networking/Server.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 881cf6d09045dad41ba8aabe5172101d \ No newline at end of file