简体   繁体   English

Unity:从客户端生成对象到所有客户端

[英]Unity: Spawn object from client to all clients

I have a Player that shoots Bullets, but the Bullets are only being Spawned to all clients when the Player that shoots is the Host.我有一个发射子弹的玩家,但是只有当发射子弹的玩家是主机时,子弹才会被生成给所有客户端。

Using Unity 5.6使用Unity 5.6

What I have so far...到目前为止我所拥有的......

  • I already have the Prefab registered in the NetworkManager.我已经在 NetworkManager 中注册了 Prefab。
  • The Prefab has a NetworkIdentity with ServerOnly in false and LocalPlayerAuthority in false . Prefab 有一个NetworkIdentityServerOnlyfalseLocalPlayerAuthorityfalse
  • The Prefab has a NetworkTransform with a NetworkSendRate = 0 and a TransformSyncMode to Sync RigidBody2D . Prefab 有一个NetworkTransform和一个NetworkSendRate = 0和一个TransformSyncMode同步 RigidBody2D
  • The Player shoots the Bullet with a method [Command] and using NetworkServer.Spawn (bulletInstance)玩家使用[Command]方法和NetworkServer.Spawn (bulletInstance)发射子弹
    • Before the NetworkServer.Spawn is called I assign the velocity to the RigidBody2D of the bulletInstance.在调用NetworkServer.Spawn之前,我将速度分配给bulletInstance的 RigidBody2D。
    • The Bullet Script Class is a NetworkBehaviour Bullet Script 类是一种网络行为
    • Player has NetworkIdentity with LocalPlayerAuthority in true播放器具有NetworkIdentityLocalPlayerAuthority为 true
    • Player Script Class is a NetworkBehaviour播放器脚本类是一种网络行为

在此处输入图片说明

In Unet,在乌内特,

1. Only the SERVER can spawn any object - end of story 1. 只有 SERVER 可以生成任何对象 - 故事结束

In Unet, servers spawn objects and that's it.在 Unet 中,服务器生成对象,仅此而已。 End of story.故事结束。

2. You HAVE TO HAVE "player" objects. 2. 你必须有“玩家”对象。 Every client absolutely MUST have a "player" object.每个客户端绝对必须有一个“玩家”对象。

It's totally normal that the "player" object is not actually a tank or biped - but is simply an empty game object . “玩家”对象实际上不是坦克或两足动物——而只是一个空的游戏对象,这是完全正常的。 (Sometimes called an "abstract player object"). (有时称为“抽象玩家对象”)。

Every client MUST have a "player object" - often just an empty game object每个客户端都必须有一个“玩家对象”——通常只是一个空的游戏对象

3. Regarding spawning objects which have client authority - see point 1. 3. 关于具有客户端权限的生成对象 - 参见第 1 点。

It's very easy to spawn objects with client authority.生成具有客户端权限的对象非常容易。 Just go to the doco page Manual/UNetSpawning.html and scroll down to Code (CSharp):只需转到 doco 页面 Manual/UNetSpawning.html 并向下滚动到 Code (CSharp):

NetworkServer.SpawnWithClientAuthority(treeGo, conn);

It is explained there (really that's all there is to it).那里有解释(真的就是这样)。

4. Say some specific player, X, will have the authority over the object. 4. 假设某个特定的玩家 X 将拥有该对象的权限。 Of course, you can't spawn the object until X has joined!当然,在 X 加入之前你不能生成对象!

In the line of code just above, people often ask how the hell do you know which "conn" NetworkConnection to use ?在上面的代码行中,人们经常问你到底是怎么知道使用哪个“conn” NetworkConnection

Of course, it is meaningless until X has actually joined the game!当然,直到X真正加入游戏才有意义! See point 2!见第2点!

So you might do things like this...所以你可能会做这样的事情......

Example 1 - only one specific client is your "mothership".示例 1 - 只有一个特定的客户是您的“母舰”。 Of course, the server must, obviously, know the NetworkConnection to that client.当然,很明显,服务器必须知道该客户端的NetworkConnection You'll have it saved in a variable networkConnectionToMothership .您将把它保存在一个变量networkConnectionToMothership Thus, in this example, you have the "conn" to use.因此,在本例中,您可以使用“conn”。

or

Example 2 - a client (say, client JohnSmith) wants to spawn an object which JohnSmith will have authority over.示例 2 - 客户端(例如,客户端 JohnSmith)想要生成一个对象,JohnSmith 将拥有该对象的权限。 Quite simply, the client (JohnSmith) asks the server to spawn the object.很简单,客户端(JohnSmith)要求服务器生成对象。 "Asks the server" simply means send a Command . “询问服务器”只是意味着发送一个Command

Inside any "Command" (ie, running on the server) you literally have the property connectionToClient which gives you the "conn" needed!在任何“命令”(即在服务器上运行)中,您确实拥有属性connectionToClient ,它为您提供所需的“conn”!

So that's the deal.所以这就是交易。

For this specific question, in your code you are calling NetworkServer.spawn .对于这个特定问题,在您的代码中,您正在调用NetworkServer.spawn

You should be calling你应该打电话

NetworkServer.SpawnWithClientAuthority(treeGo, conn);

(Search for that on this doco page: Manual/UNetSpawning.html ) (在此 doco 页面上搜索: Manual/UNetSpawning.html )

and I have explained above in example (2) how you would know what "conn" to use.我在上面的示例 (2) 中已经解释了如何知道要使用什么“conn”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM