コード例 #1
0
        private void InitializeRootGroup()
        {
            dummyPanel = PanelUtils.Internal.CreatePanel(null, this, panelPrefab);
            dummyPanel.gameObject.name            = "DummyPanel";
            dummyPanel.CanvasGroup.alpha          = 0f;
            dummyPanel.CanvasGroup.interactable   = false;
            dummyPanel.CanvasGroup.blocksRaycasts = false;
            dummyPanel.Internal.SetDummy(minimumFreeSpace);

            RootPanelGroup = new PanelGroup(this, PanelDirection.Right);
            RootPanelGroup.AddElement(dummyPanel);
        }
コード例 #2
0
        public void AnchorPanel(IPanelGroupElement source, PanelCanvas canvas, PanelDirection anchorDirection)
        {
            PanelGroup rootGroup = canvas.RootPanelGroup;
            PanelGroup tempGroup = new PanelGroup(canvas, PanelDirection.Right);

            for (int i = 0; i < rootGroup.Count; i++)
            {
                if (rootGroup[i].Group == rootGroup)
                {
                    tempGroup.AddElement(rootGroup[i]);
                }
            }

            rootGroup.AddElement(tempGroup);
            AnchorPanel(source, tempGroup, anchorDirection);
        }
コード例 #3
0
        public void AnchorPanel(IPanelGroupElement source, IPanelGroupElement anchor, PanelDirection anchorDirection)
        {
            PanelGroup group = anchor.Group;

            if (group is UnanchoredPanelGroup)
            {
                Debug.LogError("Can not anchor to an unanchored panel!");
                return;
            }

            Vector2 size;
            Panel   panel = source as Panel;

            if (panel != null)
            {
                size = panel.IsDocked ? panel.FloatingSize : panel.Size;
            }
            else
            {
                ((PanelGroup)source).Internal.UpdateLayout();
                size = source.Size;
            }

            // Fill the whole anchored area in order not to break other elements' sizes on layout update
            if (anchorDirection == PanelDirection.Left || anchorDirection == PanelDirection.Right)
            {
                if (anchor.Size.y > 0f)
                {
                    size.y = anchor.Size.y;
                }
            }
            else
            {
                if (anchor.Size.x > 0f)
                {
                    size.x = anchor.Size.x;
                }
            }

            if (panel != null)
            {
                panel.RectTransform.sizeDelta = size;
            }
            else
            {
                ((PanelGroup)source).Internal.UpdateBounds(source.Position, size);
            }

            bool addElementAfter = anchorDirection == PanelDirection.Right || anchorDirection == PanelDirection.Top;

            if (group.IsInSameDirection(anchorDirection))
            {
                if (addElementAfter)
                {
                    group.AddElementAfter(anchor, source);
                }
                else
                {
                    group.AddElementBefore(anchor, source);
                }
            }
            else
            {
                IPanelGroupElement element1, element2;
                if (addElementAfter)
                {
                    element1 = anchor;
                    element2 = source;
                }
                else
                {
                    element1 = source;
                    element2 = anchor;
                }

                PanelGroup newGroup = new PanelGroup(anchor.Canvas, anchorDirection);
                newGroup.AddElement(element1);
                newGroup.AddElement(element2);

                group.Internal.ReplaceElement(anchor, newGroup);
            }

            if (panel != null)
            {
                if (draggedPanel == panel)
                {
                    draggedPanel = null;
                }

                panel.RectTransform.SetAsFirstSibling();

                if (panel.Internal.ContentScrollRect != null)
                {
                    panel.Internal.ContentScrollRect.OnDrag(nullPointerEventData);
                }
            }
        }
