1.介绍
一个好的游戏多多少少都有许多酷炫的特效,所以我基于ET6框架封装了一个特效组件用来实例化和销毁特效。
陈芬辉/ET - Gitee.comhttps://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);
}