您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
Zhanxin Yang 60ec75a9 Change the profile memory limit in README.md 1年前
.git Initial commit 1年前
.github Initial commit 1年前
.metadata Initial commit 1年前
.yamato Initial commit 1年前
Assets Add multiverse integration guide in README 1年前
Documentation Change the profile memory limit in README.md 1年前
Packages Initial commit 1年前
ProjectSettings Add multiverse integration guide in README 1年前
RepoUtilities Initial commit 1年前
dotnet-tools Initial commit 1年前
.editorconfig Initial commit 1年前
.gitattributes Initial commit 1年前
.gitignore Initial commit 1年前
ARCHITECTURE.md Initial commit 1年前
CHANGELOG.md Initial commit 1年前
CODE_OF_CONDUCT.md Initial commit 1年前
CONTRIBUTING.md Initial commit 1年前
InitCodeMarker Initial commit 1年前
LICENSE.md Initial commit 1年前
README.md Change the profile memory limit in README.md 1年前
Third Party Notices.md Initial commit 1年前
Unity.Multiplayer.Samples.BossRoom.Client.csproj.DotSettings Initial commit 1年前
third-party contributors.md Initial commit 1年前

README.md

Banner

Boss Room - 基于Unity Netcode for GameObjects开发的多人RPG游戏

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支持)

获取项目文件

第一次打开项目文件

下载好项目文件后,请按照一下步骤来打开和运行游戏:

  • 确保安装了推荐版本2021.3.6f1或更新的Unity Editor。
    • 确保安装了Windows/Mac单独支持。
    • 确保安装了Windows/Linux Dedicated Server支持。
    • 确保安装了Linux IL2CPP和Mono的Build支持。
  • 第一次打开项目时Unity会导入所有的资源文件,这可能会花费较长的时间。
  • 当Editor导入结束后,在 Project 窗口中打开 Project/Startup 场景
  • 在此场景中可以点击 Play 来运行游戏。

测试多人游戏

为了测试多人游戏联机功能我们可以在开发机上运行多个游戏实例,可以使用ParrelSync,也可以通过网络与朋友进行联机,详见如何测试


本地多人游戏配置

首先我们需要编译出一个可执行程序,在菜单中找到 File/Build Settings 然后点击 Build

编译完成后就可以在本地运行多个游戏实例来做主机和从机。

Mac用户要运行一个程序的多个实例需要在命令行下执行 open -n BossRoom.app


公网上的多人游戏配置

要在公网上实现多人联机,首先需要将可执行文件共享给所有玩家。参考上一部分。

其次则需要有中转网络通信的手段,目前可以有两种选择:

  • 使用一个Relay服务器来进行中转,目前Boss Room提供了Unity Relay的支持。
  • 将Boss Room编译成Headless专用游戏服务器,使用Multiverse来托管一个专用的游戏服务器进行中转。

专用游戏服务器

目前Boss Room支持专用游戏服务器模式,可以支持运行一个无玩家的Headless的服务器。请按照以下步骤进行编译:

  1. 在菜单中找到 File/Build Settings 并将编译目标切换成 Dedicated Server,目标平台选择 Windows 或者 Linux
  2. 切换好编译目标后(第一次可能会花较长时间),点击 Build
  3. 编译完成后,执行编译出的可执行文件(命令行支持-port=的参数来指定服务器监听的端口)。
  4. 打开游戏可执行文件,在Direct IP模式下连接127.0.0.1的默认端口即可进行联机。

编译出的专用游戏服务器可以上传至游戏服务器托管服务(如Multiverse)。

与Multiverse进行集成

Multiverse是一个基于Kubernetes的游戏服务器托管服务。游戏开发者可以将自己的游戏服务器打包成Docker镜像,方便地进行弹性的游戏服务器运行/销毁。

重要提醒: 目前Multiverse只支持Linux的Docker镜像。

以Boss Room游戏为例,请按照以下步骤来将你的游戏与Multiverse进行集成:

集成Multiverse SDK

Multiverse SDK是游戏服务器用来维护在Multiverse中的状态和生命周期的。

重要提醒: 目前Multiverse只提供了Unity的SDK(其他语言的SDK会陆续放出)。

  1. 将Unity SDK的代码添加到Unity Project中。
  2. 给专用游戏服务器的运行入口添加Multiverse SDK的依赖。修改Assets/Scripts/Gameplay/Unity.BossRoom.Gameplay.asmdef:
    "references": [
        ...
        ...
        "Unity.BossRoom.ConnectionManagement",
        "VContainer",
        "Unity.Cn.Multiverse"
    ],
  1. 在Unity Editor Project窗口,搜索DSLobbyManagementState, 给专用游戏服务器的运行入口DSLobbyManagementState(Prefab Asset)添加一个Multiverse SDK的组件。

  2. 修改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();
            }
        }

编译游戏服务器

  1. 在菜单中找到 File/Build Settings 并将编译目标切换成 Dedicated Server,目标平台选择 Linux
  2. 切换好编译目标后(第一次可能会花较长时间),点击 Build ,并保存为BossRoom.x86_64
  3. 将编译出的目录打包成zip文件:BossRoom.zip(打包时请保持BossRoom.x86_64文件在zip包根目录)。

创建Multiverse Game

  1. Multiverse快速向导中开始Multiverse Game的创建。注意:由于docker镜像名字的限制,游戏名称暂时不可以包含大写字母。
  2. 创建游戏档案。对于BossRoom的游戏服务器,推荐至少512mb的内存限制。
  3. 创建游戏镜像。注意:容器端口填游戏服务器的默认端口9998。可执行文件填BossRoom.x86_64,该参数指的是游戏服务器可执行文件在zip包中的相对路径。在选择游戏包中选择刚才打包好的BossRoom.zip。上传结束后,点击创建游戏镜像。创建结束后,点击提交游戏镜像来将该镜像用于正式游戏服务器托管。
  4. 创建游戏地域。
  5. 创建并启用游戏档案可用区。

在Multiverse中创建游戏服务器

  1. 在Multiverse游戏服务器面板,点击 分配游戏服务器 ,创建一个指定游戏档案的游戏服务器。
  2. 刷新游戏服务器列表,获取刚刚建立的游戏服务器地址。
  3. 打开Boss Room游戏客户端,在 Join With IP 面板,输入刚刚获得的地址。(可打开两个客户端一起游玩)。
  4. 双方都加入后,即可联网进行游戏。

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