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

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.

The methods are accessible by using the singleton object - here’s an example custom network manager from GTGD’s tutorial series:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.Networking;
using System;

public class CustomNetworkManager : NetworkManager
{
    public void StartupHost()
    {
        SetPort();
        SetName();
        singleton.StartHost();
    }

    public void JoinGame()
    {
        SetIPAddress();
        SetPort();
        SetName();
        singleton.StartClient();
    }

    private void SetIPAddress()
    {
        string ipAddress = GameObject.Find("InputFieldIPAddress").transform.FindChild("Text").GetComponent<Text>().text;
        singleton.networkAddress = ipAddress;
    }

    private void SetPort()
    {
        singleton.networkPort = 7777;
    }

    private void OnLevelWasLoaded(int level)
    {
        if (level == 0)
            SetupMenuSceneButtons();
        else
            SetupOtherSceneButtons();
    }

    private void SetupMenuSceneButtons()
    {
        GameObject.Find("ButtonStartHost").GetComponent<Button>().onClick.RemoveAllListeners();
        GameObject.Find("ButtonStartHost").GetComponent<Button>().onClick.AddListener(StartupHost);

        GameObject.Find("ButtonJoinGame").GetComponent<Button>().onClick.RemoveAllListeners();
        GameObject.Find("ButtonJoinGame").GetComponent<Button>().onClick.AddListener(JoinGame);
    }

    private void SetupOtherSceneButtons()
    {
        GameObject.Find("ButtonDisconnect").GetComponent<Button>().onClick.RemoveAllListeners();
        GameObject.Find("ButtonDisconnect").GetComponent<Button>().onClick.AddListener(NetworkManager.singleton.StopHost);
    }
}
1 Like