Fantasy-Net 2025.2.1422

dotnet add package Fantasy-Net --version 2025.2.1422
                    
NuGet\Install-Package Fantasy-Net -Version 2025.2.1422
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Fantasy-Net" Version="2025.2.1422" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Fantasy-Net" Version="2025.2.1422" />
                    
Directory.Packages.props
<PackageReference Include="Fantasy-Net" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Fantasy-Net --version 2025.2.1422
                    
#r "nuget: Fantasy-Net, 2025.2.1422"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Fantasy-Net@2025.2.1422
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Fantasy-Net&version=2025.2.1422
                    
Install as a Cake Addin
#tool nuget:?package=Fantasy-Net&version=2025.2.1422
                    
Install as a Cake Tool

Fantasy Framework

专为大型多人在线游戏打造的高性能分布式服务器框架

License .NET C# Unity NuGet Downloads

📖 官方文档 | 🚀 快速开始 | 💬 QQ群: 569888673


⭐ Fantasy 是什么?

Fantasy 是一个零反射、高性能的 C# 游戏服务器框架,专为大型多人在线游戏打造。

核心特点:

  • 零反射架构 - 编译时代码生成,Native AOT 完美支持
  • 🚀 极致性能 - 对象池、内存优化、原生集合
  • 🌐 多协议支持 - TCP/KCP/WebSocket/HTTP 一键切换
  • 🔥 分布式架构 - Server-to-Server 通信、跨服事件系统
  • 🎮 ECS 设计 - Entity-Component-System,灵活扩展
  • 📦 完整工具链 - 脚手架工具、协议生成器、配置表导出

🚀 快速开始

方式一:使用 Fantasy CLI 脚手架(推荐)

安装 Fantasy CLI:

dotnet tool install -g Fantasy.Cli

创建项目:

fantasy init -n MyGameServer

一行命令即可创建完整的项目结构,包括:

  • ✅ 服务器项目(Main/Entity/Hotfix 三层结构)
  • ✅ Fantasy.config 配置文件
  • ✅ 网络协议定义和导出工具
  • ✅ NLog 日志配置

方式二:NuGet 包安装

1. 安装 NuGet 包:

dotnet add package Fantasy-Net

2. 创建配置文件 Fantasy.config(自动生成)

3. 编写启动代码:

using Fantasy;

// 加载程序集
AssemblyHelper.Initialize(typeof(Program).Assembly);

// 启动 Fantasy 框架
await Fantasy.Entry.Initialize(args);

4. 运行服务器:

dotnet run

📖 详细教程快速开始文档


🎯 适用场景

游戏类型 Fantasy 优势
🏰 MMORPG 分布式架构、跨服通信、实体寻址系统
⚔️ 实时对战 KCP 低延迟协议、高性能 ECS 架构
🎲 回合制/卡牌 TCP/WebSocket 可靠通信、数据持久化
🌍 开放世界 场景管理、实体层级系统
🏪 H5/小游戏 WebSocket 支持、Unity WebGL 兼容

💡 核心功能

1. 网络协议助手 - 一行代码搞定网络通信

传统框架需要 50+ 行代码处理消息序列化、发送、回调、超时...

Fantasy 只需 1 行:

// 自动生成的扩展方法,自动处理序列化、OpCode、回调
var response = await session.C2G_Login("player123", "password");

协议定义:

// Inner/G2G_CreateRoom.proto
message G2G_CreateRoom // IRequest
{
    int32 RoomType = 1;
    int32 MaxPlayers = 2;
}

message G2G_CreateRoomResponse // IResponse
{
    int32 ErrorCode = 1;
    int64 RoomId = 2;
}

自动生成 Helper 扩展方法:

// 服务器间调用,框架自动生成
var response = await session.G2G_CreateRoom(roomType: 1, maxPlayers: 4);

// 客户端调用
var loginRes = await session.C2G_Login(account, password);

消息处理器(自动注册,零配置):

public class C2G_LoginHandler : Message<Session, C2G_Login, G2C_Login>
{
    protected override async FTask Run(Session session, C2G_Login request, G2C_Login response)
    {
        // 框架自动路由到这里,零反射开销
        response.Token = await AuthService.Login(request.Account);
    }
}

2. Roaming 路由系统 - 自动跨服务器转发

客户端只需连接 Gate 服务器,消息自动转发到目标服务器(Map/Battle/Chat...)

协议定义:

