【问题标题】:Unity Networking: Unity crashes when I call the NetworkManager.singleton.StopClient() functionUnity Networking:当我调用 NetworkManager.singleton.StopClient() 函数时,Unity 崩溃
【发布时间】:2018-02-20 02:35:37
【问题描述】:

所以基本上,我要做的就是在用户按下按钮时断开用户的连接。 我添加了 NetworkManager.singleton.StopClient();我的 customNetworkManager 脚本上的函数,该脚本继承自 NetworkManager 类。 但是每当我按下按钮断开播放器时,unity 就会停止响应。 有人在另一篇文章中说,单元在执行函数时可能会卡在 while 循环中

这是我的脚本:

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

public class customNetworkManager : NetworkManager {
UIManager UImanager;
void Start()
{
    UImanager = gameObject.GetComponent<UIManager> ();
}
public void SelectedWan(){
    NetworkManager.singleton.StartMatchMaker();
    UImanager.showWAN ();
}
public void StartHost()
{
    Setport ();
    NetworkManager.singleton.StartHost ();
    UImanager.showGameOn ();
    UImanager.showIPAddress ();
}
public void JoinGame()
{
    string ip = GameObject.Find ("ipfield").transform.Find ("Text").GetComponent<Text> ().text;
    if (ip == "")
        return;
    setip (ip);
    Setport ();
    NetworkManager.singleton.StartClient ();
    UImanager.showGameOn ();
}
//call this method to request a match to be created on the server
public void CreateInternetMatch()
{
    string matchName= GameObject.Find ("roomName").transform.Find ("Text").GetComponent<Text> ().text;
    if (matchName == "")
        return;
    NetworkManager.singleton.matchMaker.CreateMatch(matchName, 2, true, "", "", "", 0, 0, OnInternetMatchCreate);
}

public void FindInternetMatch()
{
    string matchName= GameObject.Find ("roomName").transform.Find ("Text").GetComponent<Text> ().text;
    if (matchName == "")
        return;
    NetworkManager.singleton.matchMaker.ListMatches(0, 10, matchName, true, 0, 0, OnInternetMatchList);
}

private void OnInternetMatchCreate(bool success, string extendedInfo, MatchInfo matchInfo)
{
    if (success)
    {
        //Debug.Log("Create match succeeded");

        MatchInfo hostInfo = matchInfo;
        NetworkServer.Listen(hostInfo, 9000);

        NetworkManager.singleton.StartHost(hostInfo);
        UImanager.showGameOn ();
    }
    else
    {
        Debug.LogError("Create match failed");
    }
}
public void disconnect(){
    Network.Disconnect ();
    if (GameObject.FindGameObjectWithTag ("localPlayer").GetComponent<WizardScriptNet> ().isServer) {
        print ("host");
        NetworkManager.singleton.StopHost();
    } else {
        print ("client");
        NetworkManager.singleton.StopClient();
    }

}

//call this method to find a match through the matchmaker

//this method is called when a list of matches is returned
private void OnInternetMatchList(bool success, string extendedInfo, List<MatchInfoSnapshot> matches)
{
    if (success)
    {
        if (matches.Count != 0)
        {
            //Debug.Log("A list of matches was returned");

            //join the last server (just in case there are two...)
            NetworkManager.singleton.matchMaker.JoinMatch(matches[matches.Count - 1].networkId, "", "", "", 0, 0, OnJoinInternetMatch);

        }
        else
        {
            Debug.Log("No matches in requested room!");
        }
    }
    else
    {
        Debug.LogError("Couldn't connect to match maker");
    }
}

//this method is called when your request to join a match is returned
private void OnJoinInternetMatch(bool success, string extendedInfo, MatchInfo matchInfo)
{
    if (success)
    {
        //Debug.Log("Able to join a match");

        MatchInfo hostInfo = matchInfo;
        NetworkManager.singleton.StartClient(hostInfo);
        UImanager.showGameOn ();
    }
    else
    {
        Debug.LogError("Join match failed");
    }
}

void Setport ()
{
    NetworkManager.singleton.networkPort = 7777;
}

void setip(string Address)
{
    NetworkManager.singleton.networkAddress = Address;
}

}

【问题讨论】:

    标签: c# unity3d networking stack-overflow unity3d-unet


    【解决方案1】:

    我刚遇到这个问题,很失望没有答案。我发现由于我在托管时尝试调用 StopClient(),因此我需要调用 StopHost()。

    我创建了这个小块来调用适当的函数,具体取决于我是主机、服务器还是客户端:

    NetworkIdentity networkIdentity = GetComponent<NetworkIdentity>();
    NetworkManager networkManager = NetworkManager.singleton;
    
    if (networkIdentity.isServer && networkIdentity.isClient) {
        networkManager.StopHost ();
    } else if (networkIdentity.isServer) {
        networkManager.StopServer();
    } else {
        networkManager.StopClient();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多