photon多人在线

Exit Games is a well-known provider of high-performance network multiplayer server and hosting solutions both inside and outside the Unity development circles. In this tutorial, Exit Games developer Tobias Schweers explains how to use the new Photon Cloud multiplayer network service to create a fun game inspired by the classic children’s game, Marco Polo.

Exit Games是Unity开发圈内外的高性能网络多人服务器和托管解决方案的知名提供商。 在本教程中,退出游戏开发人员Tobias Schweers解释了如何使用新的Photon Cloud多人网络服务来创建一款受经典儿童游戏Marco Polo启发的有趣游戏。

If you like the workflow, you can get the free intro package , including one month of hosting, or you can get a running start with the comprehensive Photon Networking Guide, a starter kit with in-depth tutorials by expert Unity educator and author of the Ultimate Networking Tutorial, Mike Hergaarden. Best of all, it comes with a 6 month, 100 CCU cloud service subscription!

如果您喜欢工作流程,则可以免费获得介绍包 ,其中包括一个月的托管时间,或者可以使用全面的《 光子网络指南》作为入门指南 ,该指南套件是由Unity专业教育家和该书的作者撰写的深入教程。终极网络教程,Mike Hergaarden。 最棒的是,它提供6个月的100 CCU云服务订阅!

制备 (Preparation)

Get the Photon Unity Networking package from the Asset Store. During import, un-check the “DemoWorker” folder (and the complete sample). A PUN “Setup Wizard” will pop up. You can use the free 30 day subscription without obligation. For now, we just enter our mail address and the Wizard does its magic: If the mail address is unknown to the Cloud, we will get an “app ID” right away. If the address is known, we registered it before fetch our “app ID” from the account page. Click “Setup” in the Wizard and copy & paste the app ID”. Save and close the Wizard. We’re ready to use “The Cloud”!

从资产商店获取Photon Unity Networking软件包 。 在导入过程中,请取消选中“ DemoWorker”文件夹(以及完整的示例)。 将弹出一个PUN“设置向导”。 您可以免费使用30天的订阅。 现在,我们只需要输入我们的邮件地址,向导就可以实现它的魔力:如果邮件地址对于Cloud未知,我们将立即获得一个“应用程序ID”。 如果知道该地址,我们先进行注册,然后再从帐户页面获取“应用程序ID”。 在向导中单击“设置”,然后复制并粘贴应用ID。 保存并关闭向导。 我们准备使用“云”!

So, what exactly does this “Photon Cloud” do?!

那么,“光子云”到底是做什么的呢?

Basically, it’s a bunch of PCs with the Photon Server running on them. This “cloud” of servers is maintained by Exit Games and offered as hassle-free service for your multiplayer games. Servers are added on demand, so any number of players can be dealt with. Even though Photon Cloud is not completely free, the costs are low, especially compared to regular hosting, and you can find subscriptions available on the Unity Asset Store . The Photon Cloud is built with “room-based games” in mind, meaning there is a limited number of players (let’s say: less than 10) per match.

基本上,它是一堆运行Photon Server的PC。 服务器的“云”由Exit Games维护,并为您的多人游戏提供无忧的服务。 服务器是按需添加的,因此可以处理任意数量的播放器。 即使Photon Cloud并非完全免费,但成本较低,尤其是与常规托管相比,您可以在Unity Asset Store上找到可用的订阅。 光子云的构建考虑到了“基于房间的游戏”,这意味着每场比赛的玩家人数有限(例如:少于10名)。

Reception: Getting a Room

接待:进入房间

Before we do anything else, we need to get our players into a room where we can move around and let others know about it. Create a folder “Marco Polo” and a new C# script: “RandomMatchmaker”. The most important class in the PUN package is called PhotonNetwork. It’s similar to Unity’s Network class and contains almost all methods we’re going to use. We did the setup with the Wizard, so we can use PhotonNetwork.ConnectUsingSettings() and pass “0.1” as gameVersion. This will read our settings and use them. The gameVersion should be any short string as identification for this client. If started this would get us connected and into the lobby. With a minimum of GUI, the code looks like this:

在执行其他任何操作之前,我们需要将我们的玩家带入一个房间,我们可以四处走动并让其他人知道。 创建一个文件夹“ Marco Polo”和一个新的C#脚本:“ RandomMatchmaker”。 PUN软件包中最重要的类称为PhotonNetwork。 它类似于Unity的Network类,并且包含几乎所有我们将要使用的方法。 我们使用向导进行了设置,因此我们可以使用PhotonNetwork.ConnectUsingSettings()并将“ 0.1”作为gameVersion传递。 这将阅读并使用我们的设置。 gameVersion应该是任何短字符串作为此客户端的标识。 如果开始,这将使我们联系并进入大厅。 使用最少的GUI,代码如下所示:

