Search Unity

Deriving from NetworkManager is supposed to be possible, but how?

Discussion in 'Multiplayer' started by Slight0, Aug 16, 2015.

  1. Slight0

    Slight0

    Joined:
    Dec 6, 2013
    Posts:
    28
    The documentation claims you can derive from NetworkManager in order to extend it's functionality and to override some of its callbacks, but how can you do it when it has all engine events like Awake(), OnDestroy(), etc marked as private?

    How am I supposed to propagate engine events to the base class?

    Currently I have to use reflection to get the methods and call them.
     
    Last edited: Aug 16, 2015
  2. Slandercakes

    Slandercakes

    Joined:
    Jul 30, 2015
    Posts:
    15
    The methods are accessible by using the singleton object - here's an example custom network manager from GTGD's tutorial series:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using UnityEngine.Networking;
    5. using System;
    6.  
    7. public class CustomNetworkManager : NetworkManager
    8. {
    9.     public void StartupHost()
    10.     {
    11.         SetPort();
    12.         SetName();
    13.         singleton.StartHost();
    14.     }
    15.  
    16.     public void JoinGame()
    17.     {
    18.         SetIPAddress();
    19.         SetPort();
    20.         SetName();
    21.         singleton.StartClient();
    22.     }
    23.  
    24.     private void SetIPAddress()
    25.     {
    26.         string ipAddress = GameObject.Find("InputFieldIPAddress").transform.FindChild("Text").GetComponent<Text>().text;
    27.         singleton.networkAddress = ipAddress;
    28.     }
    29.  
    30.     private void SetPort()
    31.     {
    32.         singleton.networkPort = 7777;
    33.     }
    34.  
    35.     private void OnLevelWasLoaded(int level)
    36.     {
    37.         if (level == 0)
    38.             SetupMenuSceneButtons();
    39.         else
    40.             SetupOtherSceneButtons();
    41.     }
    42.  
    43.     private void SetupMenuSceneButtons()
    44.     {
    45.         GameObject.Find("ButtonStartHost").GetComponent<Button>().onClick.RemoveAllListeners();
    46.         GameObject.Find("ButtonStartHost").GetComponent<Button>().onClick.AddListener(StartupHost);
    47.  
    48.         GameObject.Find("ButtonJoinGame").GetComponent<Button>().onClick.RemoveAllListeners();
    49.         GameObject.Find("ButtonJoinGame").GetComponent<Button>().onClick.AddListener(JoinGame);
    50.     }
    51.  
    52.     private void SetupOtherSceneButtons()
    53.     {
    54.         GameObject.Find("ButtonDisconnect").GetComponent<Button>().onClick.RemoveAllListeners();
    55.         GameObject.Find("ButtonDisconnect").GetComponent<Button>().onClick.AddListener(NetworkManager.singleton.StopHost);
    56.     }
    57. }
     
    Glabrezu likes this.