极简UI框架实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/osuckseed/article/details/85159951

原理:通过定义“栈”集合,维护一个后进先出的UI队列,实现极简的UI控制管理

概念1:反向切换(先关闭后打开的UI)

概念2:

public class UIManagerData : DataInst<UIManagerData>
{
    //定义“栈”集合,存储显示当前所有[反向切换]的窗体类型
    Dictionary<string, GameObject> wins = new Dictionary<string, GameObject>();
    private Stack<GameObject> _StaCurrentUIForms=new Stack<GameObject>();
    //维护后进先出的UI队列 
    //ui open 方法 传递UI名字即可 //返回GameObject 可以直接操作对象上的控制脚本
    public GameObject ShowUIPushStack(string _path)
    {
        if (!wins.ContainsKey(_path))
        {
            wins.Add(_path,null);
        }


        var win = wins[_path];
        if (win==null)
        {
            GameObject store = UnityEngine.GameObject.Instantiate(Resources.Load("UI/{0}".TTOFormat(_path))) as GameObject;
           
            if (MainUIManager.GetInst() != null)
            {
                store.transform.SetParent(MainUIManager.GetInst().transform, false);
            }
            else if (GameUIManager.GetInst() != null)
            {
                store.transform.SetParent(GameUIManager.GetInst().transform, false);
            }
            win= wins[_path]= store;
        }
        if (_StaCurrentUIForms.Contains(win))
        {
            win.SetActive(true);
            //_StaCurrentUIForms.Select(s=>  s=win).First<GameObject>().SetActive(true);
        }
        else
        {
            win.SetActive(true);
            _StaCurrentUIForms.Push(win);
        }
        return win;


    }
    //依次关闭已经展示的UI
    public void PopUI()
    {
        if (_StaCurrentUIForms.Count >= 2)
        {
            //出栈处理
            var topUIForms = _StaCurrentUIForms.Pop();
            //做隐藏处理
            topUIForms.gameObject.SetActive(false);
            //出栈后,下一个窗体做“重新显示”处理。
            var nextUIForms = _StaCurrentUIForms.Peek();
            nextUIForms.gameObject.SetActive(true);
        }
        else if (_StaCurrentUIForms.Count == 1)
        {
            //出栈处理
            var topUIForms = _StaCurrentUIForms.Pop();
            //做隐藏处理
            topUIForms.gameObject.SetActive(false);
        }
    }
    public bool IsOpen(string _path)
    {
        if (wins.ContainsKey(_path))
        {
            if (wins[_path] != null)
            {
                return wins[_path].activeSelf;
            }
        }
        return false;
    }
}

猜你喜欢

转载自blog.csdn.net/osuckseed/article/details/85159951