message C2M_EnterMap // IRoamingRequest
{
    int32 MapId = 1;
}

message M2C_EnterMap // IRoamingResponse
{
    int32 ErrorCode = 1;
    int64 SceneId = 2;
}

客户端:

// Gate 服务器自动转发到 Map 服务器
var response = await session.C2M_EnterMap(mapId: 1001);

服务端(运行在 Map 服务器):

public class C2M_EnterMapHandler : Roaming<Session, C2M_EnterMap, M2C_EnterMap>
{
    protected override async FTask Run(Session session, C2M_EnterMap request, M2C_EnterMap response)
    {
        // 这里运行在 Map 服务器,Gate 已自动转发
        var scene = await CreateMapScene(request.MapId);
        response.SceneId = scene.Id;
    }
}

核心价值:

  • ✅ 客户端无需知道目标服务器地址
  • ✅ 零配置自动转发
  • ✅ 开发体验与单服务器一致

3. 零反射 + Native AOT 支持

传统框架:

// ❌ 运行时反射扫描(慢 + 不支持 AOT)
Assembly.GetTypes().Where(t => typeof(IMessageHandler).IsAssignableFrom(t))...

Fantasy:

// ✅ 编译时自动生成注册代码(快 + AOT 友好)
// 无需任何手动注册,源生成器自动完成
public class C2G_LoginHandler : Message<Session, C2G_Login, G2C_Login>
{
    protected override async FTask Run(Session session, C2G_Login request, G2C_Login response)
    {
        // 框架自动路由,零反射开销
        response.Token = await AuthService.Login(request.Account);
    }
}

4. 跨服域事件系统 (SphereEvent)

轻松实现跨服公告、跨服排行榜、跨服 PVP:

// 服务器 A 发布事件
await sphereEvent.PublishToRemoteSubscribers(new WorldBossDefeatedEvent
{
    BossId = 1001,
    KillerGuildId = 5201314
});

// 服务器 B/C/D... 自动接收
[SphereEvent]
public class WorldBossEventHandler : SphereEvent<WorldBossDefeatedEvent>
{
    protected override async FTask Run(Scene scene, WorldBossDefeatedEvent args)
    {
        // 所有订阅的服务器都会收到此事件
        await SendGlobalAnnouncement($"世界Boss已被击败!");
    }
}

5. ECS 架构 - 灵活的实体组件系统

// 定义实体
public class Player : Entity
{
    public string Name { get; set; }
    public int Level { get; set; }
}

// 添加组件(组合式设计)
player.AddComponent<BagComponent>();
player.AddComponent<EquipmentComponent>();
player.AddComponent<SkillComponent>();

// 系统自动执行(源生成器自动注册)
public class PlayerAwakeSystem : AwakeSystem<Player>
{
    protected override void Awake(Player self)
    {
        Log.Info($"玩家 {self.Name} 上线了!");
    }
}

6. 多协议支持

同一套代码,切换配置即可支持所有协议:

// TCP - 稳定可靠
var session = await NetworkHelper.Connect("127.0.0.1:20000", NetworkProtocolType.TCP);

// KCP - 低延迟(实时对战首选)
var session = await NetworkHelper.Connect("127.0.0.1:20000", NetworkProtocolType.KCP);

// WebSocket - H5/WebGL
var session = await NetworkHelper.Connect("ws://127.0.0.1:20000", NetworkProtocolType.WebSocket);

// HTTP - RESTful API
var response = await httpClient.Get("/api/users/123");

📦 完整工具链

Fantasy CLI 脚手架

一键生成项目结构:

# 安装
dotnet tool install -g Fantasy.Cli

# 创建项目
fantasy init -n MyGame

# 添加组件
fantasy add -t networkprotocol  # 协议定义
fantasy add -t nlog              # 日志组件

协议导出工具

.proto 文件生成 C# 代码 + 自动生成 Session 扩展方法:

# 定义协议
message C2G_Login // IRequest
{
    string Account = 1;
    string Password = 2;
}

# 自动生成
public static class NetworkProtocolHelper
{
    public static async FTask<G2C_Login> C2G_Login(
        this Session session, string account, string password)
    {
        // ... 自动生成的完整实现
    }
}

配置表导出

Excel → JSON/Binary,自动生成加载代码。


🔧 环境要求

组件 版本 说明
.NET SDK 8.0+ 下载地址
Unity 2022.3.62+ 客户端开发(可选)
IDE VS 2022 / Rider / VS Code 推荐 Rider 或 VS 2022
MongoDB 4.0+ 数据库(可选)

