一个优秀的ET7.2的框架学习笔记,整个项目采用ET7.2+YooAssets+luban+Fairgui。
整个框架的场景节点如下结构:
一、 Game单例管理器
Game是一个静态类,所有的单例对象都是存储在Game类中,通过AddSingleton添加用到的单例类。在AddSingleton中有singletonTypes和singletons存储了实际的单例对象,并且会调用每个单例类的Register(),Register函数中会存储具体每个单例对象引用instance。Game:Update()中调用管理的单例的Update()函数,从而驱动所有的单例类的更新。客户端用到的单例类有如下几个:
1. TimeInfo,主要和时间相关的操作,可以获取客户端、服务器的时间。
2. Logger,主要是日志处理类,默认用UnityLogger。
3. ObjectPool,对象池。
4. IdGenerater,ID生成器,尽量控制ID的唯一性。
5. EventSystem,事件处理管理器。
6. TimerComponent,定时器相关管理器。
7. CoroutineLockComponent,协程锁管理器。
8. MonoResComponent,资源加载器,主要是封装了YooAssets。
9. CodeLoader,代码加载器。
10.NetServices, 网络管理器。
11. ConfigComponent,主要是配置的加载,目前配置使用了luban。
12. MainThreadSynchronizationContext,线程同步管理器
13. Root 根节点
二、Root管理器
- Root有一个管理根部的Scene,整个客户端看做是一个Scene,Scene也是一个Entity即可以添加更多的Component。管理根部的Scene的Zone=0,类型即SceneType=SceneType.Process, Name=“Process”,Domain=this即自己,Parent=null(因为是根节点)。
- 通过Root.Instance.Scene传递事件
事件的发送 await EventSystem.Instance.PublishAsync(Root.Instance.Scene, new EventType.EntryEvent1());
事件的监听者对应逻辑处理:
三、根Scene 场景管理器
Root.Instance.Scene做为场景管理的根节点,为0号场景,所有的其他的节点都受它管理和控制,Scene也是一个Entity,可以添加很多Component:
EntryEvent1_InitShare
1. NetThreadComponent ,主要是网络线程,处理消息接发。
2. OpcodeTypeComponent,主要是消息Id
3. MessageDispatcherComponent,消息分发组件
4. NumericWatcherComponent,监视数值变化组件,分发监听
5. AIDispatcherComponent,客户端的AI分发
6. ClientSceneManagerComponent,客户端场景管理,具体玩法场景即地图之类的
GetAllConfigBytes: AInvokeHandler
8. ResComponent ,热更流程
EntryEvent3_InitClient
9. GlobalComponent,Fargui全局数据维护
10. FsmDispatcherComponent,状态机
以上15中组件的Domain都是Scene。
Scene另外一个做为事件系统的桥梁,可以在不同的模块之前数据传输从而解耦。
Scene的Domain为自己即this。
Scene的ID是0即Zone=0。
四、1号Scene
在 EntryEvent3_InitClient: AEvent中创建1号场景
1号Scene的类型Name是Game,类型是SceneType.Client,父类是ClientSceneManagerComponent,Zone是1。
Zone为1的Scene有以下Component:
CreateClientScene
1. CurrentScenesComponent
2. ObjectWait
3. PlayerComponent
AfterCreateClientScene_AddComponent添加Fairgui相关的管理
4. FUIEventComponent,Fairgui的事件
5. FUIComponent,Fairgui封装,即ui的加载和管理
InitResourceAsync
6. FsmComponent,检查资源释放有更新的状态机。
LoginHelper.Login,在登录的时候
7. RouterAddressComponent,获取路由跟realmDispatcher地址
8. NetClientComponent,网络
9. SessionComponent, 实际的一个与服务器的链接
LoadPackagesAsync加载的Fairgui的界面。
FUIComponent.ShowPanelAsync打开某个界面。
五 、GlobalComponent
GlobalComponent为Fairgui全局数据管理器,在GlobalComponentAwakeSystem中初始化各个根节点:
FUIRootHelper.GetTargetRoot(UIPanelType type根据窗口类型返回上面四个类型的根节点
public enum UIPanelType
{
Normal, // 普通主界面
Fixed, // 固定窗口
PopUp, // 弹出窗口
Other, //其他窗口
}
public static GComponent GetTargetRoot(UIPanelType type)
{
if (type == UIPanelType.Normal)
{
return GlobalComponent.Instance.NormalGRoot;
}
else if (type == UIPanelType.Fixed)
{
return GlobalComponent.Instance.FixedGRoot;
}
else if (type == UIPanelType.PopUp)
{
return GlobalComponent.Instance.PopUpGRoot;
}
else if (type == UIPanelType.Other)
{
return GlobalComponent.Instance.OtherGRoot;
}
Log.Error("uiroot type is error: " + type.ToString());
return null;
}
六、FUIComponent
FUIComponent管理所有的界面,即管理Fairgui界面记载、打开、关闭。
ShowPanelAsync打开一个界面
ShowFUIEntityAsync如果对应Panel资源没有加载就加载对应资源即LoadFUIEntitysAsync
LoadFUIEntitysAsync真正Fairgui资源加载,先找到对应的Package数据,然后CreateObjectAsync返回的结果为fuiEntity.GComponent即Fairgui的根节点。
OnInitPanelCoreData,UI实体加载后,初始化窗口数据
OnInitComponent,UI实体加载后,初始化业务逻辑数据
OnRegisterUIEvent,注册改Panel界面的UI业务逻辑事件
fuiEntity.SetRoot,跟进窗口类型,挂载在对应窗口管理根节点下
self.AllPanelsDic[(int)fuiEntity.PanelId] = fuiEntity;每个加载好的 界面存起来。
FUIEventComponent.Instance.GetUIEventHandler返回都是IFUIEventHandler
LoginEventHandler通过fuiEntity.GetComponent中转数据到LoginPanel类中处理。
窗口的资源加载好后通过RealShowPanel打开界面
比如登录界面界面打开: