Advertisement
Dieton

MyNetworkManager

Jun 1st, 2023
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.64 KB | Gaming | 0 0
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.SceneManagement;
  4. using Mirror;
  5.  
  6. /*
  7.     Documentation: https://mirror-networking.gitbook.io/docs/components/network-manager
  8.     API Reference: https://mirror-networking.com/docs/api/Mirror.NetworkManager.html
  9. */
  10.  
  11. public class MyNetworkManager : NetworkManager
  12. {
  13.     // Overrides the base singleton so we don't
  14.     // have to cast to this type everywhere.
  15.     public static new MyNetworkManager singleton { get; private set; }
  16.  
  17.     /// <summary>
  18.     /// Runs on both Server and Client
  19.     /// Networking is NOT initialized when this fires
  20.     /// </summary>
  21.     public override void Awake()
  22.     {
  23.         base.Awake();
  24.         singleton = this;
  25.     }
  26.  
  27.     #region Unity Callbacks
  28.  
  29.     public override void OnValidate()
  30.     {
  31.         base.OnValidate();
  32.     }
  33.  
  34.     /// <summary>
  35.     /// Runs on both Server and Client
  36.     /// Networking is NOT initialized when this fires
  37.     /// </summary>
  38.     public override void Start()
  39.     {
  40.         base.Start();
  41.  
  42.         //NetworkServer.AddPlayerForConnection(conn, player, id);
  43.         //NetworkServer.Spawn(obj, connectionToClient);
  44.         //NetworkIdentity.AssignClientAuthority(conn);
  45.     }
  46.  
  47.     /// <summary>
  48.     /// Runs on both Server and Client
  49.     /// </summary>
  50.     public override void LateUpdate()
  51.     {
  52.         base.LateUpdate();
  53.     }
  54.  
  55.     /// <summary>
  56.     /// Runs on both Server and Client
  57.     /// </summary>
  58.     public override void OnDestroy()
  59.     {
  60.         base.OnDestroy();
  61.     }
  62.  
  63.     #endregion
  64.  
  65.     #region Start & Stop
  66.  
  67.     /// <summary>
  68.     /// Set the frame rate for a headless server.
  69.     /// <para>Override if you wish to disable the behavior or set your own tick rate.</para>
  70.     /// </summary>
  71.     public override void ConfigureHeadlessFrameRate()
  72.     {
  73.         base.ConfigureHeadlessFrameRate();
  74.     }
  75.  
  76.     /// <summary>
  77.     /// called when quitting the application by closing the window / pressing stop in the editor
  78.     /// </summary>
  79.     public override void OnApplicationQuit()
  80.     {
  81.         base.OnApplicationQuit();
  82.     }
  83.  
  84.     #endregion
  85.  
  86.     #region Scene Management
  87.  
  88.     /// <summary>
  89.     /// This causes the server to switch scenes and sets the networkSceneName.
  90.     /// <para>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.</para>
  91.     /// </summary>
  92.     /// <param name="newSceneName"></param>
  93.     public override void ServerChangeScene(string newSceneName)
  94.     {
  95.         base.ServerChangeScene(newSceneName);
  96.     }
  97.  
  98.     /// <summary>
  99.     /// Called from ServerChangeScene immediately before SceneManager.LoadSceneAsync is executed
  100.     /// <para>This allows server to do work / cleanup / prep before the scene changes.</para>
  101.     /// </summary>
  102.     /// <param name="newSceneName">Name of the scene that's about to be loaded</param>
  103.     public override void OnServerChangeScene(string newSceneName) { }
  104.  
  105.     /// <summary>
  106.     /// Called on the server when a scene is completed loaded, when the scene load was initiated by the server with ServerChangeScene().
  107.     /// </summary>
  108.     /// <param name="sceneName">The name of the new scene.</param>
  109.     public override void OnServerSceneChanged(string sceneName) { }
  110.  
  111.     /// <summary>
  112.     /// Called from ClientChangeScene immediately before SceneManager.LoadSceneAsync is executed
  113.     /// <para>This allows client to do work / cleanup / prep before the scene changes.</para>
  114.     /// </summary>
  115.     /// <param name="newSceneName">Name of the scene that's about to be loaded</param>
  116.     /// <param name="sceneOperation">Scene operation that's about to happen</param>
  117.     /// <param name="customHandling">true to indicate that scene loading will be handled through overrides</param>
  118.     public override void OnClientChangeScene(string newSceneName, SceneOperation sceneOperation, bool customHandling) { }
  119.  
  120.     /// <summary>
  121.     /// Called on clients when a scene has completed loaded, when the scene load was initiated by the server.
  122.     /// <para>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.</para>
  123.     /// </summary>
  124.     public override void OnClientSceneChanged()
  125.     {
  126.         base.OnClientSceneChanged();
  127.     }
  128.  
  129.     #endregion
  130.  
  131.     #region Server System Callbacks
  132.  
  133.     /// <summary>
  134.     /// Called on the server when a new client connects.
  135.     /// <para>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.</para>
  136.     /// </summary>
  137.     /// <param name="conn">Connection from client.</param>
  138.     public override void OnServerConnect(NetworkConnectionToClient conn) { }
  139.  
  140.     /// <summary>
  141.     /// Called on the server when a client is ready.
  142.     /// <para>The default implementation of this function calls NetworkServer.SetClientReady() to continue the network setup process.</para>
  143.     /// </summary>
  144.     /// <param name="conn">Connection from client.</param>
  145.     public override void OnServerReady(NetworkConnectionToClient conn)
  146.     {
  147.         base.OnServerReady(conn);
  148.     }
  149.  
  150.     /// <summary>
  151.     /// Called on the server when a client adds a new player with ClientScene.AddPlayer.
  152.     /// <para>The default implementation for this function creates a new player object from the playerPrefab.</para>
  153.     /// </summary>
  154.     /// <param name="conn">Connection from client.</param>
  155.     public override void OnServerAddPlayer(NetworkConnectionToClient conn)
  156.     {
  157.         base.OnServerAddPlayer(conn);
  158.     }
  159.  
  160.     /// <summary>
  161.     /// Called on the server when a client disconnects.
  162.     /// <para>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.</para>
  163.     /// </summary>
  164.     /// <param name="conn">Connection from client.</param>
  165.     public override void OnServerDisconnect(NetworkConnectionToClient conn)
  166.     {
  167.         base.OnServerDisconnect(conn);
  168.     }
  169.  
  170.     /// <summary>
  171.     /// Called on server when transport raises an exception.
  172.     /// <para>NetworkConnection may be null.</para>
  173.     /// </summary>
  174.     /// <param name="conn">Connection of the client...may be null</param>
  175.     /// <param name="exception">Exception thrown from the Transport.</param>
  176.     public override void OnServerError(NetworkConnectionToClient conn, TransportError transportError, string message) { }
  177.  
  178.     #endregion
  179.  
  180.     #region Client System Callbacks
  181.  
  182.     /// <summary>
  183.     /// Called on the client when connected to a server.
  184.     /// <para>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.</para>
  185.     /// </summary>
  186.    
  187.  
  188.     /// <summary>
  189.     /// Called on clients when disconnected from a server.
  190.     /// <para>This is called on the client when it disconnects from the server. Override this function to decide what happens when the client disconnects.</para>
  191.     /// </summary>
  192.     public override void OnClientDisconnect() { }
  193.  
  194.     /// <summary>
  195.     /// Called on clients when a servers tells the client it is no longer ready.
  196.     /// <para>This is commonly used when switching scenes.</para>
  197.     /// </summary>
  198.     public override void OnClientNotReady() { }
  199.  
  200.     /// <summary>
  201.     /// Called on client when transport raises an exception.</summary>
  202.     /// </summary>
  203.     /// <param name="exception">Exception thrown from the Transport.</param>
  204.     public override void OnClientError(TransportError transportError, string message) { }
  205.  
  206.     #endregion
  207.  
  208.     #region Start & Stop Callbacks
  209.  
  210.     // Since there are multiple versions of StartServer, StartClient and StartHost, to reliably customize
  211.     // their functionality, users would need override all the versions. Instead these callbacks are invoked
  212.     // from all versions, so users only need to implement this one case.
  213.  
  214.     /// <summary>
  215.     /// This is invoked when a host is started.
  216.     /// <para>StartHost has multiple signatures, but they all cause this hook to be called.</para>
  217.     /// </summary>
  218.     public override void OnStartHost() { }
  219.  
  220.     /// <summary>
  221.     /// This is invoked when a server is started - including when a host is started.
  222.     /// <para>StartServer has multiple signatures, but they all cause this hook to be called.</para>
  223.     /// </summary>
  224.    
  225.  
  226.     /// <summary>
  227.     /// This is invoked when the client is started.
  228.     /// </summary>
  229.     public override void OnStartClient() { }
  230.  
  231.     /// <summary>
  232.     /// This is called when a host is stopped.
  233.     /// </summary>
  234.     public override void OnStopHost() { }
  235.  
  236.     /// <summary>
  237.     /// This is called when a server is stopped - including when a host is stopped.
  238.     /// </summary>
  239.     public override void OnStopServer() { }
  240.  
  241.     /// <summary>
  242.     /// This is called when a client is stopped.
  243.     /// </summary>
  244.     public override void OnStopClient() { }
  245.  
  246.     #endregion
  247.  
  248.     //HOST BUTTTON
  249.     public void HostButton(int _playerID) {
  250.         StaticVariables.yourPlayerID = _playerID;
  251.         StartHost();
  252.     }
  253.  
  254.     //Client Button
  255.     public void ClientButton(int _playerID) {
  256.         StaticVariables.yourPlayerID = _playerID;
  257.         StartClient();
  258.     }
  259.  
  260.     //Server Button
  261.     public void ServerButton()
  262.     {
  263.         StartServer();
  264.     }
  265.  
  266.     //Server ==== LOOK IN YOU NETWORK MANGER FOR THIS
  267.     public override void OnStartServer() {
  268.         NetworkServer.RegisterHandler<CreateCharacterMessage>(OnCreateCharacter);
  269.     }
  270.  
  271.     ////Client ==== LOOK IN YOU NETWORK MANGER FOR THIS
  272.     public override void OnClientConnect()
  273.     {
  274.         CreateCharacterMessage characterMessage = new CreateCharacterMessage
  275.         {
  276.             myPlayerID = StaticVariables.yourPlayerID
  277.         };
  278.  
  279.         NetworkClient.Send(characterMessage);
  280.     }
  281.  
  282.     //Maker Player
  283.     public void OnCreateCharacter(NetworkConnectionToClient conn, CreateCharacterMessage message)
  284.     {
  285.         GameObject character = Instantiate(spawnPrefabs[message.myPlayerID]);
  286.         NetworkServer.AddPlayerForConnection(conn, character);
  287.     }
  288.  
  289.     //Make Message
  290.     public struct CreateCharacterMessage : NetworkMessage
  291.     {
  292.         public int myPlayerID;
  293.     }
  294. }
  295. public class StaticVariables
  296. {
  297.     public static int yourPlayerID = 0;
  298. }
  299.  
Tags: Unity Mirror
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement