Unity UI框架开发(4)--实现UIWindowCtrl

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

最后一个类,也是最重要的一个控制类,功能包括窗口的创建、显示、
隐藏、重现、删除等。
由于最近忙,没时间详细解读,看客们只能自己研究学习了~_~。

public class UIWindowCtrl {

    private UIBaseWindow currentWindow;
    private List<UIBaseWindow> mWindowList;

    private static UIWindowCtrl _instance;
    public static UIWindowCtrl GetInstance(){
        if (null == _instance)
        {
            _instance = new UIWindowCtrl();
        }
        return _instance;
    }

    private UIWindowCtrl()
    {
        mWindowList = new List<UIBaseWindow>();
    }

    // 当前显示的窗口
    public UIBaseWindow GetCurrentWindow()
    {
        return currentWindow;
    }

    // 显示一个窗口
    public void ShowWindow(System.Type type)
    {
        if (null != currentWindow && !(currentWindow.GetType().Equals(typeof(Win_Main))) )
        {
            // 如果是Win_Main主窗口不关闭,可以在其上加显示其他窗口
            // 否则先关闭当前窗口,再显示其他窗口
            if (!currentWindow.GetType().Equals(type))
            {
                WindowStop(currentWindow);
                currentWindow = null;
            }
        }
        UIBaseWindow window = WindowContains(type);
        if(null!=window){
            WindowResume(window);
        }else{
            CreateWindow(type);
        }
    }

    // 创建一个窗口实例存于窗口列表
    private void CreateWindow(Type type)
    {
        UIBaseWindow window;
        window = Activator.CreateInstance(type) as UIBaseWindow;
        mWindowList.Add(window);
        currentWindow = window;
        window.OnCreate(window.ToString());
        WindowResume(window);
    }

    // 窗口的重现
    public void WindowResume(UIBaseWindow window)
    {
        currentWindow = window;
        window.OnResume();
    }

    // 判断窗口是否已存在,避免重复创建
    private UIBaseWindow WindowContains(Type type)
    {
        foreach(UIBaseWindow window in mWindowList){

            if(type.Equals(window.GetType())){
                return window;
            }        
        }
        return null;
    }

    public void WindowStop(UIBaseWindow window)
    {
        window.OnStop();
    }

    public void ClearWindowData()
    {
        if(mWindowList.Count>0){
            mWindowList.Clear();
        }
        currentWindow = null;
    }
}

猜你喜欢

转载自blog.csdn.net/chy_xfn/article/details/52973264
今日推荐