Unity自定义Server-Client UDP [英] Unity custom Server-Client UDP

查看:84
本文介绍了Unity自定义Server-Client UDP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个学校项目,我们正在制作一款统一的游戏,它应该是多人游戏.我不允许使用任何内置的服务器/客户端库,如 UNET 或 MIRROR,因为它是网络课程而不是游戏开发.

I am working on a school project where we are making a game in unity and its supposed to be multiplayer. I am not allowed to use any inbuilt server/client libraries like UNET or MIRROR because its a networking course not game dev.

到目前为止,我已经有了一个可以接收和发送数据的工作 UDP .NET 服务器.每当客户端连接时,它都会存储信息 (IPEndPoint),以便将来使用.每当服务器收到来自客户端的数据包时,服务器就会将该数据包发送给其他所有客户端.

So far I've got a working UDP .NET server that can receive and send data. Whenever a client connects it stores the info (IPEndPoint) so that it can be used in the future. Whenever the server receives a packet from a client, the server sends the packet to every other client.

感谢您阅读到这里.服务器是否应该告诉客户端他们拥有哪个 ClientID?我猜数据包必须标有某种 ID 以识别哪个客户端发送了它.同样在 Unity 中,我将如何生成和移动其他客户端游戏对象"?当我收到他们改变位置的数据时.我已经看到 NetworkManager 和 UNET 使用 IsLocalPlayer 来识别它是否是本地播放器,我找不到底层代码,但我想它只是您在实例化对象时设置的一个布尔值?

Thank you for reading this far. Should the Server tell the clients which ClientID they have? I guess the packet has to be marked with some kind of ID to identify which client sent it. Also in Unity, How would i go about spawning and "moving other clients gameobjects" when i recieve data that they have changed position. Ive seen that the NetworkManager and UNET uses IsLocalPlayer to identify if its the local player, i cant find the underlying code but i guess its just a bool you set when instantiating the object?

所以我现在有一个游戏,客户端可以连接到服务器,客户端可以将它的 transform.position 发送到服务器,服务器打印出来并发送给其他客户端.

So what i've got right now is a game, the client can connect to server, the client can send its transform.position to the server, the server prints it out and sends it to other clients.

感谢所有帮助和想法,如果这是一个愚蠢的问题,请告诉我在哪里阅读更多,因为我看到的所有教程都使用网络库,如果我被允许,这就是我会做的.

All help and ideas is appreciated, and if this is a stupid question please tell me where to read more because all tutorial i see use network libraries and thats what i would do if i was allowed to.

推荐答案

你真的面临着漫长的旅程!

You really face a long journey!

服务器是否应该告诉客户端他们拥有哪个 ClientID?

Should the Server tell the clients which ClientID they have?

在服务器端,您应该具备以下能力:

On the server side you should have these abilities:

  1. 发送给所有连接的人

  1. Send to everyone connected

只发送到某个特定的连接

Send only to some specific connection

如果你不解决这个问题,你就会遇到麻烦.

If you don't have that sorted out, you'll have trouble.

服务器是否应该告诉客户端他们拥有哪个 ClientID?

Should the Server tell the clients which ClientID they have?

这是你的选择.服务器可以选择所有的ID"或者客户可以选择自己的 ID".

It's your choice. The server can "choose all the IDs" or the clients can "choose their own id".

一个简单的方法是:当客户端启动时,只需创建一个 UUID 并使用它.因此,在此示例中,客户端选择自己的 ID.与服务器的第一次通信类似于我的 uuid 是 2i23u4y2i34u2y4ui,我的用户名为 Jane Jones".在服务器端,跟踪所有 uuid、连接和昵称等.

A simple approach is: when a client is launched, just create a UUID and use that. So, in this example the client is choosing their own ID. The first communication to the server is something like " my uuid is2i23u4y2i34u2y4ui and my user's name is Jane Jones". On the server side, keep track of all the uuids and connections and nicknames, etc etc.

(如果您还不熟悉 UUID 等数学概念,您将面临漫长的旅程.)

(If you're not already familiar with math concepts like UUIDs, you face a long journey.)

正如我所说,这是您的选择.服务器可以选择所有的ID"或者客户可以选择自己的 ID".

As I say, it's your choice. The server can "choose all the IDs" or the clients can "choose their own id".

"我将如何生成和移动其他客户端游戏对象等

"How would i go about spawning and moving other clients gameobjects etc

那么在 clientA 中,您将有一个名为SpawnARobot(position)"之类的函数

Well in clientA you'd have a function called something like "SpawnARobot(position)"

当命令到达客户端 A 时,您将解析命令.您将有一个很大的 switch 语句来了解如何处理各种命令.您的命令之一可能是spawn".并且当客户端A收到spawn"时命令 clientA 将调用SpawnARobot(position)"

When commands arrive at clientA, you'll parse the commands. You'll have a big switch statement to know how to handle various commands. One of your commands might be "spawn" and when clientA receives a "spawn" command clientA would call "SpawnARobot(position)"

下面是一些命令(即,只是一些文本)到达的例子,然后是相关的函数/被调用的东西:

Here's literally an example of some commands (ie, just some text) arriving, and then the relevant functions/whatever being called:

请注意,您需要了解一些非常基本的概念.

在某些游戏中只有服务器决定"生成某些东西,或者其他什么.客户完全是愚蠢的,基本上只是展示他们被告知的内容".

In some games only the server "decides" that something is spawned, or, whatever. The clients are completely dumb and essentially just "display what they are told".

在其他游戏中,服务器除了传递命令之外并没有真正做任何事情,并且客户端自己决定"应该产生一些东西,或者,无论如何.(这通常是一种rpc"方法.)

In other games the server doesn't really do anything other than pass around commands, and, the clients themselves "decide" that something should be spawned, or, whatever. (That is often an "rpc" approach.)

另外不要忘记......假设你有三部iPhone玩你的游戏.我们说的是服务器".(称之为主控制器")它是这三款 iPhone 之一,所以每个 GameOnAnIPhone 都有你的客户端".但一个(仅)也是服务器"(或主控制器").与此完全相反,您可以从字面上拥有服务器".在云中,即您的代码(很可能是 Node.js)在 AWS 实例上运行,并且所有三个 GameOnAnIPhone 都连接到服务器服务器".

Also don't forget there is a HUGE basic confusion between ... say you have three iPhones playing your Game. We speak of a "server" (call it a "master controller") which is one of those three iPhones, so each GameOnAnIPhone has your "client" but one (only) is also the "server" (or "master controller"). In complete contrast to that you can have literally "a server" in the cloud, ie your code (very likely Node.js) running on an AWS instance, and all three GameOnAnIPhones connect to the "server server".

所有这些概念都非常不同,恐怕你必须理清这些基础知识.

All of these concepts are incredibly different, you have to sort out these basics I'm afraid.

这篇关于Unity自定义Server-Client UDP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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