The script is not yet in the scene. We create a new, empty GameObject and name it “Scripts”. This makes it easier to find the scripts later on and we don’t rely on the camera being active all the time.

该脚本尚未出现。 我们创建一个新的空GameObject并将其命名为“脚本”。 这样一来,以后便可以更轻松地找到脚本,而且我们也不必一直依赖摄像机。

PUN正在打电话 (PUN is calling)

Like Unity, PUN will call certain methods in our code when something interesting is happening. Currently, we are interested in something like “arrived in the lobby” or “found a room” and “didn’t find a room”. The list of methods PUN will call can be found in the documentation. There is also an enum with all those method names in code. So autocompletion in MonoDevelop can give us a quick hint which methods our script might have. Type PhotonNetworkingMessage, followed by a period character (a dot), and a dropdown will list the names and a description.

像Unity一样,当有趣的事情发生时,PUN将在我们的代码中调用某些方法。 当前,我们对诸如“到达大厅”或“找到房间”以及“找不到房间”之类的东西感兴趣。 PUN将调用的方法列表可以在文档中找到。 还有一个在代码中包含所有这些方法名称的枚举。 因此,MonoDevelop中的自动完成功能可以快速提示我们脚本可能具有的方法。 键入PhotonNetworkingMessage,后跟一个句点字符(点),然后会出现一个下拉列表,列出名称和说明。

OnJoined大堂 (OnJoinedLobby)

One of the callback methods in PUN is OnJoinedLobby. It’s called when PUN gets you into the lobby. Let’s get into a room quickly: The PhotonNetwork class has a JoinRandomRoom() method. This should get us into any room. Let’s try…

PUN中的回调方法之一是OnJoinedLobby。 当PUN引导您进入大厅时,将调用此方法。 让我们快速进入一个房间:PhotonNetwork类具有JoinRandomRoom()方法。 这应该使我们进入任何房间。 我们试试吧…

Running this, we notice that JoinRandomRoom() doesn’t work – the current code stays in the Lobby?!

运行此命令,我们注意到JoinRandomRoom()不起作用–当前代码停留在Lobby ?!

处理错误 (Handling errors)

In case of errors, PUN makes use of the Log and the Console. Now is a good time to open it in the Editor (Ctrl+Shift+C). There is a message: “joinrandom failed, client stays on masterserver: OperationResponse 225: ReturnCode: 32760 (No match found).” Ok, so there is no match. This can happen. And we can fix this. A check in the PhotonNetworkingMessage enum shows there is a callback for exactly this situation: OnPhotonRandomJoinFailed. Don’t mix it up with the similar OnPhotonJoinRoomFailed.

如果发生错误,PUN将使用日志和控制台。 现在是在编辑器中打开它的好时机(Ctrl + Shift + C)。 出现一条消息:“ joinrandom失败,客户端停留在主服务器上:OperationResponse 225:ReturnCode:32760(未找到匹配项)。” 好的,所以没有匹配项。 这可能发生。 我们可以解决此问题。 对PhotonNetworkingMessage枚举的检查显示确实有这种情况的回调:OnPhotonRandomJoinFailed。 请勿将其与类似的OnPhotonJoinRoomFailed混淆。

创建一个房间 (Creating a room)

JoinRandomRoom fails if no one else is playing or if all rooms are maxed out with players. Obviously, we need to create a room. We implement OnPhotonRandomJoinFailed() and lookout for a “create room” method in PhotonNetwork. There is a perfect match: CreateRoom(). The tooltip explains that there are two overloads and if we pass null as room name a GUID will be assigned. As we don’t show room names yet, we don’t care and pass null.

如果没有其他人在玩,或者所有房间的玩家都被占用,则JoinRandomRoom失败。 显然,我们需要创建一个房间。 我们实现OnPhotonRandomJoinFailed()并在PhotonNetwork中寻找“创建房间”方法。 有一个完美的匹配:CreateRoom()。 工具提示说明有两个重载,如果我们将null作为房间名传递,则会分配一个GUID。 由于我们尚未显示房间名称,因此我们不在乎并传递null。

Try out this code by running it. The detailed state is changing more often than before and ends on “Joined”. We’re in some room!

