private void GenerateField() { if (!_gridLayoutGroup) { return; } while (_fullCount < GameConfig.ItemCount) { _gridLayoutGroup.CalculateLayoutInputHorizontal(); _gridLayoutGroup.CalculateLayoutInputVertical(); _gridLayoutGroup.SetLayoutHorizontal(); _gridLayoutGroup.SetLayoutVertical(); _row = (int)(_gridLayoutGroup.preferredHeight / _gridLayoutGroup.cellSize.y); float value = GameConfig.ItemCount / (float)_row; bool condition = (value - (int)value) > 0; _column = condition ? ((int)value) + 1 : (int)value; _fullCount = _row * _column; for (int i = 0; i < Mathf.Abs(_fullCount - GameConfig.ItemCount); i++) { var temp = _itemPrefab.GetInstance(); _field.Add(temp); temp.transform.SetParent(_gridLayoutGroup.transform, false); temp.gameObject.name = i.ToString(); } } Debug.LogFormat("<color=olive><size=15><b>row:{0}, column:{1}</b></size></color>", _row, _column); }
public void TestVerticallyContrainedCalculateLayoutVertical() { m_LayoutGroup.constraint = GridLayoutGroup.Constraint.FixedRowCount; m_LayoutGroup.constraintCount = 2; m_LayoutGroup.startAxis = GridLayoutGroup.Axis.Vertical; m_LayoutGroup.startCorner = GridLayoutGroup.Corner.LowerRight; Assert.AreEqual(GridLayoutGroup.Constraint.FixedRowCount, m_LayoutGroup.constraint, "Expected layout group constraint mode to match but it did not"); Assert.AreEqual(2, m_LayoutGroup.constraintCount, "Expected layout group constraint count mode to match but it did not"); m_LayoutGroup.CalculateLayoutInputHorizontal(); m_LayoutGroup.SetLayoutHorizontal(); m_LayoutGroup.CalculateLayoutInputVertical(); m_LayoutGroup.SetLayoutVertical(); Assert.AreEqual(390, m_LayoutGroup.minWidth, "Expected layout group min width to match but it did not"); Assert.AreEqual(100, m_LayoutGroup.minHeight, "Expected layout group min height to match but it did not"); Assert.AreEqual(390, m_LayoutGroup.preferredWidth, "Expected layout group preferred width to match but it did not"); Assert.AreEqual(100, m_LayoutGroup.preferredHeight, "Expected layout group preferred height to match but it did not"); Assert.AreEqual(-1, m_LayoutGroup.flexibleWidth, "Expected layout group flexiblle width to match but it did not"); Assert.AreEqual(-1, m_LayoutGroup.flexibleHeight, "Expected layout group flexiblle height to match but it did not"); Vector2[] expectedPositions = { new Vector2(300, -100), new Vector2(300, -50), new Vector2(200, -100), new Vector2(200, -50), new Vector2(100, -100), new Vector2(100, -50), new Vector2(0, -100), new Vector2(0, -50), }; Vector2 expectedSize = new Vector2(90, 50); for (int i = 0; i < expectedPositions.Length; ++i) { var element = m_LayoutGroup.transform.Find("Element" + (i + 1)); var rectTransform = element.GetComponent <RectTransform>(); Assert.AreEqual(expectedPositions[i], rectTransform.anchoredPosition, $"Expected Element { i + 1 } position to match but it did not"); Assert.AreEqual(expectedSize, rectTransform.sizeDelta, $"Expected Element { i + 1 } size to match but it did not"); } }
public static void ForceGridLayoutGroupRebuild(RectTransform rootTransform) { if (!LIB_GameController.IS_INVENTORY_OPEN) { // Force the layout panel to update when creating the slots - allows me to match the highlight object to the first in the sequence rootTransform.parent.gameObject.SetActive(true); GridLayoutGroup groupLayout = rootTransform.GetComponent <GridLayoutGroup>(); groupLayout.CalculateLayoutInputHorizontal(); groupLayout.CalculateLayoutInputVertical(); groupLayout.SetLayoutHorizontal(); groupLayout.SetLayoutVertical(); LayoutRebuilder.ForceRebuildLayoutImmediate(rootTransform); rootTransform.parent.gameObject.SetActive(false); } }
public static int SetLayoutHorizontal(IntPtr l) { int result; try { GridLayoutGroup gridLayoutGroup = (GridLayoutGroup)LuaObject.checkSelf(l); gridLayoutGroup.SetLayoutHorizontal(); LuaObject.pushValue(l, true); result = 1; } catch (Exception e) { result = LuaObject.error(l, e); } return(result); }
public void CellsSpawn() { for (int i = 0; i < battleMapHeight.value; i++) { for (int y = 0; y < battleMapWidth.value; y++) { GameObject mapCell = Instantiate(cell) as GameObject; mapCell.transform.SetParent(this.gameObject.transform, false); gridLayoutGroup.CalculateLayoutInputHorizontal(); gridLayoutGroup.CalculateLayoutInputVertical(); gridLayoutGroup.SetLayoutHorizontal(); gridLayoutGroup.SetLayoutVertical(); battleCellArray.cellGameObjectArray[i, y] = mapCell; mapCell.GetComponent <BattleCell>().DisplayBattleCellInfo(battleCellType.DefaultFloor.ToString(), i, y); battleMapIntArray.array[i, y] = 0; creaturesMapIntArray.array[i, y] = 0; effectsMapIntArray.array[i, y] = 0; } } }
/// <summary> /// 状态发生变化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="iState"></param> /// <param name="fieldName"></param> private void IState_Callback <T>(T iState, string fieldName) where T : IBaseState { BuffState buffState; if (TryGetBuff(iState, fieldName, out buffState)) { if (!statusEffectToObjDic.ContainsKey(buffState.statusEffect)) { GameObject stateObj = GameObject.Instantiate(stateExamplePrefab); stateObj.transform.SetParent(stateGrid.transform); UIPlayerState_State uiPlayerState_State = stateObj.GetComponent <UIPlayerState_State>(); statusEffectToObjDic.Add(buffState.statusEffect, uiPlayerState_State); } if (statusEffectToObjDic.ContainsKey(buffState.statusEffect)) { if (statusEffectToObjDic[buffState.statusEffect].SetState(buffState)) { //如果发生了显示变化则更新组件的排序 stateGrid.SetLayoutHorizontal(); stateGrid.SetLayoutVertical(); } } } }
void InitPanel() { // this is pain GameObject panel = new GameObject(); panel.name = "Panel Tile Grid"; panel.AddComponent <CanvasRenderer>(); panel.AddComponent <RectTransform>(); panel.AddComponent <Image>(); panel.AddComponent <GridLayoutGroup>(); panel.transform.SetParent(transform); GridLayoutGroup grid = panel.GetComponent <GridLayoutGroup>(); // cell size // offset // type of grid.cellSize = cellsize; RectTransform panelRectTransform = panel.GetComponent <RectTransform>(); panelRectTransform.anchorMin = new Vector2(0, 1); panelRectTransform.anchorMax = new Vector2(0, 1); panelRectTransform.pivot = new Vector2(0.0f, 1f); panelRectTransform.anchoredPosition = new Vector2(0, 0); panel.GetComponent <Image>().sprite = gridBackgroundSprite; panel.GetComponent <RectTransform>().sizeDelta = purchaseGridBackgroundSize; purchaseWindowPos.y *= -1; panelRectTransform.anchoredPosition += purchaseWindowPos; grid.SetLayoutHorizontal(); grid.constraint = GridLayoutGroup.Constraint.Flexible; CalculateColumnCount(grid); grid.cellSize = cellsize; grid.padding.left = padding.x; grid.padding.top = padding.y; grid.padding.right = padding.x; grid.padding.bottom = padding.y; for (int i = 0; i < windowSlots; i++) { GameObject windowSlot = new GameObject(); windowSlot.name = "Window Box " + i; windowSlot.AddComponent <Image>(); windowSlot.GetComponent <Image>().sprite = itemBorderBoxSprite; if (i < items.Length) { GameObject button = Instantiate(buttonPrefab); button.GetComponent <ShopButtonScript>().SetItem(items[i], cellsize / 2); button.transform.SetParent(windowSlot.transform); button.transform.position = new Vector3(button.transform.position.x, button.transform.position.y + 18, button.transform.position.z); button.GetComponent <ShopButtonScript>().InitTimerImage(windowSlot.GetComponent <RectTransform>(), cellsize); buttons.Add(button); } windowSlot.transform.SetParent(grid.transform); } }
private void Setup() { for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { HeroType ht; if (x == 0 || x == width - 1 || y == 0) { ht = HeroType.CONCRETE; } else { ht = Dot.random(); } Vector3 temp = new Vector3(Dot.calculateX(x), Dot.calculateY(y), 10f); GameObject go = Instantiate(dot, temp, Quaternion.identity, GameObject.FindGameObjectWithTag("Board").transform); go.name = "(" + x + ";" + y + ")"; dots[x, y] = new Dot(x, y, go, ht); } } glg.CalculateLayoutInputHorizontal(); glg.CalculateLayoutInputVertical(); glg.SetLayoutHorizontal(); glg.SetLayoutVertical(); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { positions[x, y] = new Vector3(dots[x, y].getObject().transform.position.x, dots[x, y].getObject().transform.position.y, dots[x, y].getObject().transform.position.z); } } y_difference = positions[0, height - 1].y - positions[0, height - 2].y; if (ContentManager.level == null) { return; } else { foreach (DotData d in ContentManager.level.dots) { string[] s = d.position.Split(':'); if (s.Length < 2) { continue; } int?x = tryParse(s[0]); if (x == null) { continue; } int?y = tryParse(s[1]); if (y == null) { continue; } if (x > width - 1 || y > height - 1) { continue; } OnType?ot = d.getOType(); if (ot != null) { dots[(int)x, (int)y].setOType((OnType)ot); } HeroType?ht = d.getType(); if (ht == null || ht == HeroType.NONE) { continue; } dots[(int)x, (int)y].setType((HeroType)ht, false); DLCType?dt = d.getDType(); if (dt != null) { dots[(int)x, (int)y].setDLCType((DLCType)dt); } } } }
public void SetLayout() { layoutGroup.SetLayoutHorizontal(); }
private void UpdateUi() { if (RenderPrefab == null) { RenderPrefab = ResourceManager.Instance.LoadExistsAsset <GameObject>(_prefabPath); } if (RenderPrefab == null) { return; } _gridGroup.enabled = true; int lastMaxFirstInitNum = _maxFirstInitNum; int lastListMin = _listMin; int lastListMax = _listMax; RectTransform viewRect = null; if (_scrollRect != null) { switch (_direction) { case Direction.Horizontal: _scrollRect.horizontal = true; _scrollRect.vertical = false; break; case Direction.Vertical: _scrollRect.horizontal = false; _scrollRect.vertical = true; break; case Direction.none: _scrollRect.horizontal = false; _scrollRect.vertical = false; break; } if ((UnityEngine.Object)_scrollRect.viewport == (UnityEngine.Object)null) { viewRect = _scrollRect.gameObject.GetRectTransform(); } else { viewRect = _scrollRect.viewport; } _scrollRect.onValueChanged.RemoveAllListeners(); //设置需要创建的最大item数量 if (_isNeedSetMaxItemNum) { if (_fullNum > 0) { _maxFirstInitNum = _fullNum + 4; } if (_maxFirstInitNum > _dataProvider.Length) { _maxFirstInitNum = 0; } else if (_bar != null) { _scrollRect.horizontalScrollbar = null; _scrollRect.verticalScrollbar = null; _bar.gameObject.SetActive(false); } if (_maxFirstInitNum > 0) { if (IsNeedRestPos == 0 || IsNeedRestPos == 3) { _listMin = 0; } else if (IsNeedRestPos == 1) { _listMin = lastListMin; if (lastListMax > _dataProvider.Length - 1) { _listMin -= lastListMax - _dataProvider.Length + 1; } if (_listMin < 0) { _listMin = 0; } } else if (IsNeedRestPos == 2) { _listMin = _dataProvider.Length - _maxFirstInitNum; } _listMax = _listMin + _maxFirstInitNum - 1; } else { _listMin = 0; _listMax = 0; } var oldPivot = _contTransform.pivot; int axis = _scrollRect.vertical ? 1 : 0; oldPivot[axis] = axis; _contTransform.pivot = oldPivot; _scrollRect.onValueChanged.AddListener(OnScroll); } } for (int i = 0; i < lastMaxFirstInitNum; i++) { _renders[lastListMin + i].gameObject.SetActive(false); _renders.Remove(lastListMin + i); if (_uiCenterOnChild != null) { Destroy(items[lastListMin + i].gameObject); items.RemoveAt(lastListMin + i); } } int initNum = _dataProvider.Length; if (_maxFirstInitNum != 0) { initNum = _maxFirstInitNum; } for (int i = 0; i < initNum; i++) { GameObject item; if (i >= _renders.Count) { if (i >= items.Count) { item = UGUITools.AddChild(gameObject, RenderPrefab); var itemRender = item.AddComponent(_itemScript) as BaseItemRender; item.name = "item" + (i + _listMin); _renders.Add(i + _listMin, itemRender); items.Add(itemRender); } else { items[i].gameObject.SetActive(true); items[i].name = "item" + (i + _listMin); _renders.Add(i + _listMin, items[i]); } } else //如果_isNeedSetMaxItemNum为false,是不进入这里的,因为前面把_renders清掉了 { item = _renders[i].gameObject; item.SetActive(true); //避免Render 里把自己给隐藏了 } _renders[i + _listMin].SetItemIndex(i + _listMin, _dataProvider.Length); _renders[i + _listMin].SetDataGridType(DataGridType); _renders[i + _listMin].SetData(_dataProvider[i + _listMin]); } //如果_isNeedSetMaxItemNum为false,是不进入这里的,因为前面把_renders清掉了 for (int i = _renders.Count - 1; i >= initNum; i--) { _renders[i].gameObject.SetActive(false); _renders.Remove(i); if (_uiCenterOnChild != null) { Destroy(items[i].gameObject); items.RemoveAt(i); } } if (_gridGroup is AutoSizeGridLayoutGroup) { (_gridGroup as AutoSizeGridLayoutGroup).ItemNum = _renders.Count; } if (_scrollRect != null) { //默认处理数量不足设置为不可拖动 if (_renders.Count <= _fullNum && _uiCenterOnChild == null) { _scrollRect.horizontal = false; _scrollRect.vertical = false; if (_bar != null) { _scrollRect.horizontalScrollbar = null; _scrollRect.verticalScrollbar = null; _bar.gameObject.SetActive(false); } } else { if (_direction == Direction.Horizontal) { _scrollRect.horizontalScrollbar = _bar; } else if (_direction == Direction.Vertical) { _scrollRect.verticalScrollbar = _bar; } } if (_direction == Direction.Horizontal) { int num; //列数 if (_gridGroup.constraint == GridLayoutGroup.Constraint.FixedRowCount) { num = (_renders.Count + _gridGroup.constraintCount - 1) / _gridGroup.constraintCount; //整数除法,向上取整 } else if (_gridGroup.constraint == GridLayoutGroup.Constraint.FixedColumnCount) { num = _gridGroup.constraintCount; } else { num = Mathf.FloorToInt((_contTransform.rect.height - _gridGroup.padding.vertical + _gridGroup.spacing.y + 1.0f / 1000.0f) / (_gridGroup.cellSize.y + _gridGroup.spacing.y)); num = Mathf.Max(1, num); num = (_renders.Count + num - 1) / num; } _contTransform.sizeDelta = new Vector2(_gridGroup.padding.horizontal + num * (_gridGroup.cellSize.x + _gridGroup.spacing.x) - _gridGroup.spacing.x, _contTransform.sizeDelta.y); _gridGroup.CalculateLayoutInputHorizontal(); _gridGroup.SetLayoutHorizontal(); _gridGroup.CalculateLayoutInputVertical(); _gridGroup.SetLayoutVertical(); if (_listMax > 0) //暂时不支持多行 { for (int i = 0; i < _renders.Count; i++) { var rectTran = _renders[i + _listMin].gameObject.GetRectTransform(); ResetPosition(rectTran, 1, _listMin * (_gridGroup.cellSize.x + _gridGroup.spacing.x)); } _contTransform.sizeDelta = new Vector2(_gridGroup.padding.horizontal + (_listMax + 1) * (_gridGroup.cellSize.x + _gridGroup.spacing.x) - _gridGroup.spacing.x, _contTransform.sizeDelta.y); _gridGroup.enabled = false; } float moveDistance = 0; float anchorGridToScrollview = (_contTransform.anchorMin.x + _contTransform.anchorMax.x) * 0.5f; _pivotPosition = (anchorGridToScrollview - 0.5f) * viewRect.rect.width; _threshold = (_gridGroup.cellSize.x + _gridGroup.spacing.x); if (IsNeedRestPos == 0) { float dGridAnchToScrLeft = anchorGridToScrollview * viewRect.rect.width; float dGridPivotToScrLeft = dGridAnchToScrLeft + _contTransform.anchoredPosition.x; float dGridPivotToGridLeft = _contTransform.rect.width * _contTransform.pivot.x; float dScrLeftToGridLeft = dGridPivotToGridLeft - dGridPivotToScrLeft; moveDistance = dScrLeftToGridLeft; } else if (IsNeedRestPos == 1) { if (_listMax > 0) { moveDistance = 0; } else if (_lastContentSize == Vector2.zero) { moveDistance = 0; } else { float delta = _contTransform.rect.width - _lastContentSize.x; moveDistance = _contTransform.pivot.x * delta; } } else if (IsNeedRestPos == 2) { float dGridAnchToScrRight = (1 - anchorGridToScrollview) * viewRect.rect.width; float dGridPivotToScrRight = dGridAnchToScrRight - _contTransform.anchoredPosition.x; float dGridPivotToGridRight = _contTransform.rect.width * (1 - _contTransform.pivot.x); float dScrRightToGridRight = dGridPivotToGridRight - dGridPivotToScrRight; moveDistance = -dScrRightToGridRight; } else if (IsNeedRestPos == 3) { float dGridPivotToScrCenter = _contTransform.anchoredPosition.x + (anchorGridToScrollview - 0.5f) * viewRect.rect.width; float dGridPivotToGridCenter = _contTransform.rect.width * (_contTransform.pivot.x - 0.5f); moveDistance = -dGridPivotToScrCenter + dGridPivotToGridCenter; } ResetPosition(_contTransform, 0, moveDistance); _startPosMin = (1 - anchorGridToScrollview) * viewRect.rect.width - _contTransform.rect.width * (1 - _contTransform.pivot.x); _startPosMax = _contTransform.rect.width * _contTransform.pivot.x - anchorGridToScrollview * viewRect.rect.width; } else if (_direction == Direction.Vertical) { float GroupLen = 0; if (_gridGroup is AutoSizeGridLayoutGroup) { bool b = IsInScroll(); _gridGroup.CalculateLayoutInputHorizontal(); _gridGroup.SetLayoutHorizontal(); _gridGroup.CalculateLayoutInputVertical(); _gridGroup.SetLayoutVertical(); GroupLen = (_gridGroup as AutoSizeGridLayoutGroup).LayoutGroupHeight; _contTransform.sizeDelta = new Vector2(_contTransform.sizeDelta.x, _gridGroup.padding.vertical + GroupLen); if (ResetPos == false && b) { IsNeedRestPos = 1; } else { if (GroupLen + _gridGroup.padding.vertical >= viewRect.rect.height) { IsNeedRestPos = 2; } else { IsNeedRestPos = 0; } } } else { int num; //行数 if (_gridGroup.constraint == GridLayoutGroup.Constraint.FixedRowCount) { num = _gridGroup.constraintCount; } else if (_gridGroup.constraint == GridLayoutGroup.Constraint.FixedColumnCount) { num = (_renders.Count + _gridGroup.constraintCount - 1) / _gridGroup.constraintCount; //整数除法,向上取整 } else { num = Mathf.FloorToInt((_contTransform.rect.width - _gridGroup.padding.horizontal + _gridGroup.spacing.x + 1.0f / 1000.0f) / (_gridGroup.cellSize.x + _gridGroup.spacing.x)); num = Mathf.Max(1, num); num = (_renders.Count + num - 1) / num; } GroupLen = num * (_gridGroup.cellSize.y + _gridGroup.spacing.y) - _gridGroup.spacing.y; _contTransform.sizeDelta = new Vector2(_contTransform.sizeDelta.x, _gridGroup.padding.vertical + GroupLen); _gridGroup.CalculateLayoutInputHorizontal(); _gridGroup.SetLayoutHorizontal(); _gridGroup.CalculateLayoutInputVertical(); _gridGroup.SetLayoutVertical(); } if (_listMax > 0) //暂时不支持多列 { for (int i = 0; i < _renders.Count; i++) { var rectTran = _renders[i + _listMin].gameObject.GetRectTransform(); ResetPosition(rectTran, 1, -_listMin * (_gridGroup.cellSize.y + _gridGroup.spacing.y)); } _contTransform.sizeDelta = new Vector2(_contTransform.sizeDelta.x, _gridGroup.padding.vertical + (_listMax + 1) * (_gridGroup.cellSize.y + _gridGroup.spacing.y) - _gridGroup.spacing.y); _gridGroup.enabled = false; } float moveDistance = 0; float anchorGridToScrollview = (_contTransform.anchorMin.y + _contTransform.anchorMax.y) * 0.5f; _pivotPosition = (anchorGridToScrollview - 0.5f) * viewRect.rect.height; _threshold = (_gridGroup.cellSize.y + _gridGroup.spacing.y); if (IsNeedRestPos == 0) { float dGridAnchToScrTop = (1 - anchorGridToScrollview) * viewRect.rect.height; float dGridPivotToScrTop = dGridAnchToScrTop - _contTransform.anchoredPosition.y; float dGridPivotToGridTop = _contTransform.rect.height * (1 - _contTransform.pivot.y); float dScrTopToGridTop = dGridPivotToGridTop - dGridPivotToScrTop; moveDistance = -dScrTopToGridTop; } else if (IsNeedRestPos == 1) { if (_listMax > 0) { // float dGridTopToScrCenter = _pivotPosition + _contTransform.anchoredPosition.y; // float dPointToGridTop = (((_listMax - _listMin + 1) >> 1) + _listMin) * _threshold; // moveDistance = dPointToGridTop - dGridTopToScrCenter; } else if (_lastContentSize == Vector2.zero) { moveDistance = 0; } else { float delta = _contTransform.rect.height - _lastContentSize.y; moveDistance = (_contTransform.pivot.y - 1) * delta; } } else if (IsNeedRestPos == 2) { float dGridAnchToScrBottom = anchorGridToScrollview * viewRect.rect.height; float dGridPivotToScrBottom = dGridAnchToScrBottom + _contTransform.anchoredPosition.y; float dGridPivotToGridBottom = _contTransform.rect.height * _contTransform.pivot.y; float dScrBottomToGridBottom = dGridPivotToGridBottom - dGridPivotToScrBottom; moveDistance = dScrBottomToGridBottom; } else if (IsNeedRestPos == 3) { float dGridPivotToScrCenter = _contTransform.anchoredPosition.y + (anchorGridToScrollview - 0.5f) * viewRect.rect.height; float dGridPivotToGridCenter = _contTransform.rect.height * (_contTransform.pivot.y - 0.5f); moveDistance = -dGridPivotToScrCenter + dGridPivotToGridCenter; } ResetPosition(_contTransform, 1, moveDistance); _startPosMin = (1 - anchorGridToScrollview) * viewRect.rect.height - _contTransform.rect.height * (1 - _contTransform.pivot.y); _startPosMax = _contTransform.rect.height * _contTransform.pivot.y - anchorGridToScrollview * viewRect.rect.height; } _lastContentSize = _contTransform.sizeDelta; } }