03-ET的生命周期函数

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生命周期也还是相对好理解的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值