private void Awake() { this.m_scrollRect = this.transform.GetComponent <ScrollRect>(); this.m_sizeDelta = this.m_scrollRect.GetComponent <RectTransform>().sizeDelta; this.m_scrollRect.onValueChanged.AddListener(new UnityAction <Vector2>(this.ScrollRectEvent)); this.m_scrollRect.decelerationRate = 0.001f; if (!this.transform.Find("Content")) { Debug.LogError("必须先编辑Content,才能使用!"); return; } this.m_content = this.transform.Find("Content").GetComponent <RectTransform>(); m_poolObj = this.transform.Find("Pool"); if (m_poolObj != null) { Destroy(m_poolObj); } m_poolObj = new GameObject("Pool").GetComponent <Transform>(); m_poolObj.SetParent(this.transform); GameObjectUtils.ResetTransform(m_poolObj); GridLayoutGroup m_gridLayoutGroup = this.m_content.GetComponent <GridLayoutGroup>(); this.m_constraintCount = m_gridLayoutGroup.constraintCount; this.m_cellSize = m_gridLayoutGroup.cellSize; this.m_spacing = m_gridLayoutGroup.spacing; this.m_padding = m_gridLayoutGroup.padding; this.m_constraint = m_gridLayoutGroup.constraint; if (this.m_constraint == GridLayoutGroup.Constraint.FixedColumnCount) { this.m_scrollRect.vertical = true; this.m_scrollRect.horizontal = false; } else if (this.m_constraint == GridLayoutGroup.Constraint.FixedRowCount) { this.m_scrollRect.vertical = false; this.m_scrollRect.horizontal = true; } else { this.m_scrollRect.vertical = true; this.m_scrollRect.horizontal = true; } Destroy(m_gridLayoutGroup); this.m_contentLocalPos = this.m_content.localPosition; GameObjectUtils.DestroyChilds(this.m_content.transform); ContentSizeFitter contentSizeFitter = this.m_content.GetComponent <ContentSizeFitter>(); if (contentSizeFitter != null) { Destroy(contentSizeFitter); } this.m_handlerDic = new Dictionary <int, AItemHandler>(); this.m_itemStack = new Stack <GameObject>(); }
public Layout Constraint(GridLayoutGroup.Constraint constraint) { if (layoutGroup is GridLayoutGroup glg) { glg.constraint = constraint; } else { Debug.LogError("Call Grid() before Constraint()"); } return(this); }
public void SetStorageItemList(List <IScrollViewItem> storageItemList, GridLayoutGroup.Constraint constraint = 1) { this.Init(); IEnumerator enumerator = this._contentTrans.GetEnumerator(); try { while (enumerator.MoveNext()) { Transform current = (Transform)enumerator.Current; UnityEngine.Object.Destroy(current.gameObject); } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable == null) { } disposable.Dispose(); } Vector2 cellSize = this._grid.cellSize; RectTransform transform2 = base.transform as RectTransform; this._grid.constraint = constraint; float width = 0f; float num2 = 0f; float height = 0f; float num4 = 0f; this._gridConstraint = constraint; if (constraint == GridLayoutGroup.Constraint.FixedColumnCount) { width = transform2.rect.width; height = transform2.rect.height; num2 = cellSize.x + this._grid.spacing.x; num4 = cellSize.y + this._grid.spacing.y; } else if (constraint == GridLayoutGroup.Constraint.FixedRowCount) { width = transform2.rect.height; height = transform2.rect.width; num2 = cellSize.y + this._grid.spacing.y; num4 = cellSize.x + this._grid.spacing.x; } this._grid.constraintCount = Mathf.FloorToInt(width / num2); this._storageItemList = storageItemList; this._createdStorageItemNum = 0; this._max_storage_item_num_shown = Mathf.FloorToInt((height / num4) + 2f) * this._grid.constraintCount; this.LoadStorageItemFromSpecifiedIndex(0); }
public static DialogGUIGridLayout GUIGridLayout(RectOffset padding, Vector2 cellSize, Vector2 spacing, GridLayoutGroup.Corner startCorner, GridLayoutGroup.Axis startAxis, TextAnchor childAligment, GridLayoutGroup.Constraint constraint, Int32 constraintCount, Action optionBuilder, Modifier <DialogGUIGridLayout> modifier = null) { DialogGUIGridLayout element = new DialogGUIGridLayout(padding, cellSize, spacing, startCorner, startAxis, childAligment, constraint, constraintCount, Declare(optionBuilder)); if (modifier != null) { element = modifier(element); } _elements.Add(element); return(element); }
public void SetGridGroup(GridLayoutGroup grid, RectOffset offset, Vector2 size, Vector2 space, int count, GridLayoutGroup.Constraint constr = GridLayoutGroup.Constraint.Flexible, GridLayoutGroup.Corner starC = GridLayoutGroup.Corner.UpperLeft , TextAnchor childA = TextAnchor.UpperLeft, GridLayoutGroup.Axis starA = GridLayoutGroup.Axis.Horizontal ) { if (!isHaveUIGrid) { Debug.Log("not set grid"); return; } Debug.Log("set grid"); grid.padding = offset; grid.cellSize = size; grid.spacing = space; grid.startCorner = starC; grid.startAxis = starA; grid.childAlignment = childA; grid.constraint = constr; if (constr == GridLayoutGroup.Constraint.FixedColumnCount) { grid.constraintCount = count; } }
public static ScrollRect newScrollView(Transform parent, string strName, Vector3 pos, Vector2 size, Vector2 sizeContent, Vector2 spacingContent, GridLayoutGroup.Axis axis, TextAnchor childAlignment, GridLayoutGroup.Constraint constraint, int constraintCount) { var scrollRect = newGameObject <ScrollRect>(parent, strName, pos, size); var viewport = newGameObject <Mask>(scrollRect.transform, "Viewport", Vector3.zero, size); var content = newGameObject <GridLayoutGroup>(viewport.transform, "Content", Vector3.zero, size); content.cellSize = sizeContent; content.spacing = spacingContent; content.startAxis = axis; content.childAlignment = childAlignment; content.constraint = constraint; content.constraintCount = constraintCount; scrollRect.content = content.GetComponent <RectTransform>(); return(scrollRect); }
private IEnumerator PositionElements() { bool UIError = false; gridLayoutGroup = GetComponent <GridLayoutGroup>(); RectOffset padding = gridLayoutGroup.padding; Vector2 spacing = gridLayoutGroup.spacing; itemsCount = gameObject.transform.childCount; GridLayoutGroup.Constraint constraint = gridLayoutGroup.constraint; int constraintCount = gridLayoutGroup.constraintCount; if (constraint.ToString() == "Flexible") { //Constraint is "Flexible". So let's keep the columns count fixed. float columnsAproximate = Mathf.Sqrt(itemsCount); if (columnsAproximate > 0) { columns = (((int)columnsAproximate) == columnsAproximate) ? (int)columnsAproximate : ((int)columnsAproximate) + 1; rows = (itemsCount % columns == 0) ? itemsCount / columns : (itemsCount / columns) + 1; } else { columns = 0; rows = 0; print("No child items in current object !"); UIError = true; } } else { //We have a constraint count on rows/columns. Check which string constraintStr = gridLayoutGroup.constraint.ToString(); if (constraintStr == "FixedRowCount") { rows = constraintCount; columns = (itemsCount % rows == 0) ? itemsCount / rows : (itemsCount / rows) + 1; } else { if (constraintStr == "FixedColumnCount") { columns = constraintCount; rows = (itemsCount % columns == 0) ? itemsCount / columns : (itemsCount / columns) + 1; } else { print("Unknown constraint error in GridLayoutGroup !"); UIError = true; } } } if (!UIError) { float sizeX = 0.0f; float sizeY = 0.0f; //X-Coordinate stretch in function of padding and spacing float allColumnsSize = parentRectWidth - padding.left - padding.right; float spacingSizeLine = (columns - 1) * spacing.x; allColumnsSize -= spacingSizeLine; sizeX = allColumnsSize / columns; //Y-Coordinate stretch in function of padding and spacing float allRowsSize = parentRectHeight - padding.top - padding.bottom; float spacingSizeColumn = (rows - 1) * spacing.y; allRowsSize -= spacingSizeColumn; sizeY = allRowsSize / rows; if (forceSquareItems) { if (sizeX < sizeY) { sizeY = sizeX; } else { sizeX = sizeY; } } gridLayoutGroup.cellSize = new Vector2(sizeX, sizeY); } yield return(null); }