跳到主要内容

MCE Online 设置

MCE Online adds a complete MMORPG layer to the Monster Capture Engine using Nakama as the backend. This guide walks you through setting up the server, configuring the client, and testing connectivity.

在 Online 版本及以上可用。

什么是 Nakama?

Nakama is an open-source game server that provides:

  • User authentication (email, device, SSO).
  • Real-time multiplayer (WebSockets).
  • Server-authoritative game logic (RPCs).
  • Matchmaking with ELO-based ratings.
  • Leaderboards, achievements, and live events.
  • Cloud save and storage.
  • Chat, friends, guilds.

MCE Online wraps Nakama with 18 specialized services for the monster capture genre.

前提条件

  • MCE Core installed and working.
  • Docker Desktop (for local development) or a hosted Nakama instance.
  • MCE Online addon imported into your project.

步骤 1:启动 Nakama 服务器

本地开发(Docker)

MCE includes a docker-compose.yml for local Nakama:

# From your project root or the MCE_Online server directory
docker-compose up -d

This starts:

  • Nakama server on port 7350 (gRPC) and 7351 (HTTP/console).
  • CockroachDB as the database on port 26257.

Access the Nakama admin console at: http://localhost:7351

Default admin credentials:

  • Username: admin
  • Password: password
Production

The default Docker configuration is for development only. For production, use a hosted Nakama instance (Heroic Cloud or self-hosted with proper security configuration).

托管(Heroic Cloud)

  1. Create an account at heroiclabs.com.
  2. Create a new Nakama project.
  3. Note the server URL and server key from the dashboard.

步骤 2:配置 Unity 客户端

OnlineConfig 资源

MCE Online uses an OnlineConfig ScriptableObject for connection settings:

  1. Navigate to Assets/MCE_Online/Resources/OnlineConfig.asset.
  2. If it does not exist, create it: Assets > Create > MCE Online > Online Config.

Configure the following fields:

FieldDescriptionLocal Default
Server HostNakama server hostnamelocalhost
Server PortHTTP port7350
Server KeyAPI key (from Nakama console)defaultkey
Use SSLEnable HTTPS/WSSfalse (local), true (production)

The OnlineConfig also contains settings for:

  • Auto-Connect: Whether to connect automatically on game start.
  • Reconnection: Exponential backoff settings for dropped connections.
  • Timeout: Request timeout duration.
  • Feature Toggles: Enable/disable specific online features.

启用 MCE_ONLINE 定义

MCE Online requires the MCE_ONLINE scripting define symbol:

  1. Go to Edit > Project Settings > Player > Other Settings > Scripting Define Symbols.
  2. Add MCE_ONLINE to the list.
  3. Click Apply.

This enables all online-related code paths in the engine.

步骤 3:部署服务器端代码

MCE Online includes TypeScript server modules for Nakama:

MCE_Online/
Server/
src/
main.ts # Entry point, RPC registration
battle.ts # Server-authoritative battle RPCs
gtl.ts # Global Trade Link RPCs
housing.ts # Housing system RPCs
tsconfig.json
package.json

部署到 Nakama

  1. Build the server modules:
cd MCE_Online/Server
npm install
npm run build
  1. Copy the built JS output to your Nakama data/modules/ directory.
  2. Restart Nakama to load the modules.

For Docker, the docker-compose.yml typically mounts the modules directory:

volumes:
- ./MCE_Online/Server/dist:/nakama/data/modules

步骤 4:NakamaManager

The NakamaManager is the singleton that manages the connection:

  • Created automatically at runtime via [RuntimeInitializeOnLoadMethod].
  • Manages 18 services (Auth, Battle, Economy, Social, etc.).
  • Handles reconnection with exponential backoff.
  • Routes all Nakama communication through a main thread queue (thread-safe).

You do not need to create or configure the NakamaManager manually. It reads from OnlineConfig and initializes itself.

连接状态

Disconnected → Connecting → Connected
↓ ↓
Offline Disconnected
↑ ↑
(reconnect) (connection lost)
StateDescription
DisconnectedNo connection, not attempting
ConnectingAttempting to connect or reconnect
ConnectedActive connection, all services available
OfflineFailed to connect, operating in offline mode

步骤 5:测试连接

快速测试

  1. Start Nakama (Docker or hosted).
  2. Open your Unity project.
  3. Enter Play Mode.
  4. Check the Console for connection messages:
[NakamaManager] Connecting to localhost:7350...
[NakamaManager] Connected successfully. Session ID: xxxx

连接测试脚本

using UnityEngine;

public class OnlineConnectionTest : MonoBehaviour
{
private void Start()
{
// NakamaManager auto-creates itself
// Check connection state after a short delay
Invoke(nameof(CheckConnection), 3f);
}

private void CheckConnection()
{
var nakama = NakamaManager.Instance;
Debug.Log($"Connection State: {nakama.ConnectionState}");
Debug.Log($"Session Valid: {nakama.IsSessionValid}");
}
}

管理控制台

While connected, verify on the Nakama admin console (http://localhost:7351):

  1. Go to Users -- you should see the auto-created device account.
  2. Go to Runtime Modules -- you should see the loaded MCE server modules.
  3. Go to API Explorer -- test RPCs directly.

在线服务概述

MCE Online provides 18 services:

ServiceDescription
AuthServiceEmail, Device, SSO authentication
SessionManagerToken persistence and refresh
WorldSyncServiceReal-time player movement, AOI filtering
PlayerNetworkEntityNetworked player representation
BattleServicesServer-authoritative PvP, matchmaking
NetworkedBattleManagerOnline battle orchestration
EconomyServicesGlobal Trade Link, wallet, shop
SocialServicesChat, guilds, friends, trading
HousingServicePlayer housing CRUD
PersistenceAndSystemsCloud saves, backups
AntiCheatManagerSpeed validation, inventory auditing
EventServiceLive events
LeaderboardServiceRankings and scores
AchievementServiceAchievement tracking
ModLoaderServer-side mod management
OnlineCommandBridgeBridges online actions to CommandGraph
OnlineSceneBootstrapperOnline scene lifecycle

Each service is documented in its own guide in this section.

故障排除

IssueSolution
"Connection refused"Ensure Nakama is running (docker ps to check)
"Invalid server key"Check OnlineConfig.ServerKey matches Nakama's config
MCE_ONLINE errorsEnsure the scripting define symbol is set
Timeout on connectCheck firewall settings, verify server host/port
"Module not found" RPC errorsDeploy server-side TypeScript modules and restart Nakama
SSL errors in productionEnsure UseSSL = true and valid certificates

下一步