前言

ET框架是一个基于C#的双端(客户端+服务端)游戏开发框架,由熊猫大佬开发维护。它的最大特点是客户端和服务端共用一套代码,大大提高了开发效率。本文将介绍ET框架的核心概念和简单入门。

ET框架特点

1. 双端共用代码

  • 客户端使用Unity引擎
  • 服务端使用.NET Core
  • 网络协议、实体组件等代码可以共用

2. ECS架构

ET采用Entity-Component-System架构:

  • Entity(实体):游戏中的对象,如玩家、怪物
  • Component(组件):实体的数据和行为
  • System(系统):处理组件的逻辑

3. Actor模型

服务端采用Actor模型进行消息处理,每个Actor独立处理自己的消息队列,避免多线程竞争。

项目结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ET/
├── Unity/ # Unity客户端项目
│ ├── Assets/
│ │ ├── Scripts/
│ │ │ ├── Core/ # 核心框架代码
│ │ │ ├── Model/ # 数据模型
│ │ │ ├── Hotfix/ # 热更新代码
│ │ │ └── ThirdParty/ # 第三方库
│ │ └── Bundles/ # 资源包
├── Server/ # 服务端项目
│ ├── Model/ # 服务端数据模型
│ ├── Hotfix/ # 服务端热更新
│ └── App/ # 启动入口
└── Share/ # 共享代码
├── Proto/ # 协议定义
└── Config/ # 配置文件

核心概念

Entity 实体

1
2
3
4
5
6
7
8
9
10
11
// 定义一个玩家实体
public class Player : Entity
{
public string Name { get; set; }
public int Level { get; set; }
}

// 创建实体
Player player = ComponentFactory.Create<Player>();
player.Name = "TestPlayer";
player.Level = 1;

Component 组件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 定义移动组件
public class MoveComponent : Component
{
public Vector3 Position { get; set; }
public float Speed { get; set; }

public void MoveTo(Vector3 target)
{
// 移动逻辑
}
}

// 给实体添加组件
player.AddComponent<MoveComponent>();

消息处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 定义消息
[Message(OuterOpcode.C2G_Login)]
public partial class C2G_LoginRequest : IRequest
{
public string Account { get; set; }
public string Password { get; set; }
}

// 消息处理器
[MessageHandler]
public class C2G_LoginHandler : AMRpcHandler<C2G_LoginRequest, G2C_LoginResponse>
{
protected override async ETTask Run(Session session, C2G_LoginRequest request,
G2C_LoginResponse response, Action reply)
{
// 处理登录逻辑
response.PlayerId = 10001;
reply();
}
}

快速入门

1. 环境准备

1
2
3
4
5
# 安装 .NET Core SDK 3.1+
# 安装 Unity 2019.4 LTS

# 克隆项目
git clone https://github.com/egametang/ET.git

2. 启动服务端

1
2
cd ET/Server/App
dotnet run

3. 启动客户端

  1. 用Unity打开 ET/Unity 项目
  2. 打开场景 Assets/Scenes/Init.unity
  3. 点击Play运行

4. 创建第一个功能

以创建一个简单的聊天功能为例:

定义协议 (Share/Proto/OuterMessage.proto)

1
2
3
4
5
6
7
8
9
10
11
message C2G_Chat // IRequest
{
string Content = 1;
}

message G2C_Chat // IResponse
{
int32 RpcId = 90;
int32 Error = 91;
string Message = 92;
}

服务端处理器

1
2
3
4
5
6
7
8
9
10
11
[MessageHandler]
public class C2G_ChatHandler : AMRpcHandler<C2G_Chat, G2C_Chat>
{
protected override async ETTask Run(Session session, C2G_Chat request,
G2C_Chat response, Action reply)
{
Log.Info($"收到聊天消息: {request.Content}");
response.Message = $"服务器收到: {request.Content}";
reply();
}
}

客户端调用

1
2
3
4
5
6
public static async ETTask SendChat(string content)
{
G2C_Chat response = await SessionComponent.Instance.Session
.Call(new C2G_Chat { Content = content }) as G2C_Chat;
Log.Info(response.Message);
}

服务端架构

ET服务端采用分布式架构,主要包含以下进程:

进程 说明
Gate 网关服务器,处理客户端连接
Map 地图服务器,处理游戏逻辑
Location 位置服务器,管理Actor位置
DB 数据库代理服务器
Realm 登录服务器

热更新机制

ET支持代码热更新:

  • Model层:不可热更新的基础代码
  • Hotfix层:可热更新的业务逻辑
1
2
// Hotfix层代码可以在运行时重新加载
// 修改Hotfix代码后,服务端可以不停服更新

总结

ET框架的优势:

  1. 开发效率高:双端共用代码
  2. 架构清晰:ECS + Actor模型
  3. 支持热更新:业务逻辑可热更
  4. 社区活跃:持续更新维护

适用场景:

  • 中小型网络游戏
  • 需要快速迭代的项目
  • 熟悉C#的团队

参考资料