Exemplo n.º 1
0
        //构建Grid布局并返回布局大小
        public static Vector2 UF_BuildLayoutGrid(RectTransform rectTransform, LayoutCorner alignement, Vector2 padding, Vector2 space, int constraint)
        {
            int           curConstraint = 0;
            int           childNum      = rectTransform.childCount;
            float         curWidth      = padding.x;
            float         curHeight     = 0;
            float         maxWidth      = padding.x;
            float         maxHeight     = padding.y;
            bool          rawH          = false;
            Vector2       anchor        = UF_GetAnchor(alignement);
            Vector2       side          = UF_GetAnchorSide(alignement);
            int           ctVal         = constraint - 1;
            RectTransform rectTrans     = null;

            for (int k = 0; k < childNum;)
            {
                rectTrans = rectTransform.GetChild(k++) as RectTransform;
                if (rectTrans != null && rectTrans.gameObject.activeSelf && rectTrans.tag != DefineTag.IngoreLayout)
                {
                    rectTrans.anchorMax = anchor;
                    rectTrans.anchorMin = anchor;
                    rectTrans.pivot     = anchor;
                    if (rectTrans.rect.height > curHeight)
                    {
                        curHeight = rectTrans.rect.height;
                    }
                    rectTrans.anchoredPosition = new Vector2(curWidth * side.x, -maxHeight * side.y);
                    curWidth += rectTrans.rect.width + space.x;
                    if (maxWidth < curWidth)
                    {
                        maxWidth = curWidth;
                    }
                    rawH = false;
                    curConstraint++;
                    if (curConstraint > ctVal)
                    {
                        rawH          = true;
                        maxHeight    += curHeight + space.y;
                        curConstraint = 0;
                        curWidth      = padding.x;
                        curHeight     = 0;
                    }
                }
            }

            maxWidth += padding.x - space.x;

            if (rawH)
            {
                maxHeight += padding.y - space.y;
            }

            else
            {
                maxHeight += padding.y + curHeight;
            }

            return(new Vector2(maxWidth, maxHeight));
        }
Exemplo n.º 2
0
        static Vector2 UF_GetAnchor(LayoutCorner alignement)
        {
            switch (alignement)
            {
            case LayoutCorner.UpperLeft: return(new Vector2(0, 1));

            case LayoutCorner.UpperRight: return(new Vector2(1, 1));

            case LayoutCorner.LowerLeft: return(new Vector2(0, 0));

            case LayoutCorner.LowerRight: return(new Vector2(1, 0));
            }
            return(new Vector2(0, 1));
        }
Exemplo n.º 3
0
    protected virtual void DrawFields()
    {
        UIGrid grid       = target as UIGrid;
        int    constraint = EditorGUILayout.IntField("Constraint", grid.constraint);

        Vector2 padding = EditorGUILayout.Vector2Field("Padding", grid.padding);

        Vector2 space = EditorGUILayout.Vector2Field("Space", grid.space);

        Vector2 cellSize = EditorGUILayout.Vector2Field("CellSize", grid.cellSize);

        LayoutCorner alignement = (LayoutCorner)EditorGUILayout.EnumPopup("Alignement", grid.alignement);

        SizeFitterType fitterType = (SizeFitterType)EditorGUILayout.EnumPopup("SizeFitter", grid.fitterType);



        bool fixedCellSize = EditorGUILayout.Toggle("FixedCellSize", grid.fixedCellSize);


        GUILayout.BeginHorizontal();
        string prefabUIName = EditorGUILayout.TextField("Prefab UI", grid.prefabUI, GUILayout.Height(20));

        if (m_PrefabUI == null || m_PrefabUI.name != prefabUIName)
        {
            m_PrefabUI = GetTargetUIPrefab(prefabUIName);
        }

        Rect rectbox = GUILayoutUtility.GetRect(16, 16, GUI.skin.box);

        if (m_PrefabUI != null)
        {
            GUI.Box(rectbox, EditorGUIUtility.IconContent("lightMeter/greenLight"));
            EditorTools.ClickAndPingObject(rectbox, m_PrefabUI);
            GUI.backgroundColor = Color.blue;
            if (GUILayout.Button("G", GUILayout.Width(18), GUILayout.Height(18)))
            {
                var temp = AssetDatabase.LoadAssetAtPath <GameObject>(string.Format("Assets/AssetBases/PrefabAssets/ui/{0}.prefab", prefabUIName));
                if (temp != null)
                {
                    var item = Object.Instantiate(temp);
                    item.name                 = temp.name;
                    item.transform.parent     = grid.transform;
                    item.transform.localScale = Vector3.one;
                }
            }
            GUI.backgroundColor = Color.red;
            if (GUILayout.Button("C", GUILayout.Width(18), GUILayout.Height(18)))
            {
                EditorTools.DestroyChild(grid.gameObject);
            }
            GUI.backgroundColor = Color.white;
        }

        GUILayout.EndHorizontal();


        if (GUI.changed)
        {
            EditorTools.RegisterUndo("UIGrid", grid);
            grid.prefabUI = prefabUIName;

            grid.constraint    = constraint;
            grid.padding       = padding;
            grid.space         = space;
            grid.cellSize      = cellSize;
            grid.alignement    = alignement;
            grid.fixedCellSize = fixedCellSize;
            grid.fitterType    = fitterType;

            EditorTools.SetDirty(grid);
        }
    }