简体   繁体   中英

How do you stop a client/host in Unity Netcode for Gameobjects?

I managed to get some kind of multiplayer working with Unity Netcode which I understand is very new, but I'm running into a pretty big issue. I can't seem to be able to disconnect from the server instance. This causes an issue in which I load the game scene and the NetworkManager instance is spawned. Then I return to the main menu scene and load the game scene again, and now there are 2 NetworkManager instances which obviously causes errors.

I read in the docs that you can use this:

public void Disconnect()
{
    if (IsHost) 
    {
        NetworkManager.Singleton.StopHost();
    }
    else if (IsClient) 
    {
        NetworkManager.Singleton.StopClient();
    }
    else if (IsServer) 
    {
        NetworkManager.Singleton.StopServer();
    }
    
    UnityEngine.SceneManagement.SceneManager.LoadScene("MainMenu");
}

However all these stop functions don't seem to exist at all under the singleton. Am I missing a library or something? For now I am using a workaround which is just disabling "Don't destroy" and then the NetworkManager is destroyed after switching scenes, but what if I wanted to keep it after switching a scene? Is there no way to control when the Server/Client is stopped? I'm assuming this is a bug but I just want to make sure.

void Disconnect()
{
    NetworkManager.Singleton.Shutdown();
}

it's automaticly shutdown connection for host and client

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM