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); } } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961