コード例 #1
0
    public void showUI(EUiId id)
    {
        //1:加载当前UI
        if (dicShowUI.ContainsKey(id))//当前UI已经在显示列表中了,就直接返回
        {
            return;
        }
        UIBasePanel ui = getUI(id);               //通过ID获取需要显示的UI,从dicAllUI容器中获取,得到的是隐藏的UIPanel

        if (ui == null)                           //如果在dicAllUI容器中没有此UI,就从资源中读取ui预制体
        {
            string path = UIPath.getUiIdPath(id); //通过ID,获取对应的路径
            if (!string.IsNullOrEmpty(path))
            {
                GameObject prefab = Resources.Load <GameObject>(path);//加载资源
                //获取UI原始数据
                Vector2 offMin    = prefab.GetComponent <RectTransform>().offsetMin;
                Vector2 offMax    = prefab.GetComponent <RectTransform>().offsetMax;
                Vector2 anchorMin = prefab.GetComponent <RectTransform>().anchorMin;
                Vector2 anchorMax = prefab.GetComponent <RectTransform>().anchorMax;
                if (prefab != null)                                           //资源加载成功
                {
                    GameObject goWillShowUI = GameObject.Instantiate(prefab); //克隆游戏对象到层次面板上
                    //goWillShowUI.SetActive(true);
                    ui = goWillShowUI.GetComponent <UIBasePanel>();           //获取此对象上的UI
                    if (ui != null)
                    {
                        //Transform root = getUIRoot(ui);//获取UI所对应的根节点
                        //放入根节点下面
                        UtilUI.addChildToParent(transUIRoot, goWillShowUI.transform);//放入根节点下面
                        //数据的恢复
                        goWillShowUI.GetComponent <RectTransform>().offsetMin = offMin;
                        goWillShowUI.GetComponent <RectTransform>().offsetMax = offMax;
                        goWillShowUI.GetComponent <RectTransform>().anchorMin = anchorMin;
                        goWillShowUI.GetComponent <RectTransform>().anchorMax = anchorMax;
                        prefab = null;//清空预制体对象
                    }
                }
                else
                {
                    Debug.LogError("资源" + path + "不存在");
                }
            }
        }
        //2:更新显示其它的UI
        UpdateOtherUIState(ui);

        //3:显示当前UI
        dicAllUI[id]  = ui;
        dicShowUI[id] = ui;
        ui.show();
    }