确定新连接的播放器是客户端还是服务器播放器(主机),并删除服务器上的消息

时间:2017-05-11 09:54:09

标签: c# networking unity3d unity3d-unet

我想确定新连接的播放器是否是客户端,服务器播放器(主机)是否删除了一个消息。在UNET中,我希望在LAN上有两名玩家。第一个玩家是服务器玩家,然后他们的对手连接。

播放器搜索LAN匹配,如下所示:

public void StartPlayerClient() {
    Debug.Log ("client set up");
    discovery.Initialize ();
    discovery.StartAsClient ();
    searching = true;
}

一旦找到匹配,它就会显示如下:

void Update() {
    if (searching) {
        if (discovery.broadcastsReceived != null) {
            foreach (var addr in discovery.broadcastsReceived.Keys) {
                var value = discovery.broadcastsReceived [addr];
                //instantiate ui button to connect...
                GameObject ui = Instantiate (GameController.instance.multiplayerMatchConnect) as GameObject;
                ui.GetComponent<ConnectToMatch> ().Initialize (value);
                ui.transform.SetParent (GameController.instance.matchListParent.transform, false);
            }

            if (discovery.broadcastsReceived.Keys.Count > 0) {
                searching = false;
            }
        } else {
            searching = false;
        }
    }
}

然后玩家可以通过点击刚刚实例化的按钮进行连接:

public void Initialize(NetworkBroadcastResult value) {
    res = value;
    GetComponent<Button> ().onClick.AddListener (Connect);

}

void Connect() {
    Debug.Log ("connecting...");
    string dataString = BytesToString (res.broadcastData);
    var items = dataString.Split(':');


    if (items.Length == 3 && items[0] == "NetworkManager") {
        if (NetworkManager.singleton != null && NetworkManager.singleton.client == null) {
            NetworkManager.singleton.networkAddress = items[1];
            NetworkManager.singleton.networkPort = Convert.ToInt32(items[2]);
            NetworkManager.singleton.StartClient();
        }

    }
}

现在一切正常,这些都是麻烦的部分:

//called on the server when client connects
public override void OnServerConnect(NetworkConnection conn) {
    if (NetworkServer.connections.Count > 1) {
        GameController.instance.pnlWaitingForOtherPlayer.SetActive (false);
        Debug.Log ("player connected");
        GameController.instance.pnlMatchList.SetActive (false);
        GameController.instance.pnlJoinCreateMenu.SetActive (false);
    }
}

在服务器客户端上它会在连接之前说“等待其他播放器”..当他们这样做时我想让服务器播放器的消息消失,这就是我试图这样做而且它不起作用。

//called on the client when connects to the server
public override void OnClientConnect(NetworkConnection conn) {
    if (!discovery.isServer) {
        GameController.instance.pnlMatchList.SetActive (false);
        Debug.Log("you connected");
    }
}

0 个答案:

没有答案