TIPS:
本系列贴仅用于博主学习ET框架的记录
前言
学习ET框架的生命周期函数
一、ET的生命周期函数
跟Unity客户端中的Awake、Start、Update、Destroy一样,ET也有生命周期函数。
二、使用步骤
1.回到Computer.cs,使其继承IAwake、IUpdate、IDestroy接口。(如果要使用生命周期函数则一定要继承对应的接口,同时继承了接口也一定要实现对应的生命周期函数!)
代码如下(示例):Computer.cs
namespace ET
{
public class Computer:Entity, IAwake,IUpdate, IDestroy
{
}
}
2.找到Computer对应的System类,编写以下方法。
代码如下(示例):ComputerSystem.cs
namespace ET
{
public static class ComputerSystem
{
public static void Start(this Computer self)
{
Log.Debug("computer start....");
self.GetComponent<PCCaseComponent>().Power();
self.GetComponent<MonitorsComponent>().Display();
}
}
//Awake
public class ComputerAwakeSystem: AwakeSystem<Computer>
{
public override void Awake(Computer self)
{
Log.Debug("ComputerAwakeSystem start");
}
}
//Update
public class ComputerUpdateSystem: UpdateSystem<Computer>
{
public override void Update(Computer self)
{
Log.Debug("ComputerAwakeSystem update");
}
}
//Destroy
public class ComputerDestroySystem: DestroySystem<Computer>
{
public override void Destroy(Computer self)
{
Log.Debug("ComputerAwakeSystem Destroy");
}
}
}
3.找到AfterCreateZoneScene_AddComponent.cs编写以下方法。
代码如下(示例):AfterCreateZoneScene_AddComponent.cs
namespace ET
{
public class AfterCreateZoneScene_AddComponent: AEvent<EventType.AfterCreateZoneScene>
{
protected override void Run(EventType.AfterCreateZoneScene args)
{
...
...
...
Test(args.ZoneScene).Coroutine();
}
public async ETTask Test(Scene zoneScene)
{
Computer computer = zoneScene.AddChild<Computer>();
computer.AddComponent<PCCaseComponent>();
computer.AddComponent<MonitorsComponent>();
computer.AddComponent<MouseComponent>();
computer.AddComponent<KeyboardComponent>();
computer.Start();
//异步等待三秒后删除computer用于触发Destroy这个生命周期函数
await TimerComponent.Instance.WaitAsync(3000);
computer.Dispose();
}
}
}
3.结果
保存代码回到Unity编辑器按下键盘F5等待代码编译通过后即可运行。
总结
ET生命周期也还是相对好理解的。