Esempio n. 1
0
        protected override JSONObject ToJSON(WXHierarchyContext context)
        {
            JSONObject json = new JSONObject(JSONObject.Type.OBJECT);

            json.AddField("type", "UIGrid");

            JSONObject subJSON = new JSONObject(JSONObject.Type.OBJECT);

            // var a = JsonUtility.ToJson(uiTable, true);
            // Debug.Log("++++++++++++++++++");
            // Debug.Log(a);

            int arrangement = 1;             // 仅用于兼容Grid参数,水平

            subJSON.AddField("arrangement", (int)arrangement);

            float cellWidth = 0;             // 仅用于兼容Grid参数,Table会自动获取子元素高度撑大自身

            subJSON.AddField("cellWidth", (float)cellWidth);

            float cellHeight = 0;             // 仅用于兼容Grid参数,Table会自动获取子元素高度撑大自身

            subJSON.AddField("cellHeight", (float)cellHeight);

            int columns = uiTable.columns;

            subJSON.AddField("columns", (int)columns);

            var cellAlignmentValue = WXUtility.TryGetContainField(uiTable, "cellAlignment");

            // Debug.Log(cellAlignmentValue);
            if (cellAlignmentValue != null)
            {
                UIWidget.Pivot cellPivot      = (UIWidget.Pivot)cellAlignmentValue;
                float          cellAlignmentX = (
                    cellPivot == UIWidget.Pivot.Left ||
                    cellPivot == UIWidget.Pivot.TopLeft ||
                    cellPivot == UIWidget.Pivot.BottomLeft
                    ) ? 0.0f : ((
                                    cellPivot == UIWidget.Pivot.Center ||
                                    cellPivot == UIWidget.Pivot.Top ||
                                    cellPivot == UIWidget.Pivot.Bottom) ? 0.5f : 1.0f);

                float cellAlignmentY = (
                    cellPivot == UIWidget.Pivot.Bottom ||
                    cellPivot == UIWidget.Pivot.BottomLeft ||
                    cellPivot == UIWidget.Pivot.BottomRight
                    ) ? 0.0f : ((
                                    cellPivot == UIWidget.Pivot.Left ||
                                    cellPivot == UIWidget.Pivot.Center ||
                                    cellPivot == UIWidget.Pivot.Right) ? 0.5f : 1.0f);
                subJSON.AddField("cellAlignmentX", cellAlignmentX);
                subJSON.AddField("cellAlignmentY", cellAlignmentY);
            }

            bool autoSize = true;             // 表格则自动由子元素撑大本身单元格大小

            subJSON.AddField("autoSize", (bool)autoSize);

            int paddingX = (int)uiTable.padding.x;             // 横向间距大小

            subJSON.AddField("paddingX", (int)paddingX);

            int paddingY = (int)uiTable.padding.y;             // 纵向间距大小

            subJSON.AddField("paddingY", (int)paddingY);

            int columnLimit = uiTable.columns;             // 每行|列最多个数,0为自动

            subJSON.AddField("columnLimit", (int)columnLimit);

            json.AddField("data", subJSON);

            return(json);
        }
        protected override JSONObject ToJSON(WXHierarchyContext context)
        {
            JSONObject json = new JSONObject(JSONObject.Type.OBJECT);

            UIPanel uiPanel = _gameObject.GetComponent(typeof(UIPanel)) as UIPanel;

            if ((UnityEngine.Object)uiPanel != (UnityEngine.Object)null)
            {
                size[0] = uiPanel.width;
                size[1] = uiPanel.height;
            }

            UIWidget uiWidget = _gameObject.GetComponent(typeof(UIWidget)) as UIWidget;

            if ((UnityEngine.Object)uiWidget != (UnityEngine.Object)null)
            {
                size[0] = uiWidget.width;
                size[1] = uiWidget.height;

                WXUIWidget.changeTransform(uiWidget, this);
            }

            UISprite uiSprite = _gameObject.GetComponent(typeof(UISprite)) as UISprite;

            if ((UnityEngine.Object)uiSprite != (UnityEngine.Object)null)
            {
                size[0] = uiSprite.width;
                size[1] = uiSprite.height;

                // finish by roamye 2019.3.25
                anchor[0] = (
                    uiSprite.pivot == UIWidget.Pivot.Left ||
                    uiSprite.pivot == UIWidget.Pivot.TopLeft ||
                    uiSprite.pivot == UIWidget.Pivot.BottomLeft
                    ) ? 0.0f : ((
                                    uiSprite.pivot == UIWidget.Pivot.Center ||
                                    uiSprite.pivot == UIWidget.Pivot.Top ||
                                    uiSprite.pivot == UIWidget.Pivot.Bottom) ? 0.5f : 1.0f);

                anchor[1] = (
                    uiSprite.pivot == UIWidget.Pivot.Bottom ||
                    uiSprite.pivot == UIWidget.Pivot.BottomLeft ||
                    uiSprite.pivot == UIWidget.Pivot.BottomRight
                    ) ? 0.0f : ((
                                    uiSprite.pivot == UIWidget.Pivot.Left ||
                                    uiSprite.pivot == UIWidget.Pivot.Center ||
                                    uiSprite.pivot == UIWidget.Pivot.Right) ? 0.5f : 1.0f);
            }
            UITexture uiTexture = _gameObject.GetComponent(typeof(UITexture)) as UITexture;

            if ((UnityEngine.Object)uiTexture != (UnityEngine.Object)null)
            {
                size[0] = uiTexture.width;
                size[1] = uiTexture.height;

                // finish by roamye 2019.3.25
                anchor[0] = (
                    uiTexture.pivot == UIWidget.Pivot.Left ||
                    uiTexture.pivot == UIWidget.Pivot.TopLeft ||
                    uiTexture.pivot == UIWidget.Pivot.BottomLeft
                    ) ? 0.0f : ((
                                    uiTexture.pivot == UIWidget.Pivot.Center ||
                                    uiTexture.pivot == UIWidget.Pivot.Top ||
                                    uiTexture.pivot == UIWidget.Pivot.Bottom) ? 0.5f : 1.0f);
                anchor[1] = (
                    uiTexture.pivot == UIWidget.Pivot.Bottom ||
                    uiTexture.pivot == UIWidget.Pivot.BottomLeft ||
                    uiTexture.pivot == UIWidget.Pivot.BottomRight
                    ) ? 0.0f : ((
                                    uiTexture.pivot == UIWidget.Pivot.Left ||
                                    uiTexture.pivot == UIWidget.Pivot.Center ||
                                    uiTexture.pivot == UIWidget.Pivot.Right) ? 0.5f : 1.0f);
            }
            UILabel uiLabel = _gameObject.GetComponent(typeof(UILabel)) as UILabel;

            if ((UnityEngine.Object)uiLabel != (UnityEngine.Object)null)
            {
                size[0] = uiLabel.width;
                size[1] = uiLabel.height;

                // TODO anchor
                // finish by roamye 2019.3.25
                anchor[0] = (
                    uiLabel.pivot == UIWidget.Pivot.Left ||
                    uiLabel.pivot == UIWidget.Pivot.TopLeft ||
                    uiLabel.pivot == UIWidget.Pivot.BottomLeft
                    ) ? 0.0f : ((
                                    uiLabel.pivot == UIWidget.Pivot.Center ||
                                    uiLabel.pivot == UIWidget.Pivot.Top ||
                                    uiLabel.pivot == UIWidget.Pivot.Bottom) ? 0.5f : 1.0f);
                anchor[1] = (
                    uiLabel.pivot == UIWidget.Pivot.Bottom ||
                    uiLabel.pivot == UIWidget.Pivot.BottomLeft ||
                    uiLabel.pivot == UIWidget.Pivot.BottomRight
                    ) ? 0.0f : ((
                                    uiLabel.pivot == UIWidget.Pivot.Left ||
                                    uiLabel.pivot == UIWidget.Pivot.Center ||
                                    uiLabel.pivot == UIWidget.Pivot.Right) ? 0.5f : 1.0f);
            }

            UITable uiTable = _gameObject.GetComponent(typeof(UITable)) as UITable;

            if (uiTable != null)
            {
                var contentPivotValue = WXUtility.TryGetContainField(uiTable, "pivot");
                if (contentPivotValue != null)
                {
                    UIWidget.Pivot tableContentPivot = (UIWidget.Pivot)contentPivotValue;
                    anchor[0] = (
                        tableContentPivot == UIWidget.Pivot.Left ||
                        tableContentPivot == UIWidget.Pivot.TopLeft ||
                        tableContentPivot == UIWidget.Pivot.BottomLeft
                        ) ? 0.0f : ((
                                        tableContentPivot == UIWidget.Pivot.Center ||
                                        tableContentPivot == UIWidget.Pivot.Top ||
                                        tableContentPivot == UIWidget.Pivot.Bottom) ? 0.5f : 1.0f);

                    anchor[1] = (
                        tableContentPivot == UIWidget.Pivot.Bottom ||
                        tableContentPivot == UIWidget.Pivot.BottomLeft ||
                        tableContentPivot == UIWidget.Pivot.BottomRight
                        ) ? 0.0f : ((
                                        tableContentPivot == UIWidget.Pivot.Left ||
                                        tableContentPivot == UIWidget.Pivot.Center ||
                                        tableContentPivot == UIWidget.Pivot.Right) ? 0.5f : 1.0f);
                }
            }

            UIGrid uiGrid = _gameObject.GetComponent(typeof(UIGrid)) as UIGrid;

            if (uiGrid != null)
            {
                int entityChildrenCount = _gameObject.transform.childCount;
                if ((int)uiGrid.arrangement == 0) // Horizontal
                {
                    size[0] = uiGrid.maxPerLine * uiGrid.cellWidth == 0 ? entityChildrenCount * uiGrid.cellWidth : uiGrid.maxPerLine * uiGrid.cellWidth;
                    size[1] = uiGrid.maxPerLine == 0 ? uiGrid.cellHeight : uiGrid.cellHeight * ((entityChildrenCount - entityChildrenCount % uiGrid.maxPerLine) / uiGrid.maxPerLine + entityChildrenCount % uiGrid.maxPerLine);
                }
                else if ((int)uiGrid.arrangement == 1) // Vertical
                {
                    size[0] = uiGrid.maxPerLine == 0 ? uiGrid.cellWidth : uiGrid.cellWidth * ((entityChildrenCount - entityChildrenCount % uiGrid.maxPerLine) / uiGrid.maxPerLine + entityChildrenCount % uiGrid.maxPerLine);
                    size[1] = uiGrid.maxPerLine * uiGrid.cellHeight == 0 ? entityChildrenCount * uiGrid.cellHeight : uiGrid.maxPerLine * uiGrid.cellHeight;
                }

                UIWidget.Pivot contentPivot = uiGrid.pivot;
                anchor[0] = (
                    contentPivot == UIWidget.Pivot.Left ||
                    contentPivot == UIWidget.Pivot.TopLeft ||
                    contentPivot == UIWidget.Pivot.BottomLeft
                    ) ? 0.0f : ((
                                    contentPivot == UIWidget.Pivot.Center ||
                                    contentPivot == UIWidget.Pivot.Top ||
                                    contentPivot == UIWidget.Pivot.Bottom) ? 0.5f : 1.0f);

                anchor[1] = (
                    contentPivot == UIWidget.Pivot.Bottom ||
                    contentPivot == UIWidget.Pivot.BottomLeft ||
                    contentPivot == UIWidget.Pivot.BottomRight
                    ) ? 0.0f : ((
                                    contentPivot == UIWidget.Pivot.Left ||
                                    contentPivot == UIWidget.Pivot.Center ||
                                    contentPivot == UIWidget.Pivot.Right) ? 0.5f : 1.0f);
            }

            if (_gameObject.GetComponent(typeof(UIRoot)) != null)
            {
                size[0]            = 0;
                size[1]            = 0;
                transformPositionX = 0;
                transformPositionY = 0;
                transformScaleX    = 1;
                transformScaleY    = 1;
                transformRotation  = 0;
            }

            // 位置信息
            JSONObject position = new JSONObject(JSONObject.Type.ARRAY);

            position.Add(transformPositionX);
            position.Add(transformPositionY);

            // 缩放信息
            JSONObject scale = new JSONObject(JSONObject.Type.ARRAY);

            scale.Add(transformScaleX);
            scale.Add(transformScaleY);

            JSONObject sizeArray = new JSONObject(JSONObject.Type.ARRAY);

            //Debug.Log(size[0] + ":" + size[1]);
            sizeArray.Add(size[0]);
            sizeArray.Add(size[1]);

            JSONObject anchorArray = new JSONObject(JSONObject.Type.ARRAY);

            anchorArray.Add(anchor[0]); // 0.5
            anchorArray.Add(anchor[1]); // 0.5

            json.AddField("type", this.getTypeName());
            JSONObject subJSON = new JSONObject(JSONObject.Type.OBJECT);

            subJSON.AddField("position", position);
            subJSON.AddField("rotation", transformRotation);
            subJSON.AddField("scale", scale);
            subJSON.AddField("size", sizeArray);
            subJSON.AddField("anchor", anchorArray);
            json.AddField("data", subJSON);

            return(json);
        }