一、UI基类
一般情况下都是用Panel做容器来放各种控件的,一个Panle相当一个UI小界面,然后做成Prefab进行加载,所有界面都有载入载出功能,有的可能还有等待和恢复的,适合建立一个UI基类,然后各个子界面继承。
public abstract class WUIBasePanel : MonoBehaviour
{
public abstract void OnEnter();
public abstract void OnExit();
public abstract void OnPause();
public abstract void OnResume();
}
二、UI子类
每个UI子界面都继承基类,然后实现各个方法,加载的时候可以在OnEnter()方法做一些显示动画或一些状态的改变等,OnExit()里可以做消失动画和一些关闭界面需要做的操作等。为了方便统一启用和禁用管理都添加CanvasGroup组件,也可以用来做隐藏和显示。
public class WUIPanel1 : WUIBasePanel
{
private CanvasGroup canvasGroup;
public override void OnEnter()
{
if (canvasGroup == null)
{
canvasGroup = GetComponent<CanvasGroup>();
}
canvasGroup.blocksRaycasts = true;
gameObject.SetActive(true);
}
public override void OnExit()
{
canvasGroup.blocksRaycasts = false;
gameObject.SetActive(false);
}
public override void OnPause()
{
canvasGroup.blocksRaycasts = false;
}
public override void OnResume()
{
canvasGroup.blocksRaycasts = true;
}
public void ClosePanel1()
{
WUIManager.instance.PopPanel();
}
public void OpenPanel2()
{
WUIManager.instance.PushPanel("UIPanel2");
}
三、UI管理类
需要一个Manager类统一管理所有的UI类,做成单例,因为每个Panel都是做成预制体来加载,所以需要一个Dictionary进行管理每个预制体所有的路径,每个预制体势力后也同一个Dictionary来管理做缓存,一个界面打开子界面,再打开孙界面,先关了孙界面才能管子界面,所以适合使用一个Stack来管理界面的显示和关闭。方法也建立两个一个PushPanel(),一个PopPanel()。
public class WUIManager
{
private static WUIManager Instance;
public static WUIManager instance
{
get
{
if (Instance == null)
{
Instance = new WUIManager();
}
return Instance;
}
}
public Dictionary<string, string> pathDict = new Dictionary<string, string>
{
{ "UIPanel1","UIPanel/Panel1"},
{ "UIPanel2","UIPanel/Panel2"},
{ "UIPanel3","UIPanel/Panel3"}
};
private Dictionary<string, WUIBasePanel> panelDict = new Dictionary<string, WUIBasePanel>();
private Stack<WUIBasePanel> panelStack;
private Transform CanvasTransform;
public Transform canvasTransform
{
get
{
if (CanvasTransform == null)
{
CanvasTransform = GameObject.Find("Canvas").transform;
}
return CanvasTransform;
}
}
/// <summary>
/// 显示界面
/// </summary>
/// <param name="type"></param>
public void PushPanel(string panelName)
{
if (panelStack == null)
{
panelStack = new Stack<WUIBasePanel>();
}
if (panelStack.Count > 0)
{
WUIBasePanel topPanel = panelStack.Peek();
topPanel.OnPause();
}
WUIBasePanel panel = GetPanel(panelName);
Debug.Log("push panel:" + panel.name);
panel.OnEnter();
panelStack.Push(panel);
}
/// <summary>
/// 隐藏界面,隐藏逻辑自己写在界面代码里
/// </summary>
public void PopPanel()
{
if (panelStack == null || panelStack.Count < 1)
{
Debug.Log("stack have no panel");
return;
}
else
{
WUIBasePanel topPanel = panelStack.Pop();
topPanel.OnExit();
if (panelStack.Count > 0)
{
WUIBasePanel curPanel = panelStack.Peek();
curPanel.OnResume();
}
}
}
四、使用
显示
WUIManager.instance.PushPanel("UIPanel1");
关闭
WUIManager.instance.PopPanel();
效果如下:
五、源码文章来源:https://www.toymoban.com/news/detail-545248.html
https://download.csdn.net/download/emailforwei/87254372文章来源地址https://www.toymoban.com/news/detail-545248.html
到了这里,关于Unity 3D 开发--UI管理框架的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!