コード例 #4
0
        private static IPanelGroupElement Deserialize(PanelCanvas canvas, ISerializedElement element)
        {
            if (element == null)
            {
                return(null);
            }

            if (element is SerializedDummyPanel)
            {
                return(canvas.Internal.DummyPanel);
            }

            if (element is SerializedPanel serializedPanel)
            {
                Panel panel = null;

                SerializedPanelTab[] tabs = serializedPanel.tabs;
                for (int i = 0; i < tabs.Length; i++)
                {
                    if (!PanelNotificationCenter.TryGetTab(tabs[i].id, out PanelTab tab))
                    {
                        continue;
                    }

                    if (panel == null)
                    {
                        panel = tab.Detach();
                        canvas.UnanchoredPanelGroup.AddElement(panel);
                    }
                    else
                    {
                        panel.AddTab(tab);
                    }
                }

                if (panel != null)
                {
                    if (serializedPanel.activeTab < tabs.Length)
                    {
                        int activeTabIndex = panel.GetTabIndex(tabs[serializedPanel.activeTab].id);
                        if (activeTabIndex >= 0)
                        {
                            panel.ActiveTab = activeTabIndex;
                        }
                    }

                    if (serializedPanel is SerializedUnanchoredPanel unanchoredPanel)
                    {
                        panel.RectTransform.anchoredPosition = unanchoredPanel.position;
                        panel.gameObject.SetActive(unanchoredPanel.active);
                    }

                    panel.FloatingSize = serializedPanel.floatingSize;
                }

                return(panel);
            }

            if (element is SerializedPanelGroup serializedPanelGroup)
            {
                ISerializedElement[] children = serializedPanelGroup.children;
                if (children == null || children.Length == 0)
                {
                    return(null);
                }

                PanelGroup panelGroup = new PanelGroup(canvas, serializedPanelGroup.horizontal ? PanelDirection.Right : PanelDirection.Top);
                for (int i = 0; i < children.Length; i++)
                {
                    if (children[i] == null)
                    {
                        continue;
                    }

                    IPanelGroupElement childElement = Deserialize(canvas, children[i]);
                    if (childElement != null)
                    {
                        panelGroup.AddElement(childElement);
                        sizesHolder.Add(new GroupElementSizeHolder(childElement, children[i].size));
                    }
                }

                if (panelGroup.Count > 0)
                {
                    return(panelGroup);
                }
            }

            return(null);
        }
コード例 #5
0
        public static void DeserializeCanvasFromArray(PanelCanvas canvas, byte[] data)
        {
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                Debug.LogError("Can deserialize in Play mode only!");
                return;
            }
#endif

            if (data == null || data.Length == 0)
            {
                Debug.LogError("Data is null!");
                return;
            }

            SerializedCanvas serializedCanvas;
            BinaryFormatter  formatter = new BinaryFormatter();
            using (MemoryStream stream = new MemoryStream(data))
            {
                serializedCanvas = formatter.Deserialize(stream) as SerializedCanvas;
            }

            if (serializedCanvas == null)
            {
                return;
            }

            sizesHolder.Clear();
            canvas.LeaveFreeSpace = serializedCanvas.useFreeSpace;

            if (serializedCanvas.rootPanelGroup != null)
            {
                PanelGroup           rootPanelGroup = canvas.RootPanelGroup;
                ISerializedElement[] children       = serializedCanvas.rootPanelGroup.children;
                for (int i = children.Length - 1; i >= 0; i--)
                {
                    IPanelGroupElement element = Deserialize(canvas, children[i]);
                    if (element != null)
                    {
                        if (rootPanelGroup.Count == 0)
                        {
                            rootPanelGroup.AddElement(element);
                        }
                        else
                        {
                            rootPanelGroup.AddElementBefore(rootPanelGroup[0], element);
                        }

                        sizesHolder.Insert(0, new GroupElementSizeHolder(element, children[i].size));
                    }
                }
            }

            if (sizesHolder.Count > 0)
            {
                canvas.ForceRebuildLayoutImmediate();

                for (int i = 0; i < sizesHolder.Count; i++)
                {
                    sizesHolder[i].element.ResizeTo(sizesHolder[i].size, PanelDirection.Right, PanelDirection.Top);
                }
            }

            if (serializedCanvas.unanchoredPanelGroup != null)
            {
                ISerializedElement[] children = serializedCanvas.unanchoredPanelGroup.children;
                for (int i = 0; i < children.Length; i++)
                {
                    if (children[i] is SerializedUnanchoredPanel unanchoredPanel)
                    {
                        Panel panel = Deserialize(canvas, unanchoredPanel) as Panel;
                        if (panel != null)
                        {
                            panel.Detach();
                            canvas.UnanchoredPanelGroup.RestrictPanelToBounds(panel);
                        }
                    }
                }
            }

            for (int i = 0; i < canvas.UnanchoredPanelGroup.Count; i++)
            {
                Panel panel = canvas.UnanchoredPanelGroup[i] as Panel;
                if (panel != null)
                {
                    panel.RectTransform.SetAsLastSibling();
                }
            }

            canvas.gameObject.SetActive(serializedCanvas.active);
        }