Version: 2017.2

NetworkServer.AddPlayerForConnection

Switch to Manual
public static bool AddPlayerForConnection (Networking.NetworkConnection conn, GameObject player, short playerControllerId);

Parameters

conn Connection which is adding the player.
player Player object spawned for the player.
playerControllerId The player controller ID number as specified by client.

Returns

bool True if player was added.

Description

When an AddPlayer message handler has received a request from a player, the server calls this to associate the player object with the connection.

When a player is added for a connection, the client for that connection is made ready automatically. The player object is automatically spawned, so you do not need to call NetworkServer.Spawn for that object. This function is used for "adding" a player, not for "replacing" the player on a connection. If there is already a player on this playerControllerId for this connection, this will fail.

using UnityEngine;
using UnityEngine.Networking;

class MyServer : MonoBehaviour { public GameObject playerPrefab;

void Start() { NetworkServer.RegisterHandler(MsgType.AddPlayer, OnAddPlayerMessage); }

void OnAddPlayerMessage(NetworkMessage netMsg) { GameObject thePlayer = (GameObject)Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);

// This spawns the new player on all clients NetworkServer.AddPlayerForConnection(conn, thePlayer, 0); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961