Пример #1
0
        /// <summary>
        /// 获取面板
        /// </summary>
        public PanelBase GetPanel(string uiname)
        {
            if (m_PanelDict == null)
            {
                m_PanelDict = new Dictionary <string, PanelBase>();
            }

            if (m_PanelDict.TryGetValue(uiname, out PanelBase panel))
            {
                if (panel == null)
                {
                    throw new FrameworkException("[UI] 面板已被卸载");
                }
                return(panel);
            }
            else
            {
                // 根据prefab去实例化面板
                m_PanelPathDict.TryGetValue(uiname, out string path);
                GameObject instPanel = GameObject.Instantiate(m_ResMgr.Load <GameObject>(path));

                // UICore与派生类不一定在一个程序集类,所以不能直接用Type.GetType
                Assembly asmb = Assembly.Load("Assembly-CSharp");
                Type     type = asmb.GetType(uiname);

                if (type == null || !type.IsSubclassOf(typeof(PanelBase)))
                {
                    throw new FrameworkException("[UI] 面板类名错误 | 没有继承BasePanel");
                }

                PanelBase basePanel = instPanel.AddComponent(type) as PanelBase;
                basePanel.Init(uiname);
                m_PanelDict.Add(uiname, basePanel);

                Transform uiGroup = CanvasTransform.Find("Level" + basePanel.Level);
                if (uiGroup == null)
                {
                    RectTransform rect;
                    rect = (new GameObject("Level" + basePanel.Level)).AddComponent <RectTransform>();
                    rect.SetParent(CanvasTransform);
                    rect.sizeDelta        = CanvasTransform.GetComponent <UnityEngine.UI.CanvasScaler>().referenceResolution;
                    rect.anchorMin        = Vector2.zero;
                    rect.anchorMax        = Vector3.one;
                    rect.anchoredPosition = Vector2.zero;
                    rect.sizeDelta        = Vector2.zero;
                    rect.localScale       = Vector3.one;
                    uiGroup = rect;
                }
                instPanel.transform.SetParent(uiGroup, false);
                return(basePanel);
            }
        }
Пример #2
0
        /// <summary>
        /// 根据面板类型 得到实例化的面板
        /// </summary>
        /// <returns></returns>
        public PanelBase GetPanel(string uiname)
        {
            if (m_PanelDict == null)
            {
                m_PanelDict = new Dictionary <string, PanelBase>();
            }

            m_PanelDict.TryGetValue(uiname, out PanelBase panel);

            if (panel == null)
            {
                // 根据prefab去实例化面板
                m_PanelPathDict.TryGetValue(uiname, out string path);
                Debug.Log(path);
                GameObject instPanel = GameObject.Instantiate(Resources.Load(path)) as GameObject;

                // UICore与派生类不一定在一个程序集类,所以不能直接用Type.GetType
                Assembly asmb = Assembly.Load("Assembly-CSharp");
                Type     type = asmb.GetType(uiname);
                if (type == null || type.IsSubclassOf(typeof(PanelBase)))
                {
                    throw new XFrameworkException("[UI] wrong panel name or panel is not inherit BasePanel");
                }
                PanelBase basePanel = instPanel.AddComponent(type) as PanelBase;
                basePanel.Init(uiname);
                m_PanelDict.Add(uiname, basePanel);

                Transform uiGroup = CanvasTransform.Find("Level" + basePanel.Level);
                if (uiGroup == null)
                {
                    RectTransform rect;
                    rect = (new GameObject("Level" + basePanel.Level)).AddComponent <RectTransform>();
                    rect.SetParent(CanvasTransform);
                    rect.sizeDelta  = CanvasTransform.sizeDelta;
                    rect.position   = CanvasTransform.position;
                    rect.localScale = Vector3.one;
                    uiGroup         = rect;
                }
                instPanel.transform.SetParent(uiGroup, false);
                return(basePanel);
            }
            else
            {
                return(panel);
            }
        }
Пример #3
0
        /// <summary>
        /// 获取面板
        /// </summary>
        public PanelBase GetPanel(string uiname)
        {
            if (m_PanelDict.TryGetValue(uiname, out PanelBase panel))
            {
                if (panel == null)
                {
                    throw new XFrameworkException("[UI] The panel you want has been unloaded");
                }
                return(panel);
            }
            else
            {
                // 根据prefab去实例化面板
                m_PanelPathDict.TryGetValue(uiname, out string path);
                GameObject instPanel = GameObject.Instantiate(m_ResMgr.Load <GameObject>(path));

                // UICore与派生类不一定在一个程序集类,所以不能直接用Type.GetType
                Assembly asmb = Assembly.Load("Assembly-CSharp");
                Type     type = asmb.GetType(uiname);

                if (type == null || !type.IsSubclassOf(typeof(PanelBase)))
                {
                    throw new XFrameworkException("[UI] wrong panel name or panel is not inherit BasePanel");
                }

                PanelBase basePanel = instPanel.AddComponent(type) as PanelBase;
                basePanel.Init(uiname);
                m_PanelDict.Add(uiname, basePanel);

                Transform uiGroup = CanvasTransform.Find("Level" + basePanel.Level);
                if (uiGroup == null)
                {
                    RectTransform rect;
                    rect = (new GameObject("Level" + basePanel.Level)).AddComponent <RectTransform>();

                    int siblingIndex = CanvasTransform.childCount;
                    for (int i = 0, length = CanvasTransform.childCount; i < length; i++)
                    {
                        string levelName = CanvasTransform.GetChild(i).name;
                        if (int.TryParse(levelName[levelName.Length - 1].ToString(), out int level))
                        {
                            if (basePanel.Level < level)
                            {
                                siblingIndex = i;
                                break;
                            }
                        }
                    }
                    rect.SetParent(CanvasTransform);
                    rect.SetSiblingIndex(siblingIndex);
                    rect.sizeDelta          = CanvasTransform.GetComponent <UnityEngine.UI.CanvasScaler>().referenceResolution;
                    rect.anchorMin          = Vector2.zero;
                    rect.anchorMax          = Vector2.one;
                    rect.anchoredPosition3D = Vector3.zero;
                    rect.sizeDelta          = Vector2.zero;
                    rect.localScale         = Vector3.one;
                    uiGroup = rect;
                }
                instPanel.transform.SetParent(uiGroup, false);
                return(basePanel);
            }
        }