Unity简单的UI框架---------UGUI

/* 
    那天看了SIKI的一个UI框架,他是用到了json解析,感觉有点繁琐,对于新增需求或者需求变动,所更改的内容比较多: 
    1、需要更改json文本  2、需要更改对应的枚举类型。所以自己又总结了一个简单的,只需要增加或者更改UIPanelPath即可, 
    里面都是存放着对应的Resources文件夹下的UI面板路径 
 */  
using UnityEngine;  
using System.Collections;  
using System.Collections.Generic;  
  
///   
/// UI管理器,用于管理所有面板的显示和隐藏  
///   
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 panelDict;    //储存实例化面板的字典  
    private Stack panelStack;    //储存面板的栈  
  
    ///   
    /// 获取面板  
    ///   
    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();  
            panelDict.Add(panelPath, basePanel);  
            return basePanel;  
        }  
        else  
        {  
            return panel;  
        }  
    }  
  
    ///   
    /// 初始化对应的字典和栈  
    ///   
    private void Init()  
    {  
        if (panelDict == null)  
            panelDict = new Dictionary();  
        if (panelStack == null)  
            panelStack = new Stack();  
    }  
  
    ///   
    /// 入栈,显示面板  
    ///   
    ///  面板的路径   
    public void PushPanel(string panelPath)  
    {  
        if (panelStack.Count > 0)  
        {  
            BasePanel topPanel = panelStack.Peek();  
            topPanel.OnPause();  
        }  
        BasePanel panel = GetPanel(panelPath);  
        panel.OnEnter();  
        panelStack.Push(panel);  
    }  
  
    ///   
    /// 出栈,移除面板  
    ///   
    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;  
  
///   
/// Resources文件夹下UI面板的路径,新增和变动只需更改这里即可  
///   
public class UIPanelPath {  
  
    public const string MAINUIPANEL = "UIPanel/MainUIPanel";  
    public const string MISSIONCHANGEPANEL = "UIPanel/MissionChangePanel";  
}  

using UnityEngine;  
using System.Collections;  
  
///   
/// 面板基类,每个面板继承此类,重写里面的方法即可  
///   
public class BasePanel : MonoBehaviour {  
  
    //每个子类面板都挂CanvasGroup组件,用于控制显示和隐藏  
    protected CanvasGroup canvasGroup;   
  
    public virtual void Awake()  
    {  
        canvasGroup = GetComponent();  
    }  
  
    ///   
    /// 面板显示  
    ///   
    public virtual void OnEnter()  
    {  
    }  
  
    ///   
    /// 面板暂停  
    ///   
    public virtual void OnPause()  
    {  
    }  
  
    ///   
    /// 面板恢复  
    ///   
    public virtual void OnResume()  
    {  
    }  
  
    ///   
    /// 面板关闭  
    ///   
    public virtual void OnExit()  
    {  
    }  
}  

using UnityEngine;  
using System.Collections;  
  
///   
///启动框架  
///   
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);  
    }  
  
    ///   
    /// 点击导弹按钮  
    ///   
    private void OnClickMissileButton()  
    {  
        Debug.Log("导弹");  
    }  
  
    ///   
    /// 点击卫星按钮  
    ///   
    private void OnClickSatelliteButton()  
    {  
        Debug.Log("卫星");  
    }  
  
    ///   
    /// 点击空间站按钮  
    ///   
    private void OnClickSpaceportButton()  
    {  
        UIManager.Instance.PushPanel(UIPanelPath.MISSIONCHANGEPANEL);  
    }  
  
    public override void OnPause()  
    {  
        canvasGroup.alpha = 0;  
    }  
  
    public override void OnResume()  
    {  
        canvasGroup.alpha = 1;  
    }  
}  

你可能感兴趣的:(UGUI,Unity)