在游戏当中,或许会存在各式各样的提示面板,例如:密码账号错误!请重新输入!该用户名已经存在!您的信息中包含敏感内容!等等...
对于这种UI复现率高的面板,我们可以将其制作成面板管理器,每一次只改变面板内的提示信息。(根据你的需求可以修改UI内容,此处不详细描述)
对于提示信息管理的代码思路:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 提示管理器 提示所有相关提示的公共方法给外部
/// </summary>
public class TipMgr : BaseManager<TipMgr>
{
/// <summary>
/// 显示一键提示面板
/// </summary>
/// <param name="info"></param>
public void ShowOneBtnTip(string info)
{
UIManager.GetInstance().ShowPanel<OneBtnTipPanel>("OneBtnTipPanel", E_UI_Layer.System, (panel) =>
{
panel.InitInfo(info);
});
}
}
此处使用继承了单例模式的基类(此处没给出源码,感兴趣可以私聊我获取),显示面板(使用了搭建的UI框架,并调用其中的初始化方法,初始化面板内显示的文字信息。
对于提示面板中,其中一个面板的代码逻辑思路,如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class OneBtnTipPanel : BasePanel
{
// Start is called before the first frame update
void Start()
{
GetControl<Button>("btnSure").onClick.AddListener(() =>
{
UIManager.GetInstance().HidePanel("OneBtnTipPanel");
});
}
public void InitInfo(string info)
{
GetControl<Text>("txtInfo").text = info;
}
}
以上代码使用了UI框架,可以不必通过拖拽等方式获取特定UI,如果你没有搭建类似的框架,可以直接使用拖拽方式,当然效果是一致的。
以上代码思路非常简单,没有复杂逻辑,希望可以帮助一些开发小白。如果对你有所帮助,就点赞收藏吧!