Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity Networking:Unity在调用NetworkManager.singleton.StopClient()函数时崩溃_C#_Unity3d_Networking_Stack Overflow_Unity3d Unet - Fatal编程技术网

C# Unity Networking:Unity在调用NetworkManager.singleton.StopClient()函数时崩溃

C# Unity Networking:Unity在调用NetworkManager.singleton.StopClient()函数时崩溃,c#,unity3d,networking,stack-overflow,unity3d-unet,C#,Unity3d,Networking,Stack Overflow,Unity3d Unet,所以基本上,我想做的就是在用户按下按钮时断开连接。 我添加了NetworkManager.singleton.StopClient();函数在从NetworkManager类继承的customNetworkManager脚本上运行。 但每当我按下按钮断开播放器时,unity就会停止响应。 有人在另一篇文章中说,在执行函数时,可能单元陷入了while循环 这是我的剧本: using System.Collections; using System.Collections.Generic; usin

所以基本上,我想做的就是在用户按下按钮时断开连接。 我添加了NetworkManager.singleton.StopClient();函数在从NetworkManager类继承的customNetworkManager脚本上运行。 但每当我按下按钮断开播放器时,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;
}

}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine。联网;
使用UnityEngine.Networking.Match;
使用UnityEngine.UI;
公共类customNetworkManager:NetworkManager{
UIManager UIManager;
void Start()
{
UImanager=gameObject.GetComponent();
}
已选择的公共无效WAN(){
NetworkManager.singleton.StartMatchMaker();
UImanager.showWAN();
}
公共空间StartHost()
{
设置端口();
NetworkManager.singleton.StartHost();
UImanager.showGameOn();
UImanager.showIPAddress();
}
公共游戏()
{
字符串ip=GameObject.Find(“ipfield”).transform.Find(“Text”).GetComponent().Text;
如果(ip==“”)
返回;
setip(ip);
设置端口();
NetworkManager.singleton.StartClient();
UImanager.showGameOn();
}
//调用此方法以请求在服务器上创建匹配
public void CreateInternetMatch()
{
字符串matchName=GameObject.Find(“roomName”).transform.Find(“Text”).GetComponent().Text;
如果(匹配名称==“”)
返回;
NetworkManager.singleton.matchMaker.CreateMatch(matchName,2,true,“”,“”,“”,“”,0,0,OnInternetMatchCreate);
}
public void FindInternetMatch()
{
字符串matchName=GameObject.Find(“roomName”).transform.Find(“Text”).GetComponent().Text;
如果(匹配名称==“”)
返回;
NetworkManager.singleton.matchMaker.ListMatches(0,10,matchName,true,0,0,OnInternetMatchList);
}
InternetMatchCreate上的私有void(布尔成功、字符串扩展信息、MatchInfo匹配信息)
{
如果(成功)
{
//Log(“创建匹配成功”);
MatchInfo主机信息=MatchInfo;
NetworkServer.Listen(主机信息,9000);
NetworkManager.singleton.StartHost(hostInfo);
UImanager.showGameOn();
}
其他的
{
Debug.LogError(“创建匹配失败”);
}
}
公共空间断开连接(){
断开连接();
if(GameObject.FindGameObjectWithTag(“localPlayer”).GetComponent().isServer){
打印(“主机”);
NetworkManager.singleton.StopHost();
}否则{
印刷品(“客户”);
NetworkManager.singleton.StopClient();
}
}
//调用此方法通过媒人查找匹配项
//当返回匹配列表时调用此方法
InternetMatchList上的私有void(bool成功、字符串扩展信息、列表匹配)
{
如果(成功)
{
如果(matches.Count!=0)
{
//Log(“返回了匹配项列表”);
//加入最后一个服务器(以防有两个…)
NetworkManager.singleton.matchMaker.JoinMatch(匹配[matches.Count-1].networkId,“,”,“,”,0,0,OnJoinInternetMatch);
}
其他的
{
Log(“请求的文件室中没有匹配项!”);
}
}
其他的
{
LogError(“无法连接到match maker”);
}
}
//当返回加入匹配的请求时,将调用此方法
私有void OnJoinInternetMatch(bool成功、字符串扩展信息、匹配信息)
{
如果(成功)
{
//Log(“能够加入匹配项”);
MatchInfo主机信息=MatchInfo;
NetworkManager.singleton.StartClient(hostInfo);
UImanager.showGameOn();
}
其他的
{
log错误(“连接匹配失败”);
}
}
无效设置端口()
{
NetworkManager.singleton.networkPort=7777;
}
void setip(字符串地址)
{
NetworkManager.singleton.networkAddress=地址;
}
}

我遇到了这个问题,对没有答案感到失望。我发现,因为我在托管时试图调用StopClient(),所以需要调用StopClient()

我创建这个小程序块是为了根据我是主机、服务器还是客户端调用相应的函数:

NetworkIdentity networkIdentity = GetComponent<NetworkIdentity>();
NetworkManager networkManager = NetworkManager.singleton;

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