基于ET6框架的特效组件

8 篇文章 1 订阅

1.介绍

一个好的游戏多多少少都有许多酷炫的特效,所以我基于ET6框架封装了一个特效组件用来实例化和销毁特效。

陈芬辉/ET - Gitee.comicon-default.png?t=LBL2https://gitee.com/chen_fen_hui/ET/tree/ETPro

2.核心代码解析

1. UIEffectDefine.cs

 定义每个特效的参数

    public class UIEffectClass
    {
        public string name;     //名字
        public string effectPath;  //特效路径
        public bool isLoop;    //是否循环
        public float liveTime; //存活时间(秒),-1永久
    }

    public static class UIEffectDefine
    {
        //添加每个特效的参数
        public static UIEffectClass UI_gaizhang = new UIEffectClass()
        {
            name = "UI_gaizhang",
            effectPath = "UI_gaizhang",
            isLoop = false,
            liveTime = 0.2f,
        };
    }

2.UIEffectSystem.cs

每个特效的实体类

        public static void Init(this UIEffect self, int sortingOrder, UIEffectClass effectConfig, Action createCallback, Action destroyCallback)
		{
            //加载特效资源
			ResourcesComponent resourcesComponent = Game.Scene.GetComponent<ResourcesComponent>();
			GameObject bundleGameObject = (GameObject)resourcesComponent.GetAsset(UIEffectComponent.Instance.effectAbName.StringToAB(), effectConfig.effectPath);
            //实例化
			self.go = UnityEngine.Object.Instantiate(bundleGameObject);
			self.name = effectConfig.name + IdGenerater.Instance.GenerateId();
			self.go.name = self.name;

			self.config = effectConfig;
			self.timer = effectConfig.liveTime;
            //设置层级
			Renderer[] renderers = self.go.GetComponentsInChildren<Renderer>(true);
			foreach (Renderer render in renderers)
			{
				render.sortingOrder = sortingOrder;
			}
            createCallback?.Invoke();

			self.destroyCallback = destroyCallback;
        }

3.UIEffectComponentSystem

管理所有的UIEffect类

        public override void Update(UIEffectComponent self)
        {
            //非循环播放的特效到时间销毁
            for (int i = 0; i < self.uiEffectList.Count; i++)
            {
                UIEffect effect = self.uiEffectList[i];
                if (effect.config.liveTime == -1 || effect.config.isLoop)
                    continue;
                effect.timer -= Time.deltaTime;
                if (effect.timer <= 0)
                {
                    self.Remove(effect);
                }
            }
        }

3.使用

1.在UIEffectDefine.cs定义特效参数 

      //非循环特效
      public static UIEffectClass UI_gaizhang = new UIEffectClass()
        {
            name = "UI_gaizhang",
            effectPath = "UI_gaizhang",
            isLoop = false,
            liveTime = 0.2f,
        };
        //循环特效
        public static UIEffectClass jianzhuzhong = new UIEffectClass()
        {
            name = "jianzhuzhong",
            effectPath = "jianzhuzhong",
            isLoop = true,
            liveTime = -1f,
        };

2.创建特效

        //界面显示调用
        public static void ShowWindow(this DlgLogin self, Entity contextData = null)
        {              
            创建循环特效       
            self.jianzhaozhong = UIEffectComponent.Instance.Add(self.View.EButton_login.transform, self.sortingOrder+1,UIEffectDefine.jianzhuzhong,()=>
            {
                Log.Debug("effect create!!!!");
            },()=>
            {
                Log.Debug("effect destroy!!!!");
            });
        }

        //按钮点击事件
        public static void OnLogin(this DlgLogin self)
        {
            //创建非循环特效
            UIEffectComponent.Instance.Add(self.View.EButton_login.transform, self.sortingOrder + 1, UIEffectDefine.UI_gaizhang, () =>
            {
                Log.Debug("effect create!!!!");
            }, () =>
            {
                Log.Debug("effect destroy!!!!");
            });
        }

3.销毁特效(循环特效要手动销毁,非循环特效到时间会自动销毁)

      //隐藏界面调用
      public static void HideWindow(this DlgLogin self)
        {
             //销毁循环特效
            UIEffectComponent.Instance.Remove(self.jianzhaozhong);
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萧寒大大

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值