Ejemplo n.º 1
0
        /// <summary>
        /// 切换UIRoot
        /// </summary>
        /// <param name="uiRoot"></param>
        void ChangeUIRoot(Transform uiRoot)
        {
            if (uiRoot == null)
            {
                return;
            }

            //先记录旧的UIRoot
            Transform oldUIRoot = this.uiRoot;

            this.uiRoot = uiRoot;
            hudCamera   = uiRoot.gameObject.FindComponent <Camera>("CameraHUD");
            uiCamera    = uiRoot.gameObject.FindComponent <Camera>("Camera");
            poolCanvas  = uiRoot.FindTransform("CanvasPool");

            foreach (UIType uiType in Enum.GetValues(typeof(UIType)))
            {
                if (uiType == UIType.Child)
                {
                    continue;
                }

                Canvas tempCanvas = uiRoot.gameObject.FindComponent <Canvas>($"Canvas{uiType}");
                canvasDic[uiType] = tempCanvas;

                if (!showDic.ContainsKey(uiType))
                {
                    if ((uiType & StackType) != 0)
                    {
                        showDic[uiType] = new UIStackContainer(uiType, tempCanvas.sortingOrder);
                    }
                    else
                    {
                        showDic[uiType] = new UIListContainer(uiType, tempCanvas.sortingOrder);
                    }
                }
            }

            //改变池中UI的父节点
            foreach (var kv in poolDic)
            {
                if (kv.Value.UI != null && kv.Value.UI.Transform != null)
                {
                    if (kv.Value.UI.Transform.parent != poolCanvas)
                    {
                        kv.Value.UI.Transform.SetParent(poolCanvas, true);
                    }
                }
            }

            //改变ui的父节点
            for (int i = 0; i < uiList.Count; i++)
            {
                UIContext uiContex = uiList[i];
                if (uiContex != null && uiContex.UI != null && uiContex.UI.Transform != null)
                {
                    SetUIParent(uiContex.UI.Transform, uiContex.UIData.UIType, true);
                }
            }

            if (oldUIRoot != null)
            {
                GameObject.Destroy(oldUIRoot.gameObject);
                oldUIRoot = null;
            }
        }
Ejemplo n.º 2
0
 public void SetContext(UIContext uiContext)
 {
     this.UIContext = uiContext;
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 加载UI
        /// </summary>
        /// <param name="uiName">ui名字</param>
        /// <returns></returns>
        private Task LoadUI(string uiName)
        {
            UIContext tempUIContext = FindUIContext(uiName);

            if (tempUIContext != null)
            {
                //加载完成
                return(tempUIContext.TCS.Task);
            }
            else
            {
                //从池中找
                if (poolDic.TryGetValue(uiName, out tempUIContext))
                {
                    poolDic.Remove(uiName);
                    uiList.Add(tempUIContext);
                    return(tempUIContext.TCS.Task);
                }

                //加载新UI
                UIData uiData = null;
                uiRegisterDic.TryGetValue(uiName, out uiData);

                //是否子UI
                if (uiData == null)
                {
                    uiChildRegisterDic.TryGetValue(uiName, out uiData);
                }

                if (uiData != null)
                {
                    tempUIContext        = new UIContext();
                    tempUIContext.UIData = uiData;
                    tempUIContext.TCS    = new TaskCompletionSource <bool>();
                    uiList.Add(tempUIContext);

                    if (tempUIContext.UIData.UIResType != UIResType.SetGameObject)
                    {
                        Main.Instance.StartCoroutine(LoadAsset(GetAssetUrl(uiName), go =>
                        {
                            if (tempUIContext.UIData.IsChildUI)
                            {
                                GameUI parentUI = FindUI(tempUIContext.UIData.ParentUIName) as GameUI;
                                if (parentUI != null)
                                {
                                    tempUIContext.UI = new ChildUI(tempUIContext.UIData.UIName, parentUI);
                                }
                            }
                            else
                            {
                                tempUIContext.UI = new GameUI();
                            }

                            tempUIContext.UI.SetContext(go, tempUIContext);

                            if (tempUIContext.TCS != null)
                            {
                                tempUIContext.TCS.SetResult(true);
                            }
                        }));
                    }
                    else
                    {
                        //处理SetGameObject的子UI
                        GameUI parentUI = FindUI(tempUIContext.UIData.ParentUIName) as GameUI;
                        if (parentUI != null)
                        {
                            if (tempUIContext.UIData.IsChildUI)
                            {
                                tempUIContext.UI = new ChildUI(tempUIContext.UIData.UIName, parentUI);

                                if (parentUI != null)
                                {
                                    GameObject childGameObject = parentUI.ChildParentNode.FindGameObject(tempUIContext.UIData.UIName);
                                    if (childGameObject == null)
                                    {
                                        Debug.LogErrorFormat("父UI:{0}不存在子UI节点:{1}", tempUIContext.UIData.ParentUIName, tempUIContext.UIData.UIName);
                                    }
                                    else
                                    {
                                        tempUIContext.UI.SetContext(childGameObject, tempUIContext);
                                        tempUIContext.TCS.SetResult(true);
                                    }
                                }
                            }
                        }
                    }

                    return(tempUIContext.TCS.Task);
                }

                Debug.LogError($"{uiName}:不存在");
                return(null);
            }
        }