简体   繁体   English

Unity Mirror - 如何进行个人场景旅行?

[英]Unity Mirror - How To Perform Individual Scene Traveling?

I'm a little stuck on what to do here.我有点坚持在这里做什么。 I haven't gotten any help from the discord server so I'm turning here in hopes someone has some insight for me.我没有从 discord 服务器获得任何帮助,所以我转向这里希望有人对我有一些见解。

Background:背景:

Here is what I'm trying to accomplish.这是我想要完成的。 I want to have a server+client host (using Unity Mirror) but allow all clients to travel back and forth between scenes separately.我想要一个server+client主机(使用 Unity Mirror),但允许所有客户端分别在场景之间来回移动。 The concept seems simple enough but I haven't found a good solution yet.这个概念似乎很简单,但我还没有找到一个好的解决方案。 Please note that I have server reconciliation setup for player movement.请注意,我为玩家移动设置了服务器协调设置。

What I Have Tried:我试过的:

Every time a client wants to travel to a new scene he sends a request to the server.每次客户端想要前往一个新场景时,他都会向服务器发送一个请求。 The server will additively load the scene and send an RPC back to the client who will also additively load the scene.服务器将附加加载场景并将 RPC 发送回客户端,客户端也将附加加载场景。 Then the scene has been successfully loaded, either by the client or the server it will call the MoveObjects command (on the SceneManager) to move the player character to the new scene.然后场景已经成功加载,无论是客户端还是服务器都会调用 MoveObjects 命令(在 SceneManager 上)将玩家角色移动到新场景。 So that means that reconcilation should* still be okay.所以这意味着和解应该*仍然可以。 If you're not the server then unload the old scene.如果您不是服务器,则卸载旧场景。 If you're the server keep all scenes additively loaded when there are still clients in that scene.如果您是服务器,则在该场景中仍有客户端时保持所有场景的附加加载。

The only issue with my above approach is that I can't disable/hide scenes on the server (or at least I don't know how to do that yet).我上述方法的唯一问题是我无法在服务器上禁用/隐藏场景(或者至少我还不知道该怎么做)。

My Request我的请求

Is this the best approach?这是最好的方法吗? Are there free code examples I could look at?有我可以查看的免费代码示例吗? Tutorials that might explain a better approach?可能解释更好方法的教程? Has anyone else done this with a better method?有没有其他人用更好的方法做到这一点? I would really appreciate any help I could get on this.我非常感谢我能得到的任何帮助。

Final Thoughts最后的想法

The last thing I thought about what dynamically starting a new server and connecting to the new server for every new unity scene to separate players that way.我想到的最后一件事是动态启动新服务器并为每个新的统一场景连接到新服务器,从而以这种方式分隔玩家。 The only problem with this approach is it breaks cross scene communication.这种方法的唯一问题是它破坏了跨场景通信。 Like if I wanted to display the scene the player was in and their current health to all players in a party across multiple scenes.就像我想在多个场景中向派对中的所有玩家展示玩家所在的场景以及他们当前的健康状况一样。

This is somewhat complicated.这有点复杂。 However, I was able to achieve it like the following:但是,我能够像下面这样实现它:

  1. Make a travel function, this function will have the clients simply send a request to the server to have the server load the scene itself and then tell the clients to do the same.进行一次旅行 function,这个 function 将让客户端简单地向服务器发送请求,让服务器自己加载场景,然后告诉客户端也这样做。 In the travel function it will just have a simple call like this:在旅行 function 中,它只会有一个像这样的简单调用:
NetworkClient.Send(new ClientRequestLoadScene() { sceneName = sceneName, travelPoint = travelPoint }); 
  1. On the server register a custom handler that will expect this message type.在服务器上注册一个需要此消息类型的自定义处理程序。 Load the scene yourself if it isn't already loaded, then tell the client to load a scene using the built in handler that is registered with clients out of the box like so:如果尚未加载场景,请自行加载场景,然后告诉客户端使用开箱即用的客户端注册的内置处理程序加载场景,如下所示:
conn.Send(new SceneMessage() { sceneName = msg.sceneName, sceneOperation = SceneOperation.LoadAdditive });
  1. When the server has finished loading that scene move that clients player to the new scene:当服务器完成加载该场景时,将该客户端播放器移动到新场景:
SceneManager.MoveGameObjectToScene(character, SceneManager.GetSceneByName(GetCleanedSceneName(msg.sceneName)));
  1. Include the NetworkSceneChecker to have visibility only when you're in that particular scene包括NetworkSceneChecker以仅在您处于该特定场景时才可见
  2. (Optional) Include the PhysicsSimulator component if you have the scene loaded with 3d physics. (可选)如果您的场景加载了 3d 物理,则包括PhysicsSimulator组件。

Now this doesn't give every tiny little detail on how to do this but this is a good starting point to understand how you might be able to accomplish this.现在这并没有给出关于如何做到这一点的每一个微小的细节,但这是一个很好的起点,可以帮助您了解如何实现这一点。 The big take aways are the following:主要的收获如下:

  • The clients will not load the scenes themselves unless they explicity receive a request to do so from the server除非客户端明确收到来自服务器的请求,否则客户端不会自行加载场景
  • clients need to tell the server what scene they need to load客户端需要告诉服务器他们需要加载什么场景
  • clients need to tell the server when they're done loading客户端需要在加载完成时告诉服务器
  • the server needs to load all requested scenes服务器需要加载所有请求的场景
  • the server is responsible for moving the player to the new scene服务器负责将玩家移动到新场景

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

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