NetworkServer.SpawnWithClientAuthority

切换到手册
public static bool SpawnWithClientAuthority (GameObject obj, GameObject player);
public static bool SpawnWithClientAuthority (GameObject obj, Networking.NetworkConnection conn);
public static bool SpawnWithClientAuthority (GameObject obj, Networking.NetworkHash128 assetId, Networking.NetworkConnection conn);

参数

obj要生成的对象。
player要为其设置客户端权限的玩家对象。
assetId要生成的对象的 assetId。用于自定义生成处理程序。
conn要为其设置客户端权限的连接。

返回

bool 如果已生成对象,则为 true。

描述

此方法会生成一个像 NetworkServer.Spawn() 一样的对象,同时将客户端权限分配到指定客户端。

这与为生成的对象调用 NetworkIdentity.AssignClientAuthority 一样。

using UnityEngine;
using UnityEngine.Networking;

class TestBehaviour : NetworkBehaviour { public GameObject otherPrefab; [Command] public void CmdSpawn() { GameObject go = (GameObject)Instantiate(otherPrefab, transform.position + new Vector3(0, 1, 0), Quaternion.identity); NetworkServer.SpawnWithClientAuthority(go, connectionToClient); } }