🖥️ 平台支持

平台 支持 说明
🖥️ Windows Server 游戏服务器首选
🐧 Linux Server Docker/K8s 部署
🍎 macOS 开发调试
🎮 Unity (全平台) Win/Mac/iOS/Android
🌐 Unity WebGL H5 小游戏

📚 文档与教程


💬 社区与支持


🎯 为什么选择 Fantasy?

对比项 Fantasy 传统框架
网络消息 1 行代码 50+ 行代码
性能 零反射 + AOT 大量反射
分布式 内置 Roaming/SphereEvent 需要自己实现
协议切换 配置文件一键切换 需要重写代码
学习曲线 脚手架 + 文档 + 视频 文档不全
生产就绪 ✅ 完整工具链 ⚠️ 需要自己搭建

🤝 优质开源项目推荐


📄 开源协议

本项目采用 MIT License 开源协议。


🙏 贡献者

感谢所有为 Fantasy 做出贡献的开发者!

Contributors


Built with ❤️ by Fantasy Team | Made for Game Developers

🎉 如果 Fantasy 对你有帮助,请给个 Star ⭐

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Fantasy-Net:

Package Downloads
Fantasy-Net.NLog

Fantasy is a high-performance network development framework based on .NET, supporting mainstream protocols. It is designed for development teams or individuals needing a quick start, scalability, and a distributed, cross-platform solution at the commercial level. Fantasy aims to provide easy-to-use tools while ensuring high system performance and scalability.

Fantasy-Net.ConfigTable

Fantasy is a high-performance network development framework based on .NET, supporting mainstream protocols. It is designed for development teams or individuals needing a quick start, scalability, and a distributed, cross-platform solution at the commercial level. Fantasy aims to provide easy-to-use tools while ensuring high system performance and scalability.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2025.2.1422 78 1/14/2026
2025.2.1421 90 1/11/2026
2025.2.1420 95 1/8/2026
2025.2.1419 157 12/26/2025
2025.2.1418 188 12/25/2025
2025.2.1416 180 12/23/2025
2025.2.1415 172 12/23/2025
2025.2.1414 286 12/17/2025
2025.2.1413 273 12/16/2025
2025.2.1412 268 12/16/2025
2025.2.1410 130 12/12/2025
2025.2.1408 209 12/5/2025
2025.2.1407 684 12/2/2025
2025.2.1406 491 12/1/2025
2025.2.1405 412 12/1/2025
2025.2.1402 204 11/24/2025
2025.2.1401 236 11/22/2025
2025.2.131 312 11/17/2025
2025.2.14 403 11/20/2025
2025.2.13 293 11/14/2025
2025.2.12 292 11/11/2025
2025.2.11 299 11/11/2025
2025.2.0 160 11/8/2025
2024.2.26 294 6/23/2025
2024.2.25 328 4/14/2025
2024.2.24 238 2/21/2025
2024.2.23 170 2/18/2025
2024.2.22 714 2/6/2025
2024.2.21 175 1/18/2025
2024.2.20 207 1/2/2025
2024.2.10 193 12/22/2024
2024.2.0 207 12/14/2024
2024.1.26 184 12/6/2024
2024.1.25 174 12/5/2024
2024.1.24 168 12/5/2024
2024.1.23 179 12/3/2024
2024.1.22 188 12/3/2024
2024.1.21 179 12/1/2024
2024.1.20 174 11/19/2024
2024.1.19 186 11/12/2024
2024.1.18 171 11/10/2024
2024.1.17 226 11/7/2024
2024.1.16 161 11/7/2024
2024.1.15 176 11/4/2024
2024.1.14 276 10/29/2024
2024.1.13 159 10/26/2024
2024.1.12 167 10/24/2024
2024.1.11 177 10/15/2024
2024.1.10 175 10/11/2024
2024.1.9 182 10/9/2024
2024.1.8 187 10/9/2024
2024.1.7 212 9/30/2024
2024.1.6 196 9/29/2024
2024.1.5 185 9/24/2024
2024.1.4 181 9/24/2024
2024.1.3 737 9/23/2024
2024.1.2 190 9/23/2024
2024.1.1 199 9/22/2024
2024.1.0 188 9/21/2024 2024.1.0 is deprecated because it is no longer maintained and has critical bugs.
2.0.0 111 11/8/2025