Unity3D.试图在没有权限的情况下发送命令给对象 [英] Unity3D. Trying to send command for object without authority

查看:732
本文介绍了Unity3D.试图在没有权限的情况下发送命令给对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于多人回合的策略游戏,需要一名游戏管理员来控制当前的游戏状态(轮到谁等).该管理器对于每个客户端都应该是通用的,它的状态应该在服务器上同步.

I have a multiplayer turn-based strategy game that needs a game manager, controlling current game state (who's turn it is etc.). This manager should be common for every client, it's state should be synchronized on server.

这是我的操作方式: 游戏管理器对象是NetworkBehaviour,它具有NetworkIdentity,它既不是本地玩家权限也不是服务器权限.我做了一个自定义的NetworkManager,它在客户端连接上生成了Game Manager,还测试了它是否是服务器.这是代码:

Here's how I'm doing this: The game manager object is NetworkBehaviour and it has NetworkIdentity which is not local player authority nor server authority. I've made a custom NetworkManager and it spawns the Game Manager on client connect, also testing if it is a server. Here's a code:

public override void OnClientConnect(NetworkConnection conn)
    {
        ClientScene.Ready(conn);
        if (NetworkServer.active)
        {
            var manager = Instantiate(MultiplayerManagerPrefab, Vector3.zero, Quaternion.identity) as GameObject;
            var tacticsManager = manager.GetComponent<MultiplayerManagerModel>();
            NetworkServer.RegisterHandler(MsgType.AddPlayer, tacticsManager.CreatePlayerOnServer);
            NetworkServer.Spawn(manager);
        }
        ClientScene.AddPlayer(0);
    }

当我在服务器上运行它时,它可以正常工作,它在客户端上创建一个实例,并将变量从服务器同步到客户端.但是,当我尝试从客户端运行命令时,它将忽略它们,并发出以下警告:

When I run it on a server it works fine, it creates an instance on a client and synchronizes variables from server to client. But when I try to run commands from client it ignores them, throwing this warning:

试图在没有权限的情况下发送对象的命令. UnityEngine.Networking.NetworkBehaviour:SendCommandInternal(NetworkWriter, Int32,字符串)

Trying to send command for object without authority. UnityEngine.Networking.NetworkBehaviour:SendCommandInternal(NetworkWriter, Int32, String)

请注意,此游戏管理器先于任何玩家生成,因为它必须负责生成玩家.我在做什么错了?

Note that this Game Manager is spawned before any player is because it must be responsible for spawning players. What am I doing wrong?

推荐答案

在代码片段旁边,警告

尝试针对未经授权的对象发送命令.

意思是:是您从对象(其<玩家>没有权限)发送命令.

Means that: you are sending command from an object whose authority, your (player) don't have.

Unity文档说明: 命令从客户端上的播放器对象发送到服务器上的播放器对象.为了安全起见,命令只能从您的播放器对象发送,因此您无法控制其他播放器的对象.

What Unity docs states: Commands are sent from player objects on the client to player objects on the server. For security, Commands can only be sent from YOUR player object, so you cannot control the objects of other players.

从Unity版本5.2开始,可以从以下位置发送命令 具有客户权限的非玩家对象.这些对象必须具有 被NetworkServer.SpawnWithClientAuthority生成或具有 使用NetworkIdentity.AssignClientAuthority设置的权限.指令 从这些对象发送的消息将在该对象的服务器实例上运行, 不在客户端的关联播放器对象上(更多).

Starting with Unity release 5.2 it is possible to send commands from non-player objects that have client authority. These objects must have been spawned with NetworkServer.SpawnWithClientAuthority or have authority set with NetworkIdentity.AssignClientAuthority. Commands sent from these object are run on the server instance of the object, not on the associated player object for the client(more).

那是什么解决方案: 在发送 Command (或执行 command 函数)之前,将该对象的权限分配给播放器.像这样

So what is the solution: Before sending the Command (or executing command function), Assign authority of that object to your Player. Something like this

assignAuthorityObj.GetComponent<NetworkIdentity>().AssignClientAuthority(this.GetComponent<NetworkIdentity>().connectionToClient);

""将代表您的Player对象.调用 Command 后,您可以使用此代码段删除权限.

"this" will be represent to your Player object. After making Command call you can remove authority using this code snippet.

 assignAuthorityObj.GetComponent<NetworkIdentity>().RemoveClientAuthority(this.GetComponent<NetworkIdentity>().connectionToClient);

同样,""将代表您的Player对象.

Again, "this" will be represent to your Player object.

重要说明:我通常使用 OnTriggerEnter 分配对象的权限(如果要使用该权限),并在 OnTriggerExit 上删除权限强>.在特定情况下,您希望在什么情况下获取或删除对象授权.

Important Note: I usually assign Authority of an object (If I want to use that) using OnTriggerEnter and remove authority on OnTriggerExit. It depend on specific scenario that at what event you want to acquire or remove an object authority.

这篇关于Unity3D.试图在没有权限的情况下发送命令给对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