通过运行它来尝试该代码。 详细状态的更改比以前更频繁,并且以“加入”结束。 我们在某个房间里!

马可波罗:同步职位 (Marco Polo: Syncing Positions)

Let’s give each player a monster to run around with. First, get a “Monster” character (by Dries Maesen) from the Asset Store. After import, we will do some preparation for later use: Rename the folder “character1” to “Resources”. We will instantiate this prefab by name and this means it must be in a Resources folder . Add a PhotonView component to the “monsterprefab”. They are found in the Components menu under “Miscellaneous”.

让我们给每个玩家一个怪物来跑来跑去。 首先,从Asset Store获得一个“ Monster”角色(由Dries Maesen负责)。 导入后,我们将做一些准备以备后用:将文件夹“ character1”重命名为“ Resources”。 我们将通过名称实例化此预制件,这意味着它必须位于Resources文件夹中 。 将PhotonView组件添加到“ monsterprefab”。 它们位于“其他”的“组件”菜单中。

光子视图 (PhotonView)

A PhotonView is PUN’s equivalent of a NetworkView (if you are familiar with Unity Networking). PUN needs a PhotonView per instantiated prefab to keep the networking reference (known as PhotonViewId), the owner of the object and a reference to an “observed” component. PUN keeps track of the PhotonViews it instantiates locally. At runtime regular checks of observed components will send updates to the other clients. More observed objects mean more work and network traffic. To setup the new PhotonView to observe the translation of its “monsterprefab”, drag & drop the translation to the “observed” field. We don’t need to change the other settings of the PhotonView.

PhotonView与PUN等效于NetworkView(如果您熟悉Unity Networking)。 PUN每个实例化的预制件都需要一个PhotonView,以保留网络引用(称为PhotonViewId),对象的所有者以及对“已观察”组件的引用。 PUN跟踪在本地实例化的PhotonViews。 在运行时,对观察到的组件的定期检查将向其他客户端发送更新。 观察到更多的对象意味着更多的工作和网络流量。 要设置新的PhotonView以观察其“ monsterprefab”的翻译,请将其拖放到“ observed”字段中。 我们不需要更改PhotonView的其他设置。

添加怪物 (Adding Monsters)

To instantiate a monster use PhotonNetwork.Instantiate(). Don’t mix it up with Unity’s Instantiate() or Network.Instantiate(). This makes sure the view gets instantiated on the other clients, too. In OnJoinedRoom(), we call Instantiate() like so:

要实例化怪物,请使用PhotonNetwork.Instantiate()。 请勿将其与Unity的Instantiate()或Network.Instantiate()混淆。 这也可以确保在其他客户端上实例化该视图。 在OnJoinedRoom()中,我们这样调用Instantiate():

Running this will show a monster pop up and start falling. Poor monster. Let’s create some ground.

运行此命令将显示怪物弹出并开始掉落。 可怜的怪物。 让我们创造一些基础。

接地的 (Grounded)

The next steps are common Unity “work”: Create a directional light in the scene and rotate it to point down in some angle – imitating the sun. Add a plane, scale it to 10, 1, 10 and move it to 0, 0, 0. Let’s try some substances from the Asset Store. Download the Eighteen Free Substances package and import it. The “Pavement_01” looks nice, so we open its folder and apply the material to the plane. In the Inspector, change the texture tiling from 1 to 10 for x and y.

接下来的步骤是Unity的常见“工作”:在场景中创建定向光,然后将其旋转以向下倾斜(模仿太阳)。 添加一个平面,将其缩放到10、1、10,然后将其移动到0、0、0。让我们尝试一些从Asset Store中获取的物质。 下载十八种免费物质包装并导入。 “ Pavement_01”看起来不错,因此我们打开其文件夹并将材质应用于平面。 在检查器中,将x和y的纹理拼贴从1更改为10。

为控制而战 (Fight for Control)

Checking the progress with two clients, we notice that all monsters are moved by our key input. We cloned the monster including the enabled myThirdPersonController component! There are countless ways to do this. One simple way is to disable the script in the “monsterprefab” (!) and enable it only when we instantiate a monster for “our” client. Instantiate() returns the GameObject it created, so this is no big deal. Aside from one handicap: The myThirdPersonController is a UnityScript script. To make any script of one language available to the scripts of another language, you can move it to a “Plugins” folder in your project. Create this folder and move the script over (drag & drop in the editor).After instantiating our monster, we can grab its myThirdPersonController component and activate it.

