示例#1
0
        public void ReleaseUI(string uiName, bool hideFirst = true)
        {
            if (_allPanels.ContainsKey(uiName))
            {
                UIBasePanel panel = _allPanels[uiName];
                if (hideFirst)
                {
                    Hide_Internel(panel);
                }

                Release_Internel(panel);
            }
        }
示例#2
0
        void Release_Internel(UIBasePanel panel)
        {
            if (panel == null)
            {
                return;
            }

            panel.Release();

            UIAssetManager.UnloadTexturesOnGameObject(panel.CachedGameObject);

            if (_allPanels.ContainsKey(panel.name))
            {
                _allPanels.Remove(panel.name);
            }

            UIAssetManager.UI_Destroy_GameObject(panel.CachedGameObject);
        }
示例#3
0
        void Show_Internel(UIBasePanel ui, object args, Action <UIBasePanel> onUIReady)
        {
            if (ui == null)
            {
                return;
            }

            ui.Show(args);

            if (AddToTrackerStack(ui))
            {
                UpdateColliderMask(ui);
            }

            if (onUIReady != null)
            {
                onUIReady(ui);
            }
        }
示例#4
0
        /// <summary>
        /// Releases all UI panels of current scene except UILoadingPanel.
        /// </summary>
        public void ReleaseAllUIOfCurrentUnityScene()
        {
            ClearUITrackers();

            string[] keys = new string[_allPanels.Keys.Count];
            _allPanels.Keys.CopyTo(keys, 0);

            for (int i = 0; i < keys.Length; i++)
            {
                UIBasePanel panel = _allPanels[keys[i]];
                if (!CheckNonReleaseList(keys[i]))
                {
                    Release_Internel(panel);
                }
            }

            Resources.UnloadUnusedAssets();

            SetFullScreenMask(false);
        }
示例#5
0
        public void ShowUI(string uiName, object args = null, Action <UIBasePanel> onUIReady = null)
        {
            if (string.IsNullOrEmpty(uiName))
            {
                this.LogWarning("Null ui name!!!");
                return;
            }

            if (!_allPanels.ContainsKey(uiName))
            {
                UIAssetManager.RequestUIPopup(uiName, delegate(string popupName, GameObject uiPopup)
                {
                    if (uiPopup == null)
                    {
                        Debug.LogError("Can't load the ui prefab: " + popupName);
                        return;
                    }

                    UIBasePanel panel = uiPopup.GetComponent <UIBasePanel>();
                    AnchorUIGameObject(panel);
                    panel.Setup();

                    _allPanels.Add(uiName, panel); // add to pool.

                    Show_Internel(panel, args, onUIReady);

                    if (UIReleaseCheckOnHide == false)
                    {
                        CheckToReleaseUI();
                    }
                });
            }
            else
            {
                UIBasePanel panel = _allPanels[uiName];
                Show_Internel(panel, args, onUIReady);
            }
        }
示例#6
0
        /// <summary>
        /// release hiden ui to free memory if the amount of ui is too large.
        /// </summary>
        void CheckToReleaseUI()
        {
            if (_allPanels.Count > MaxAliveUICount)
            {
                hidenPanels.Clear();

                foreach (var panel in _allPanels.Values)
                {
                    if (!panel.IsActive())
                    {
                        if (!IsInTrackerStack(panel.name))
                        {
                            // TODO: release only low-frequently used UIs, not all.
                            hidenPanels.Add(panel);

                            if (_allPanels.Count - hidenPanels.Count <= MaxAliveUICount)
                            {
                                break;
                            }
                        }
                    }
                }

                if (hidenPanels.Count > 0)
                {
                    UIBasePanel panel = null;

                    for (int i = 0; i < hidenPanels.Count; i++)
                    {
                        panel = hidenPanels[i];
                        Debug.Log("--> release ui: " + panel.name);

                        uiRoot.StartCoroutine(ReleaseAsync(panel));
                    }
                    hidenPanels.Clear();
                }
            }
        }
示例#7
0
        void UpdateColliderMask(UIBasePanel ui)
        {
            if (ui == null || ui.colliderType == UIColliderType.None)
            {
                SetFullScreenMask(false);
            }
            else
            {
                SetFullScreenMask(true);

                Image mask = uiRoot.fullScreenMask;
                mask.transform.SetParent(uiRoot.maskRoot); // reset ui's sibling index

                if (ui.colliderType == UIColliderType.Transparent)
                {
                    SetMaskAlpha(mask, 0f);
                }
                else if (ui.colliderType == UIColliderType.DarkMask)
                {
                    SetMaskAlpha(mask, 0.5f);
                }

                if (mask.enabled)
                {
                    if (mask.transform.parent != ui.CachedTransform.parent)
                    {
                        mask.transform.SetParent(ui.CachedTransform.parent);
                    }

                    mask.transform.SetAsLastSibling();
                    ui.CachedTransform.SetAsLastSibling();
                    //int siblingIndex = ui.CachedTransform.GetSiblingIndex();
                    //mask.transform.SetSiblingIndex(siblingIndex);
                }
            }
        }
示例#8
0
        IEnumerator ReleaseAsync(UIBasePanel panel)
        {
            yield return(null);

            Release_Internel(panel);
        }
