AderKonstantin 07afed99f3 feat: implement basic server functionality
Adds a new Server class to manage multiplayer sessions. The server 
automatically starts in a server build, initializes the NetworkManager, 
and loads the specified multiplayer scene after a delay. It also logs 
client connections and handles cleanup on application quit. This 
enhances the networking capabilities of the application.
2025-01-26 04:01:58 +03:00

81 lines
2.2 KiB
C#

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
}
}