与两个客户一起检查进度时,我们注意到所有怪物都是通过我们的按键输入移动的。 我们克隆了包含启用的myThirdPersonController组件的怪物! 有无数种方法可以做到这一点。 一种简单的方法是禁用“ monsterprefab”(!)中的脚本,仅在我们为“我们的”客户端实例化怪兽时才启用它。 Instantiate()返回它创建的GameObject,所以没什么大不了的。 除了一个障碍之外:myThirdPersonController是UnityScript脚本。 要使一种语言的脚本可用于另一种语言的脚本,可以将其移动到项目中的“插件”文件夹中。 创建这个文件夹并将脚本移到上方(拖放到编辑器中)。实例化怪物之后,我们可以获取其myThirdPersonController组件并激活它。

So far, monsters of other players are moved but don’t walk. The animation is missing. Also, the position updates are not smooth. This can be fixed by a bit of code. We need another script. Create a “NetworkCharacter” C# script in the Marco Polo folder. Add it to the “monsterprefab” and make it the observed component of the PhotonView (drag & drop). While a script is observed, the PhotonView regularly calls the method OnPhotonSerializeView(). The task of this is to create the info we want to pass to others and to handle such incoming info – depending on who created the PhotonView.

到目前为止,其他玩家的怪物已移动但不行走。 缺少动画。 另外,位置更新也不顺利。 这可以通过一些代码解决。 我们需要另一个脚本。 在Marco Polo文件夹中创建一个“ NetworkCharacter” C#脚本。 将其添加到“ monsterprefab”中,使其成为PhotonView的观察组件(拖放)。 当观察到脚本时,PhotonView会定期调用方法OnPhotonSerializeView()。 此任务是创建我们想要传递给他人的信息并处理此类传入信息-取决于创建PhotonView的人。

A PhotonStream is passed to OnPhotonSerializeView() and the value of isWriting tells us if we need to write or read remote data from it, A simple way to smoothly “push” a monster to its correct position is to Lerp it there over time. We add OnPhotonSerializeView(), correctPlayerPos and correctPlayerRot to the NetworkCharacter. In OnPhotonSerializeView we store the new values and apply them (bit by bit) in Update(). It looks like this:

将PhotonStream传递到OnPhotonSerializeView(),isWriting的值告诉我们是否需要从中写入或读取远程数据,一种将怪物平稳地“推”到正确位置的简单方法是随着时间的推移将其拉到那里。 我们将OnPhotonSerializeView(),correctPlayerPos和correctPlayerRot添加到NetworkCharacter中。 在OnPhotonSerializeView中,我们存储新值,并将它们(逐位)应用到Update()中。 看起来像这样:

Monsters of other players won’t be using the exact same speed this way but they will reach the same point in a short time. For some games it might be more important to have the characters at the correct position than how exactly they get there.

其他玩家的怪物不会以这种方式使用完全相同的速度,但是他们会在短时间内达到相同的速度。 对于某些游戏,将角色放置在正确的位置比准确地到达角色更重要。

结论 (Conclusion)

By now, you should have learned to register an application with the Photon Cloud, how to create a room, and implement smoothly moving characters in a network multiplayer game– and in mere minutes! If you’d like to learn more about Photon Cloud, consider examining the tutorial pages made available at www.exitgames.com

到目前为止,您应该已经学会了如何在Photon Cloud上注册应用程序,如何创建房间以及如何在网络多人游戏中几分钟内实现平滑移动的角色! 如果您想了解有关Photon Cloud的更多信息,请考虑查看www.exitgames.com上提供的教程页面。

翻译自: https://blogs.unity3d.com/2012/03/22/introduction-to-network-multiplayer-development-with-photon-cloud/

photon多人在线

