
Boss Room是一个基于Unity Netcode开发的多人协作RPG游戏。它是作为一个在网络游戏中的常见游戏模式的示例游戏开发出来的。
你可以在你自己的Unity游戏中使用这个项目中的任何素材和代码。该项目基于Unity Companion License开发。
重要提醒:
- Boss Room目前已经支持在以下平台上开发和测试 (Windows, Mac, iOS, 和Android).
- Boss Room目前支持最新的Unity LTS版本.
- 在安装Unity Editor时请确保安装了Windows/Mac单独支持。如需要专用游戏服务器的支持,还需要安装Windows/Linux Dedicated Server支持。(Linux还需要安装IL2CPP和Mono的Build支持)


Download Repository在Unity Hub中下载。下载好项目文件后,请按照一下步骤来打开和运行游戏:
2021.3.6f1或更新的Unity Editor。

为了测试多人游戏联机功能我们可以在开发机上运行多个游戏实例,可以使用ParrelSync,也可以通过网络与朋友进行联机,详见如何测试
首先我们需要编译出一个可执行程序,在菜单中找到 File/Build Settings 然后点击 Build 。

编译完成后就可以在本地运行多个游戏实例来做主机和从机。
Mac用户要运行一个程序的多个实例需要在命令行下执行
open -n BossRoom.app
要在公网上实现多人联机,首先需要将可执行文件共享给所有玩家。参考上一部分。
其次则需要有中转网络通信的手段,目前可以有两种选择:
目前Boss Room支持专用游戏服务器模式,可以支持运行一个无玩家的Headless的服务器。请按照以下步骤进行编译:

-port=的参数来指定服务器监听的端口)。Direct IP模式下连接127.0.0.1的默认端口即可进行联机。编译出的专用游戏服务器可以上传至游戏服务器托管服务(如Multiverse)。
Multiverse是一个基于Kubernetes的游戏服务器托管服务。游戏开发者可以将自己的游戏服务器打包成Docker镜像,方便地进行弹性的游戏服务器运行/销毁。
重要提醒: 目前Multiverse只支持Linux的Docker镜像。
以Boss Room游戏为例,请按照以下步骤来将你的游戏与Multiverse进行集成:
Multiverse SDK是游戏服务器用来维护在Multiverse中的状态和生命周期的。
重要提醒: 目前Multiverse只提供了Unity的SDK(其他语言的SDK会陆续放出)。
Assets/Scripts/Gameplay/Unity.BossRoom.Gameplay.asmdef: "references": [
...
...
"Unity.BossRoom.ConnectionManagement",
"VContainer",
"Unity.Cn.Multiverse"
],
在Unity Editor Project窗口,搜索DSLobbyManagementState, 给专用游戏服务器的运行入口DSLobbyManagementState(Prefab Asset)添加一个Multiverse SDK的组件。

修改DSLobbyManagementState.cs,添加MultiverseSDK的初始化代码。
using VContainer;
using Unity.Cn.Multiverse;
...
...
public class DSLobbyManagementState : GameStateBehaviour
{
[Inject]
ConnectionManager m_ConnectionManager;
public override GameState ActiveState => GameState.DedicatedServerLobbyManagement;
private MultiverseSdk m_Multiverse = null;
...
...
IEnumerator StartServerCoroutine()
{
DedicatedServerUtilities.Log($"Starting Headless Server, listening on address {address}:{port}");
m_ConnectionManager.StartServerIP(address, port); // This will switch to the char select scene once the server started callback has been called
// Start the multiverse SDK
StartMultiverse();
yield return new WaitForServerStarted(); // Less performant than just the callback, but way more readable than a callback hell.
// TODO change scene to char select here and do other init. why is it handled by connection manager right now?
SceneLoaderWrapper.Instance.AddOnSceneEventCallback();
SceneLoaderWrapper.Instance.LoadScene(SceneNames.CharSelect, useNetworkSceneManager: true);
}
...
...
private async void StartMultiverse()
{
m_Multiverse = GetComponent<MultiverseSdk>();
bool ok = await m_Multiverse.Connect();
if (ok)
{
Debug.Log(("Server - Connected"));
}
else
{
Debug.Log(("Server - Failed to connect, exiting"));
Application.Quit(1);
}
ok = await m_Multiverse.Ready();
if (ok)
{
Debug.Log($"Server - Ready");
}
else
{
Debug.Log($"Server - Ready failed");
Application.Quit();
}
}

BossRoom.x86_64。BossRoom.zip(打包时请保持BossRoom.x86_64文件在zip包根目录)。

9998。可执行文件填BossRoom.x86_64,该参数指的是游戏服务器可执行文件在zip包中的相对路径。在选择游戏包中选择刚才打包好的BossRoom.zip。上传结束后,点击创建游戏镜像。创建结束后,点击提交游戏镜像来将该镜像用于正式游戏服务器托管。







关于Multiverse的详细文档,请参见https://xxxx