UI界面切换框架

这个框架是基于Siki之前的UI框架进行修改的。在之前框架的基础上,进行了一些个人的修改和扩展。
下面是这个框架的架构:
UI界面切换框架_第1张图片
修改的地方:
1 首先是扩展:
原框架:
原框架是通过一个BasePanel类做基类进行扩展,想要自定义一个面板类,就要继承BasePanel。
修改后:
将BasePanel替换为一个IPanel接口,节省出继承基类的空间,而且如果有一些不方便再继承BasePanel的情况下,

public interface IPanel {
    int ID { get; }
    void OnEnter();
    void OnExit();
    void OnPause();
    void OnResume();
}

2 UIManager
修改前:UIManager具有生成和存储面板prefab,PushPanel()、PopPanel()等一系列作用
修改后:单独扩展一个PanelPool类,用于生成和存储面板Prefab,对外只开放PushPanel()和PopPanel()两个方法。UIManager则主要负责与外界进行交互。

public class UIManager {
    private static UIManager _instance;    //单例模式
    public static UIManager Instance {
        get {
            if (_instance == null) {
                _instance = new UIManager();
            }
            return _instance;
        }
    }

    PanelPool panelPool;

    private UIManager() {
        panelPool = new PanelPool("Json/JsonInfo", GameObject.Find("Canvas").transform);
    }

    public void PushPanel(int PanelID) {
        panelPool.PushPanel(PanelID);
    }

    public void PopPanel() {
        panelPool.PopPanel();
    }

    public void BackToMainMenu() {
        while (panelPool.Peek().ID != 0) {
            PopPanel();
        }
    }

    public void ReplayGame() {
        BackToMainMenu();
        PushPanel(1);
    }
}

增加一个面板存储池类

public class PanelPool {

    private UIPanelInfo _panelList;
    //根据Panel的ID进行存储
    private Dictionary<int, string> _panelPathDict;
    private Dictionary<int, IPanel> _panelTypeByID;
    private Stack _panelStack;

    private Transform _rootTransform;

    public PanelPool(string jsonPath, Transform rootTransfrom) {
        _rootTransform = rootTransfrom;
        JsonToObject(jsonPath);
        DictAddItem();
    }

    //Json文件转对象
    private void JsonToObject(string path) {
        _panelList = JsonReader.GetJsonObjectFromPath(path);
    }

    // 给存储路径的字典添加选项
    private void DictAddItem() {
        if (_panelPathDict == null) _panelPathDict = new Dictionary<int, string>();
        for (int i = 0; i < _panelList.InfoList.Length; i++) {
            string PanelPathStr;
            PanelPathStr = _panelList.InfoList[i].PanelPath;
            _panelPathDict.Add(_panelList.InfoList[i].ID, PanelPathStr);
        }
    }

    public void PushPanel(int panelID) {
        if (_panelStack == null) {
            _panelStack = new Stack();
        }
        if (_panelStack.Count > 0) {
            IPanel topPanel = _panelStack.Peek();
            topPanel.OnPause();
        }
        IPanel panelToPush = GetPanel(panelID);
        panelToPush.OnEnter();
        _panelStack.Push(panelToPush);
    }

    public void PopPanel() {
        if (_panelStack == null) {
            _panelStack = new Stack();
        }
        if (_panelStack.Count <= 0) return;
        IPanel topPanel = _panelStack.Pop();
        topPanel.OnExit();
        if (_panelStack.Count <= 0) return;
        IPanel topPanel2 = _panelStack.Peek();
        topPanel2.OnResume();
    }

    public IPanel Peek() {
        if (_panelStack == null) {
            return null;
        }
        return _panelStack.Peek();
    }

    private IPanel GetPanel(int panelID) {
        if (_panelTypeByID == null) {
            _panelTypeByID = new Dictionary<int, IPanel>();
        }
        IPanel panel = _panelTypeByID.TryGet(panelID);
        if (panel == null) {
            string pathstr = _panelPathDict.TryGet(panelID);
            GameObject instPanel = GameObject.Instantiate(Resources.Load(pathstr)) as GameObject;
            instPanel.transform.SetParent(_rootTransform, false);
            _panelTypeByID.Add(panelID, instPanel.GetComponent());
            return instPanel.GetComponent();
        }
        else {
            return panel;
        }
    }
}

3 扩展一个IPanel

public class BeginPanel : MonoBehaviour, IPanel {
    //Sound按钮是否启用
    bool isBTSoundActive = true;

    //Button列表
    Button bt_Play;
    Button bt_Rate;
    Button bt_Sound;
    Button bt_Rank;
    Button bt_Shop;
    Button bt_ADS;
    Button bt_Info;
    Button bt_Quit;

    //金币(只显示金币数量)
    Text txt_Coin;

    Image img_Sound;

    //设置PanelId
    private int _id = -1;
    int IPanel.ID {
        get {
            if (_id == -1) {
                _id = 0;
            }
            return _id;
        }
    }

    private void Awake() {
        bt_Play = transform.Find("BT_Play").GetComponent

你可能感兴趣的:(学习)