示例#9
0
        void Hide_Internel(UIBasePanel ui)
        {
            if (ui == null || ui.IsActive() == false)
            {
                return;
            }

            if (!IsTrackedUIType(ui.uiType))
            {
                ui.Hide();

                if (UIReleaseCheckOnHide == true)
                {
                    CheckToReleaseUI();
                }

                return;
            }

            if (_uiStack.Count == 0)
            {
                ui.Hide();
            }
            else if (_uiStack.Count == 1)
            {
                UITracker tracker = _uiStack.Peek();

                if (tracker.windowStack.Count > 0)
                {
                    int    lastIndex = tracker.windowStack.Count - 1;
                    string uiName    = tracker.windowStack[lastIndex];

                    if (ui.name == uiName)
                    {
                        tracker.windowStack.RemoveAt(lastIndex);
                        ui.Hide();
                    }
                }
                else
                {
                    this.LogWarning("The only one ui in scene can't be hiden: " + ui.name);
                }
            }
            else
            {
                UITracker tracker = _uiStack.Peek();
                if (tracker.windowStack.Count > 0)
                {
                    int    lastIndex = tracker.windowStack.Count - 1;
                    string uiName    = tracker.windowStack[lastIndex];

                    if (ui.name == uiName)
                    {
                        tracker.windowStack.RemoveAt(lastIndex);
                        ui.Hide();
                    }
                }
                else
                {
                    if (ui.name == tracker.uiScene)
                    {
                        _uiStack.Pop();  // pop tracker and hide the old uiScene
                        ui.Hide();

                        // show new tracker ui
                        tracker = _uiStack.Peek();
                        ShowUI(tracker.uiScene);

                        for (int i = 0; i < tracker.windowStack.Count; i++)
                        {
                            ShowUI(tracker.windowStack[i]);
                        }
                    }
                }
            }

            string topUIName = GetTopUIName();

            if (_allPanels.ContainsKey(topUIName))
            {
                UpdateColliderMask(_allPanels[topUIName]);
            }
            else
            {
                UpdateColliderMask(null);
            }

            if (UIReleaseCheckOnHide == true)
            {
                CheckToReleaseUI();
            }
        }
示例#10
0
        bool AddToTrackerStack(UIBasePanel ui)
        {
            if (!IsTrackedUIType(ui.uiType))
            {
                return(false);
            }

            string uiName = ui.name;

            if (_uiStack.Count == 0)
            {
                if (ui.uiType == UIType.Window)
                {
                    this.LogError("You must show a UIType.Scene ui before any UIType.Window on new scenes loaded");
                    return(false);
                }
                else
                {
                    UITracker tracker = new UITracker();
                    tracker.uiScene = uiName;
                    _uiStack.Push(tracker);
                    return(true);
                }
            }
            else
            {
                if (ui.uiType == UIType.Window)
                {
                    UITracker tracker = _uiStack.Peek();

                    if (tracker.windowStack.Contains(uiName))
                    {
                        tracker.windowStack.Remove(uiName);
                    }

                    tracker.windowStack.Add(uiName);
                }
                else
                {
                    // hide old scene ui
                    UITracker oldTracker = _uiStack.Peek();
                    if (oldTracker.uiScene != uiName)
                    {
                        if (_allPanels.ContainsKey(oldTracker.uiScene))
                        {
                            _allPanels[oldTracker.uiScene].Hide();
                        }

                        for (int i = 0; i < oldTracker.windowStack.Count; i++)
                        {
                            if (_allPanels.ContainsKey(oldTracker.windowStack[i]))
                            {
                                _allPanels[oldTracker.windowStack[i]].Hide();
                            }
                        }

                        // add tracker of new scene
                        UITracker tracker = new UITracker();
                        tracker.uiScene = uiName;
                        _uiStack.Push(tracker);
                    }
                }
                return(true);
            }
        }
示例#11
0
        void AnchorUIGameObject(UIBasePanel ui)
        {
            if (ui == null)
            {
                return;
            }

            if (uiRoot == null)
            {
                Debug.LogWarning("UIRoot was not initialized.");
                GameObject prefab = Resources.Load <GameObject>("UI/UIRoot");
                GameObject go     = GameObject.Instantiate(prefab) as GameObject;
                go.name = prefab.name;
                uiRoot  = go.GetComponent <UIRoot>();
            }

            Vector2 anchoredPos = Vector2.zero;
            Vector2 sizeDelta   = Vector2.zero;
            Vector3 scale       = Vector3.one;

            RectTransform rectTran = ui.CachedRectTransform;

            if (rectTran != null)
            {
                anchoredPos = rectTran.anchoredPosition;
                sizeDelta   = rectTran.sizeDelta;
                scale       = rectTran.localScale;
            }
            else
            {
                anchoredPos = ui.CachedTransform.localPosition;
                scale       = ui.CachedTransform.localScale;
            }

            if (ui.uiType == UIType.Scene)
            {
                ui.CachedTransform.SetParent(uiRoot.sceneRoot);
            }
            else if (ui.uiType == UIType.Window)
            {
                ui.CachedTransform.SetParent(uiRoot.windowRoot);
            }
            else if (ui.uiType == UIType.Popup)
            {
                ui.CachedTransform.SetParent(uiRoot.popupRoot);
            }
            else if (ui.uiType == UIType.Other)
            {
                ui.CachedTransform.SetParent(uiRoot.otherRoot);
            }

            if (rectTran != null)
            {
                rectTran.anchoredPosition = anchoredPos;
                rectTran.sizeDelta        = sizeDelta;
                rectTran.localScale       = scale;
            }
            else
            {
                ui.CachedTransform.localPosition = anchoredPos;
                ui.CachedTransform.localScale    = scale;
            }
        }