/*
那天看了SIKI的一个UI框架,他是用到了json解析,感觉有点繁琐,对于新增需求或者需求变动,所更改的内容比较多:
1、需要更改json文本 2、需要更改对应的枚举类型。所以自己又总结了一个简单的,只需要增加或者更改UIPanelPath即可,
里面都是存放着对应的Resources文件夹下的UI面板路径
*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// UI管理器,用于管理所有面板的显示和隐藏
/// </summary>
public class UIManager{
private static UIManager instance;
public static UIManager Instance
{
get
{
if (instance==null)
{
instance = new UIManager();
}
return instance;
}
}
private UIManager()
{
Init();
}
private Transform canvasTransform;
private Transform CanvasTransform
{
get
{
if (canvasTransform==null)
{
canvasTransform = GameObject.Find("Canvas").transform;
}
return canvasTransform;
}
}
private Dictionary<string, BasePanel> panelDict; //储存实例化面板的字典
private Stack<BasePanel> panelStack; //储存面板的栈
/// <summary>
/// 获取面板
/// </summary>
private BasePanel GetPanel(string panelPath)
{
BasePanel panel;
panelDict.TryGetValue(panelPath, out panel);
if (panel==null)
{
GameObject insObj = GameObject.Instantiate(Resources.Load(panelPath)) as GameObject;
insObj.transform.SetParent(CanvasTransform, false);
BasePanel basePanel = insObj.GetComponent<BasePanel>();
panelDict.Add(panelPath, basePanel);
return basePanel;
}
else
{
return panel;
}
}
/// <summary>
/// 初始化对应的字典和栈
/// </summary>
private void Init()
{
if (panelDict == null)
panelDict = new Dictionary<string, BasePanel>();
if (panelStack == null)
panelStack = new Stack<BasePanel>();
}
/// <summary>
/// 入栈,显示面板
/// </summary>
/// <param name="panelPath"> 面板的路径 </param>
public void PushPanel(string panelPath)
{
if (panelStack.Count > 0)
{
BasePanel topPanel = panelStack.Peek();
topPanel.OnPause();
}
BasePanel panel = GetPanel(panelPath);
panel.OnEnter();
panelStack.Push(panel);
}
/// <summary>
/// 出栈,移除面板
/// </summary>
public void PopPanel()
{
if (panelStack.Count <= 0) return;
BasePanel topPanel = panelStack.Pop();
topPanel.OnExit();
if (panelStack.Count <= 0) return;
BasePanel nextPanel = panelStack.Peek();
nextPanel.OnResume();
}
}
using UnityEngine;
using System.Collections;
/// <summary>
/// Resources文件夹下UI面板的路径,新增和变动只需更改这里即可
/// </summary>
public class UIPanelPath {
public const string MAINUIPANEL = "UIPanel/MainUIPanel";
public const string MISSIONCHANGEPANEL = "UIPanel/MissionChangePanel";
}
using UnityEngine;
using System.Collections;
/// <summary>
/// 面板基类,每个面板继承此类,重写里面的方法即可
/// </summary>
public class BasePanel : MonoBehaviour {
//每个子类面板都挂CanvasGroup组件,用于控制显示和隐藏
protected CanvasGroup canvasGroup;
public virtual void Awake()
{
canvasGroup = GetComponent<CanvasGroup>();
}
/// <summary>
/// 面板显示
/// </summary>
public virtual void OnEnter()
{
}
/// <summary>
/// 面板暂停
/// </summary>
public virtual void OnPause()
{
}
/// <summary>
/// 面板恢复
/// </summary>
public virtual void OnResume()
{
}
/// <summary>
/// 面板关闭
/// </summary>
public virtual void OnExit()
{
}
}
using UnityEngine;
using System.Collections;
/// <summary>
///启动框架
/// </summary>
public class FrameworkStart : MonoBehaviour {
void Start ()
{
UIManager.Instance.PushPanel(UIPanelPath.MAINUIPANEL);
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class MainUIPanel : BasePanel {
[SerializeField]
private Button missileButton;
[SerializeField]
private Button satelliteButton;
[SerializeField]
private Button spaceportButton;
private void Start()
{
missileButton.onClick.AddListener(OnClickMissileButton);
satelliteButton.onClick.AddListener(OnClickSatelliteButton);
spaceportButton.onClick.AddListener(OnClickSpaceportButton);
}
/// <summary>
/// 点击导弹按钮
/// </summary>
private void OnClickMissileButton()
{
Debug.Log("导弹");
}
/// <summary>
/// 点击卫星按钮
/// </summary>
private void OnClickSatelliteButton()
{
Debug.Log("卫星");
}
/// <summary>
/// 点击空间站按钮
/// </summary>
private void OnClickSpaceportButton()
{
UIManager.Instance.PushPanel(UIPanelPath.MISSIONCHANGEPANEL);
}
public override void OnPause()
{
canvasGroup.alpha = 0;
}
public override void OnResume()
{
canvasGroup.alpha = 1;
}
}