前言
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/ │ ├── 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
|
git clone https://github.com/egametang/ET.git
|
2. 启动服务端
1 2
| cd ET/Server/App dotnet run
|
3. 启动客户端
- 用Unity打开
ET/Unity 项目
- 打开场景
Assets/Scenes/Init.unity
- 点击Play运行
4. 创建第一个功能
以创建一个简单的聊天功能为例:
定义协议 (Share/Proto/OuterMessage.proto)
1 2 3 4 5 6 7 8 9 10 11
| message C2G_Chat { string Content = 1; }
message G2C_Chat { 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层:可热更新的业务逻辑
总结
ET框架的优势:
- 开发效率高:双端共用代码
- 架构清晰:ECS + Actor模型
- 支持热更新:业务逻辑可热更
- 社区活跃:持续更新维护
适用场景:
- 中小型网络游戏
- 需要快速迭代的项目
- 熟悉C#的团队
参考资料
评论和共享