photon多人在线_Photon Cloud网络多人游戏开发简介相关推荐

  1. BigWorld Pty. Ltd.是一家全球领先的大型多人在线游戏(MMOG)开发解决方案供应商...

    BigWorld Pty. Ltd.是一家全球领先的大型多人在线游戏(MMOG)开发解决方案供应商.其开发套件为网络游戏提供了一整套解决方案,可大幅度提高游戏产品质量,并使用户大大降低游戏开发成本,从 ...

  2. 网络增强现实开发简介 Introduction to Web AR development

    搭配webXR.mindAR.three.js和tensorflow.js 你会学到: 获得构建不同类型的网络增强现实应用程序的实践经验,包括图像效果.人脸效果和世界效果 获得关于增强现实如何在网络浏 ...

  3. 笔记 - Ali Cloud网络(VPC, SLB) 简介

    VPC的概念 VPC 全称 Virtual Private Cloud 虚拟私有云. 是基于Ali Cloud构建的1个隔离带网络环境, 专有网络之间逻辑上彻底隔离. VPC 主要提供两个能力: 用户 ...

  4. 快节奏多人在线游戏网络入门系列教程(1):简介

    简介 该系列教程主要讨论快节奏多人在线游戏的网络相关的技术和算法.这是该系列教程的第一章,如果你对多人在线游戏有一定了解,可以跳过本章. 开发任何一款游戏都是一个挑战性的任务.而多人在线游戏增加了更多 ...

  5. 使用FLEX3开发大型多人在线游戏

    使用FLEX3开发大型多人在线游戏 2009-09-02 10:07 使用FLEX3开发大型多人在线游戏 收藏 使用FLEX3开发大型多人在线游戏 大型多人在线游戏(MMO)技术已经涉足到各种软件形式 ...

  6. 游戏开发中的问题-----摘自《大型多人在线游戏开发》

    原文: 虽然在大型多人在线游戏开发过程中会遇到很多问题和挑战,但最关键的一点就是有能力编写稳健的代码.这意味着我们必须进行良好的设计而不是随意地堆砌代码,这意味着我们必须在代码中加入足够的注释以方便支 ...

  7. 论文阅读|两人零和马尔可夫博弈的在线极大极小Q网络学习《Online Minimax Q Network Learning for TZMGs》

    文章获取https://doi.org/10.1109/TNNLS.2020.3041469https://doi.org/10.1109/TNNLS.2020.3041469 <Online ...

  8. 快节奏多人在线游戏网络入门系列教程(2):客户端预测与服务器协调

    简介 在上一篇文章中,我们简单介绍了权威服务器的体系.客户端发送交互信息给服务器,服务器周期性的更新游戏状态,然后返回游戏状态给客户端. 这个简单体系会导致用户发送命令时和屏幕渲染响应之间的延迟.产生 ...

  9. 一款二次元的Web多人在线网络聊天系统:Fiora安装及使用

    说明:Fiora是一款偏二次元的Web多人在线聊天应用,使用Node.js.Mongodb.Socket.io和React编写,使用起来还行,挺简洁的,这里水个搭建教程,有兴趣的可以玩玩. 截图 功能 ...

最新文章

  1. 报名照片审核处理工具_初级报名!你的照片怎么处理才能通过审核
  2. a different object with the same identifier val...
  3. python基础学习中要懂的知识点:反射机制
  4. 科技管理的作业选题 很重要
  5. Android之mediarecorder中的方法以及工作流程的过程
  6. java es 数据批量导入_ElasticSearch—Java批量导入导出
  7. 【LeetCode】3月17日打卡-Day2
  8. Android--Activity的跳转及Activity之间的数据传递
  9. 利用大数据构建用户画像的好处
  10. php mysql网站入侵_第一篇:PHP+MySQL injection攻击:浅谈网页安全
  11. 管理九段,你的管理入围“几段”了?
  12. 查询EI检索号的方法
  13. 大前端-阶段2 - 模块2 - 前端工程化实战-模块化开发(ESModule)-打包工具(webpack4)
  14. iPhone设备上安装beta版本系统,在浏览器中搜索网址 beta.apple.com
  15. seek()方法的使用
  16. bilibili直播 斗鱼直播等直播工具黑屏怎么办?
  17. MySQL报错:Data too long for column
  18. 用 Python 制作家用防盗工具
  19. mysql spj_MySQL查询优化器--非SPJ优化--LIMIT优化
  20. [网络收集]JS刷新页面总和!多种JS刷新页面代码!

热门文章

  1. Python 十六进制,十进制转换
  2. 网件公司M4100-D12G三层交换机,部分配置说明(2)
  3. 鸿蒙音波萨顶顶,假唱被揭穿5位明星,筷子兄弟丢人到国外,萨顶顶竟然拿反话筒!...
  4. linux系统创建RAID0、1、10、50
  5. BAT有增有减amp;nbsp;互联网2015校园…
  6. 谈谈幼儿时期的孤独和成年后的残忍
  7. 软件测试之搜索框功能点用例梳理
  8. 小米化!?凡客还有多少故事可讲?
  9. vbox虚拟机无法桥接网卡怎么办
  10. 如何用python制作静摩擦力模拟器 python项目小发明 【安安教具】-【物理】-【静摩擦力】模拟器