unity-game-engine - Unity UNET - 在玩家对象之外调用 [Command]

标签 unity-game-engine network-programming unity-networking

所以我知道 [command] 不适用于非玩家对象,但是......为什么?我该如何同步非玩家对象的数据,例如 NPC 位置。在非玩家对象上调用 Command 的能力将节省每个客户端上的大量重复计算时间。

有人想出解决这个设计决策的方法吗?

此外,如果不在播放器对象上,SyncVar 似乎也不会同步。

现在我最好的策略是在玩家对象上存储大量数据,并且必须不断从外部类引用它

编辑: 哎呀,我的错误,正如所指出的,SyncVars 只在服务器上更新时才适用于非玩家对象

最佳答案

是的,[命令]用于将玩家 RPC 发送到服务器。

但是,SyncVars 会将任何具有 NetworkBehaviour 的对象(不仅仅是玩家对象)上的状态从服务器同步到客户端。

权威服务器方法是让服务器使用 NetworkServer.Spawn() 生成您的 NPC 位置,然后更新 NPC Syncvars,它们会自动更新所有客户。

如果您确实需要从客户端发送[命令],则使用新版本的 Unity,您可以使用 AssignClientAuthority() 授予场景对象本地玩家权限:

如果您拥有 PRO 许可证并且可以访问 Unity 5.2 beta 版本,那么他们会在 5.2b1 中添加此内容

Networking: Added support for client-side authority for non-player objects. The new function NetworkServer.SpawnWithClientAuthority(GameObject obj, NetworkConnection conn) allows authority to be assigned to a client by its connection when an object is created. This would typically be used in a command handler for a client asking to spawn an object, then the client's connection would be passed in. For example:

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

For setting the authority on objects after they are created, there are the new functions AssignClientAuthority(NetworkConnection conn) and RemoveClientAuthority(NetworkConnection conn) on the NetworkIdentity class. Note that only one client can be the authority for an object at a time. On the client that has authority, the function OnStartAuthority() is called, and the property hasAuthority will be true. The set of objects that is owned by a client is available in the new property NetworkConnection.clientOwnedObjects which is a set of NetworkInstanceIds. This set can be used on the server when a message is received to ensure that the client that sent the message actually owns the object. When a client disconnects, the function DestroyPlayersForConnection now destroys all the objects owned by that connection, not just the player object. Objects which have their authority set to a client must have LocalPlayerAuthority set in their NetworkIdentity. Previously only player objects could have local authority, which meant that the NetworkTransform (and other components) could only be used for controlling the single player object for a connection on a client. With these changes it is possible to have multiple objects on a client that are locally controlled.

关于unity-game-engine - Unity UNET - 在玩家对象之外调用 [Command],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31666024/

相关文章:

c# - Unity3D 事件管理器 : Dictionary with Events of generic Type that derives from an interface

tcp - 类似 Nagle 的问题

javascript - 在 Unity 中创建交互式 UI

unity-game-engine - unity3d 4.1.2 构建的独立窗口在退出应用程序时崩溃

linux - client connect()先于server accept()成功,client如何知道server accept()成功呢?

c# - 没有服务器的Unity3D内置多人游戏?

c# - Unity Networking-UNet 中的附加场景加载

unity-game-engine - 在Unity中,如何在不使用播放器对象的情况下从客户端向服务器发送消息(同步变量,调用命令)

c# - Unity - 让对象消失一段时间然后再次显示

python - 了解 ping 延迟