Version: 2017.3
public void OnServerConnect (Networking.NetworkConnection conn);

参数

conn 来自客户端的连接。

描述

新客户端连接到服务器时在服务器上调用。

Unity 会在客户端连接到服务器时在服务器上调用此方法。执行重载以告知 NetworkManager 在客户端连接到服务器后要执行的操作。

//Attach this script to a GameObject and add a NetworkHUD component to the GameObject.
//Create a Text GameObject (Create>UI>Text) and attach it in the Text field in the Inspector.
//This script changes Text on the screen when a client connects to the server

using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI;

public class OnServerConnectExample : NetworkManager { //Assign a Text component in the GameObject's Inspector public Text m_Text;

//Detect when a client connects to the Server public override void OnServerConnect(NetworkConnection connection) { //Change the text to show the connection and the client's ID m_Text.text = "Client " + connection.connectionId + " Connected!"; } }