using System; using UnityEngine; using UnityEngine.SceneManagement; using Mirror; /* Documentation: https://mirror-networking.gitbook.io/docs/components/network-manager API Reference: https://mirror-networking.com/docs/api/Mirror.NetworkManager.html */ public class #SCRIPTNAME# : NetworkManager { // You can adjust the parameters of the Actions below to suit your needs and pass the values through the Invoke() method. public event Action OnStartAction; public event Action OnDestroyAction; public event Action ServerChangeSceneAction; public event Action OnServerChangeSceneAction; public event Action OnServerSceneChangedAction; public event Action OnClientChangeSceneAction; public event Action OnClientSceneChangedAction; public event Action OnServerConnectAction; public event Action OnServerReadyAction; public event Action OnServerAddPlayerAction; public event Action OnServerDisconnectAction; public event Action OnServerErrorAction; public event Action OnServerTransportExceptionAction; public event Action OnClientConnectAction; public event Action OnClientDisconnectAction; public event Action OnClientNotReadyAction; public event Action OnClientErrorAction; public event Action OnClientTransportExceptionAction; public event Action OnStartServerAction; public event Action OnStopServerAction; public event Action OnStartHostAction; public event Action OnStopHostAction; public event Action OnStartClientAction; public event Action OnStopClientAction; // Overrides the base singleton so we don't have to cast to this type everywhere. public static new #SCRIPTNAME# singleton => (#SCRIPTNAME#)NetworkManager.singleton; /// /// Runs on both Server and Client /// Networking is NOT initialized when this fires /// public override void Awake() { base.Awake(); // Example of adding a handler for the OnStartAction // Multiple handlers can be added for actions // Use -= to remove handlers // Set the action to null to remove all handlers OnStartAction += OnStartedActionHandler; } /// /// Example handler for OnStartAction /// /// Handlers can be assigned from, and exist, in any script public void OnStartedActionHandler() { Debug.Log("#SCRIPTNAME#.OnStartAction invoked"); } #region Unity Callbacks public override void OnValidate() { base.OnValidate(); } /// /// Runs on both Server and Client /// Networking is NOT initialized when this fires /// public override void Start() { OnStartAction?.Invoke(); base.Start(); } /// /// Runs on both Server and Client /// public override void LateUpdate() { base.LateUpdate(); } /// /// Runs on both Server and Client /// public override void OnDestroy() { OnDestroyAction?.Invoke(); base.OnDestroy(); } #endregion #region Start & Stop /// /// Set the frame rate for a headless server. /// Override if you wish to disable the behavior or set your own tick rate. /// public override void ConfigureHeadlessFrameRate() { base.ConfigureHeadlessFrameRate(); } #endregion #region Scene Management /// /// This causes the server to switch scenes and sets the networkSceneName. /// Clients that connect to this server will automatically switch to this scene. This is called automatically if onlineScene or offlineScene are set, but it can be called from user code to switch scenes again while the game is in progress. This automatically sets clients to be not-ready. The clients must call NetworkClient.Ready() again to participate in the new scene. /// /// public override void ServerChangeScene(string newSceneName) { ServerChangeSceneAction?.Invoke(newSceneName); base.ServerChangeScene(newSceneName); } /// /// Called from ServerChangeScene immediately before SceneManager.LoadSceneAsync is executed /// This allows server to do work / cleanup / prep before the scene changes. /// /// Name of the scene that's about to be loaded public override void OnServerChangeScene(string newSceneName) { OnServerChangeSceneAction?.Invoke(newSceneName); } /// /// Called on the server when a scene is completed loaded, when the scene load was initiated by the server with ServerChangeScene(). /// /// The name of the new scene. public override void OnServerSceneChanged(string sceneName) { OnServerSceneChangedAction?.Invoke(sceneName); } /// /// Called from ClientChangeScene immediately before SceneManager.LoadSceneAsync is executed /// This allows client to do work / cleanup / prep before the scene changes. /// /// Name of the scene that's about to be loaded /// Scene operation that's about to happen /// true to indicate that scene loading will be handled through overrides public override void OnClientChangeScene(string newSceneName, SceneOperation sceneOperation, bool customHandling) { OnClientChangeSceneAction?.Invoke(newSceneName, sceneOperation, customHandling); } /// /// Called on clients when a scene has completed loaded, when the scene load was initiated by the server. /// Scene changes can cause player objects to be destroyed. The default implementation of OnClientSceneChanged in the NetworkManager is to add a player object for the connection if no player object exists. /// public override void OnClientSceneChanged() { OnClientSceneChangedAction?.Invoke(); base.OnClientSceneChanged(); } #endregion #region Server System Callbacks /// /// Called on the server when a new client connects. /// Unity calls this on the Server when a Client connects to the Server. Use an override to tell the NetworkManager what to do when a client connects to the server. /// /// Connection from client. public override void OnServerConnect(NetworkConnectionToClient conn) { OnServerConnectAction?.Invoke(conn); } /// /// Called on the server when a client is ready. /// The default implementation of this function calls NetworkServer.SetClientReady() to continue the network setup process. /// /// Connection from client. public override void OnServerReady(NetworkConnectionToClient conn) { OnServerReadyAction?.Invoke(conn); base.OnServerReady(conn); } /// /// Called on the server when a client adds a new player with ClientScene.AddPlayer. /// The default implementation for this function creates a new player object from the playerPrefab. /// /// Connection from client. public override void OnServerAddPlayer(NetworkConnectionToClient conn) { OnServerAddPlayerAction?.Invoke(conn); base.OnServerAddPlayer(conn); } /// /// Called on the server when a client disconnects. /// This is called on the Server when a Client disconnects from the Server. Use an override to decide what should happen when a disconnection is detected. /// /// Connection from client. public override void OnServerDisconnect(NetworkConnectionToClient conn) { OnServerDisconnectAction?.Invoke(conn); base.OnServerDisconnect(conn); } /// /// Called on server when transport raises an error. /// NetworkConnection may be null. /// /// Connection of the client...may be null /// TransportError enum /// String message of the error. public override void OnServerError(NetworkConnectionToClient conn, TransportError transportError, string message) { OnServerErrorAction?.Invoke(conn, transportError, message); } /// /// Called on server when transport raises an exception. /// NetworkConnection may be null. /// /// Connection of the client...may be null /// Exception thrown from the Transport. public override void OnServerTransportException(NetworkConnectionToClient conn, Exception exception) { OnServerTransportExceptionAction?.Invoke(conn, exception); } #endregion #region Client System Callbacks /// /// Called on the client when connected to a server. /// The default implementation of this function sets the client as ready and adds a player. Override the function to dictate what happens when the client connects. /// public override void OnClientConnect() { OnClientConnectAction?.Invoke(); base.OnClientConnect(); } /// /// Called on clients when disconnected from a server. /// This is called on the client when it disconnects from the server. Override this function to decide what happens when the client disconnects. /// public override void OnClientDisconnect() { OnClientDisconnectAction?.Invoke(); } /// /// Called on clients when a servers tells the client it is no longer ready. /// This is commonly used when switching scenes. /// public override void OnClientNotReady() { OnClientNotReadyAction?.Invoke(); } /// /// Called on client when transport raises an error. /// /// TransportError enum. /// String message of the error. public override void OnClientError(TransportError transportError, string message) { OnClientErrorAction?.Invoke(transportError, message); } /// /// Called on client when transport raises an exception. /// /// Exception thrown from the Transport. public override void OnClientTransportException(Exception exception) { OnClientTransportExceptionAction?.Invoke(exception); } #endregion #region Start & Stop Callbacks // Since there are multiple versions of StartServer, StartClient and StartHost, to reliably customize // their functionality, users would need override all the versions. Instead these callbacks are invoked // from all versions, so users only need to implement this one case. /// /// This is invoked when a server is started - including when a host is started. /// StartServer has multiple signatures, but they all cause this hook to be called. /// public override void OnStartServer() { OnStartServerAction?.Invoke(); } /// /// This is called when a server is stopped - including when a host is stopped. /// public override void OnStopServer() { OnStopServerAction?.Invoke(); } /// /// This is invoked when a host is started. /// StartHost has multiple signatures, but they all cause this hook to be called. /// public override void OnStartHost() { OnStartHostAction?.Invoke(); } /// /// This is called when a host is stopped. /// public override void OnStopHost() { OnStopHostAction?.Invoke(); } /// /// This is invoked when the client is started. /// public override void OnStartClient() { OnStartClientAction?.Invoke(); } /// /// This is called when a client is stopped. /// public override void OnStopClient() { OnStopClientAction?.Invoke(); } #endregion }