private GameObject GetPopUp(GameObject panel)
 {
     if (panel != null)
     {
         DestroyEventTrigger popUp = panel.GetComponentInChildren <DestroyEventTrigger>();
         if (popUp != null)
         {
             return(popUp.gameObject);
         }
     }
     return(null);
 }
        private void OnDestroy(GameObject gameObject)
        {
            Transform parent;

            if (gameObject.transform.parent != null)
            {
                parent = gameObject.transform.parent;
            }
            else
            {
                DestroyEventTrigger trigger = gameObject.GetComponent <DestroyEventTrigger>();
                parent = trigger.parent;
            }

            if (parent != null)
            {
                if (parent.parent != null)
                {
                    NGUITools.Destroy(parent);
                }
            }
        }
        public GameObject RemovePopUp(GameObject popUp)
        {
            if (popUp != null)
            {
                DestroyEventTrigger trigger = popUp.GetComponent <DestroyEventTrigger>();
                if (trigger != null)
                {
                    trigger.onDestroy -= OnDestroy;
                }

                Transform panel = popUp.transform.parent;
                if (panel != null && panel.name.IndexOf(POPUPPANEL_NAME) != -1)
                {
                    NGUITools.Destroy(panel.gameObject);
                }
                else
                {
                    NGUITools.Destroy(popUp);
                }
            }
            return(popUp);
        }
        public GameObject AddPopUp(GameObject prefab, bool mode = true, string id = null, bool isBringForward = true, float maskAlpha = MASK_ALPHA, bool canRemove = true)
        {
            GameObject layer = GetPopUpLayer(true);

            if (layer == null || prefab == null)
            {
                return(null);
            }

            GameObject          popUp    = null;
            GameObject          maskClip = null;
            GameObject          panel    = null;
            DestroyEventTrigger trigger  = null;

            if (!string.IsNullOrEmpty(id))
            {
                panel = GetPopUpPanel(id, layer);
                popUp = GetPopUp(panel);
            }
            else
            {
                if (prefab.transform.parent != null)
                {
                    if (prefab.transform.parent.parent == layer)
                    {
                        panel = prefab.transform.parent.gameObject;
                        popUp = prefab;
                    }
                }
            }

            if (popUp != null)
            {
                maskClip = GetMask(panel);
                if (mode == false && maskClip)
                {
                    NGUITools.Destroy(maskClip);
                }
                else if (mode == true && maskClip == null)
                {
                    maskClip = CreateMask(panel, maskAlpha);
                }
            }
            else
            {
                if (panel == null)
                {
                    panel = NGUITools.AddChild(layer);
                }

                panel.name = POPUPPANEL_NAME + (!string.IsNullOrEmpty(id) ? ("_" + id) : "");
                layer.SetActive(true);

                if (prefab.transform.parent == null)
                {
                    popUp = NGUITools.AddChild(panel, prefab);
                }
                else
                {
                    popUp = prefab;
                    Transform t = popUp.transform;
                    t.parent        = panel.transform;
                    t.localPosition = Vector3.zero;
                    t.localRotation = Quaternion.identity;
                    t.localScale    = Vector3.one;
                    popUp.layer     = panel.layer;
                }

                panel.AddComponent <UIPanel>();
                trigger = popUp.GetComponent <DestroyEventTrigger>();
                if (trigger == null)
                {
                    trigger = popUp.AddComponent <DestroyEventTrigger>();
                }

                trigger.canBeBatchRemoved = canRemove;
                NGUITools.SetLayer(panel, panel.layer);
                trigger.onDestroy += OnDestroy;

                if (mode)
                {
                    maskClip = CreateMask(panel, maskAlpha);
                }
            }

            AdjustPanelSortingOrderAndDepth(layer, panel);
            if (isBringForward)
            {
                FUtil.BringForwardInParent(layer, panel);
            }

            return(popUp);
        }