Version: 2017.1

Network.CloseConnection

切换到手册
public static void CloseConnection (NetworkPlayer target, bool sendDisconnectionNotification);

描述

关闭与另一个系统的连接。

target 定义了要关闭的位于连接对侧的系统。如果是客户端,唯一可能的连接是 服务器连接,如果是服务器,目标玩家将被强制下线。 sendDisconnectionNotification 启用或禁用向对方发送通知。如果禁用,连接直接关闭,否则,将向对方可靠发送断开连接通知,然后再关闭连接。

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { void OnGUI() { if (GUILayout.Button("Disconnect from server")) if (Network.connections.Length == 1) { Debug.Log("Disconnecting: " + Network.connections[0].ipAddress + ":" + Network.connections[0].port); Network.CloseConnection(Network.connections[0], true); } else if (Network.connections.Length == 0) Debug.Log("No one is connected"); else if (Network.connections.Length > 1) Debug.Log("Too many connections. Are we running a server?"); if (GUILayout.Button("Disconnect first player")) if (Network.connections.Length > 0) { Debug.Log("Disconnecting: " + Network.connections[0].ipAddress + ":" + Network.connections[0].port); Network.CloseConnection(Network.connections[0], true); } } }