解决点
ET框架可以说是比较好的一个框架了,但是ET为了让小白快速的跑起来,放弃了很多东西,只留下了简要的东西,这就让上手ET后需要扩展的东西挺多的,今天就动手扩展一个菊花等待组件吧(提示,消息弹窗 同理)
功能点
- 需要能够菊花间不干扰
- 菊花需要等自定义显示和延迟显示和一直显示时间
- 菊花要能全部强制关闭
- 外部要能获取到当前菊花是否正在显示
- 菊花要能自定义友好提示
实现
- 定义菊花等待组件实体类
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace ET
{
public class UIWaitComponent:Entity
{
public static UIWaitComponent Instance;
/// <summary>
/// 提示文本
/// </summary>
public Text TipsText;
/// <summary>
/// 倒计时文本
/// </summary>
public Text CountDownText;
/// <summary>
/// 等待菊花
/// </summary>
public Image WaitTexture;
/// <summary>
/// 物体主体
/// </summary>
public GameObject SelfGo;
public Dictionary<string, UIWaitData> waitDic = null;
public bool isShowWait; // 是否显示等待菊花
}
}
- 定义菊花等待的系统
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
namespace ET
{
public class UIWaitComponentAwakeSystem : AwakeSystem<UIWaitComponent>
{
public override void Awake(UIWaitComponent self)
{
UIWaitComponent.Instance = self;
self.waitDic = new Dictionary<string, UIWaitData>();
self.isShowWait = false;
ReferenceCollector rc = self.GetParent<UI>().GameObject.GetComponent<ReferenceCollector>();
self.SelfGo = rc.Get<GameObject>("UIWait");
self.TipsText = rc.Get<Text>("TipsText");
self.CountDownText = rc.Get<Text>("CountDownText");
self.WaitTexture = rc.Get<Image>("WaitTexture");
self.SelfGo.ExtSetActive(false);
}
}
public class UIWaitComponentDestroySystem : DestroySystem<UIWaitComponent>
{
public override void Destroy(UIWaitComponent self)
{
UIWaitComponent.Instance = null;
self.waitDic.Clear();
self.waitDic = null;
self.isShowWait = false;
}
}
public class UIWaitComponentUpdateSystem : UpdateSystem<UIWaitComponent>
{
public override void Update(UIWaitComponent self)
{
self.UpdateWait();
}
}
public class UIWaitEvent_VisitionWaitUI : AEvent<EventType.VisitionWaitUI>
{
protected override async ETTask Run(EventType.VisitionWaitUI a)
{
if (UIWaitComponent.Instance != null)
UIWaitComponent.Instance.AddWait(a.Key, a.IsShow, a.Tips!=null? a.Tips:"", a.DelayTime, a.OutTime, a.IsShowMask);
await ETTask.CompletedTask;
}
}
public class UIWaitEvent_ForceCloseWaitUI : AEvent<EventType.ForceCloseWaitUI>
{
protected override async ETTask Run(EventType.ForceCloseWaitUI a)
{
if (UIWaitComponent.Instance != null)
UIWaitComponent.Instance.ForceCloseAllWaitUI();
await ETTask.CompletedTask;
}
}
public static class UIWaitComponentSystem
{
/// <summary>
/// 添加等待菊花
/// </summary>
/// <param name="self"></param>
/// <param name="key"></param>
/// <param name="isShow"></param>
/// <param name="tips"></param>
/// <param name="delayTime"></param>
/// <param name="isShowMask"></param>
public static void AddWait(this UIWaitComponent self, string key, bool isShow, string tips = "", int delayTime = 2000, int outTime = 0, bool isShowMask = false)
{
self.AddWait(new UIWaitData()
{
Key = key,
IsShow = isShow,
Tips = tips,
StartTime = TimeHelper.ServerNow(),
DelayTime = delayTime + TimeHelper.ServerNow(),
OutTime = outTime == 0 ? outTime : outTime + TimeHelper.ServerNow(),
ShowMask = isShowMask
});
}
public static void AddWait(this UIWaitComponent self, UIWaitData uiWaitData)
{
if (!self.waitDic.ContainsKey(uiWaitData.Key))
self.waitDic.Add(uiWaitData.Key, uiWaitData);
else
self.waitDic[uiWaitData.Key] = uiWaitData;
}
/// <summary>
/// 移除等待菊花
/// </summary>
/// <param name="self"></param>
/// <param name="key"></param>
public static void RemoveWait(this UIWaitComponent self, string key)
{
if (self.waitDic.ContainsKey(key))
self.waitDic.Remove(key);
}
public static void RemoveWait(this UIWaitComponent self, UIWaitData uiWaitData)
{
self.RemoveWait(uiWaitData.Key);
}
/// <summary>
/// 强制关闭所有的
/// </summary>
/// <param name="self"></param>
public static void ForceCloseAllWaitUI(this UIWaitComponent self)
{
self.waitDic.Clear();
}
/// <summary>
/// 获取所有菊花
/// </summary>
/// <param name="self"></param>
/// <returns></returns>
public static UIWaitData[] GetAllWait(this UIWaitComponent self)
{
return self.waitDic.Values.ToArray();
}
/// <summary>
/// 更新wait
/// </summary>
/// <param name="self"></param>
public static void UpdateWait(this UIWaitComponent self)
{
UIWaitData[] uIWaitDatas = self.GetAllWait();
bool isShowWait = false;
UIWaitData currUIWaitData = null;
if (uIWaitDatas != null && uIWaitDatas.Length > 0)
{
for (int i = uIWaitDatas.Length - 1; i >= 0; i--)
{
// 显示等待
if (uIWaitDatas[i].IsShow)
{
// 没有达到延迟显示时间,不显示
if (uIWaitDatas[i].DelayTime >= TimeHelper.ServerNow())
{
continue;
}
// 超过了超时时间,移除
if (uIWaitDatas[i].OutTime > 0 && uIWaitDatas[i].OutTime <= TimeHelper.ServerNow())
{
self.RemoveWait(uIWaitDatas[i].Key);
continue;
}
isShowWait = true;
currUIWaitData = uIWaitDatas[i];
break;
}
else // 不显示的移除
{
self.RemoveWait(uIWaitDatas[i].Key);
continue;
}
}
}
if (isShowWait != self.isShowWait && self.SelfGo != null)
{
self.SelfGo.ExtSetActive(isShowWait);
self.isShowWait = isShowWait;
}
if (currUIWaitData != null)
{
self.TipsText.GetComponent<Text>().text = currUIWaitData.Tips;
self.CountDownText.GetComponent<Text>().text = (TimeHelper.ServerNow() - currUIWaitData.StartTime).ToString();
}
}
}
}
- 定义菊花等待的创建
3.1 UIType 中增加 public const string UIWait = "UIWait";
3.2 创建菊花UI代码
using UnityEngine;
namespace ET
{
[UIEvent(UIType.UIWait)]
public class UIWaitEvent : AUIEvent
{
public override async ETTask<UI> OnCreate(UIComponent uiComponent)
{
await ResourcesComponent.Instance.LoadBundleAsync(UIType.UIWait.StringToAB());
GameObject bundleGameObject = (GameObject)ResourcesComponent.Instance.GetAsset(UIType.UIWait.StringToAB(), UIType.UIWait);
GameObject gameObject = UnityEngine.Object.Instantiate(bundleGameObject);
UI ui = EntityFactory.CreateWithParent<UI, string, GameObject>(uiComponent, UIType.UIWait, gameObject);
ui.AddComponent<UIWaitComponent>();
return ui;
}
public override void OnRemove(UIComponent uiComponent)
{
ResourcesComponent.Instance.UnloadBundle(UIType.UIWait.StringToAB());
}
}
}
- 定义外部调用菊花的Event事件
/// <summary>
/// 显示菊花控制
/// </summary>
public struct VisitionWaitUI
{
public string Key;
public bool IsShow;
public string Tips;
public bool IsShowMask;
public int DelayTime;//延迟显示时间 毫秒
public int OutTime;//自动关闭时间 毫秒
}
- 将菊花等待添加到UIHelper.cs方便使用
/// <summary>
/// 是否显示菊花
/// </summary>
/// <param name="key"></param>
/// <param name="isShow"></param>
/// <param name="tips"></param>
/// <param name="delayTime"></param>
/// <param name="isShowMask"></param>
public static void VisitionWaitUI(string key, bool isShow, string tips = "", int delayTime = 0, int outTime = 0, bool isShowMask = false)
{
UIWaitComponent.Instance.AddWait(key, isShow, tips, delayTime, outTime, isShowMask);
}
/// <summary>
/// 是否在转菊
/// </summary>
/// <returns></returns>
public static bool IsShowWait()
{
return UIWaitComponent.Instance.isShowWait;
}
/// <summary>
/// 关闭所有菊花
/// </summary>
public static void ForceCloseWait()
{
UIWaitComponent.Instance.ForceCloseAllWaitUI();
}
- 外部使用菊花等待方法
UIHelper.VisitionWaitUI()
UIHelper.IsShowWait()
UIHelper.ForceCloseWait()
UIWaitComponent.Instance.AddWait()
UIWaitComponent.Instance.RemoveWait()
UIWaitComponent.Instance.ForceCloseWait()
UIWaitComponent.Instance.ForceCloseWait()
await Game.EventSystem.Publish(new EventType.VisitionWaitUI() { Key = "LoginHelper_Connect", IsShow = true });
await Game.EventSystem.Publish(new EventType.VisitionWaitUI() { Key = "LoginHelper_Connect", IsShow = false });
效果图
后面补上,现在的UI太low了