public void SetDataList(List <GameResData> dataList) { if (dataList == null) { return; } TransformUtil.ClearChildren(rewardIconRoot, true); int count = dataList.Count; int spaceHorizon = 130; int spaceVertical = 120; int columnMax = 4; int totalRow = count / columnMax + (count % columnMax == 0 ? 0 : 1); // GridLayoutGroup layout = rewardIconRoot.GetComponent <GridLayoutGroup>(); float height = layout.cellSize.y + layout.spacing.y; RectTransform rectTran = rewardIconRoot.transform as RectTransform; Vector2 rewardRootSizeDelta = rectTran.sizeDelta; rewardRootSizeDelta = rectTran.sizeDelta = new Vector2(rewardRootSizeDelta.x, height * totalRow - layout.spacing.y + 50); if (totalRow == 1) { if (layout != null) { layout.enabled = false; } } else { if (layout != null) { layout.enabled = true; } } rewardIconPrefab.SetActive(true); float xPadding = rewardRootSizeDelta.x / 2; float yPadding = -100; for (int i = 0; i < count; i++) { GameObject clone = Instantiate <GameObject>(rewardIconPrefab); CommonRewardIcon icon = CommonRewardIcon.Create(clone.transform); RectTransform iconTran = icon.transform as RectTransform; iconTran.SetAsFirstSibling(); // iconTran.anchorMax = iconTran.anchorMin = new Vector2(0.5f,0.5f); GameResData resData = dataList[i]; icon.SetGameResData(resData); Transform tran = clone.transform; tran.SetParent(rewardIconRoot, false); float x = 0; float y = 0; int row = i / columnMax; int col = i % columnMax; if (totalRow == 1) //单行时居中 { x = count % 2 == 0 ?((i - count / 2) * spaceHorizon + spaceHorizon / 2):((i - count / 2) * spaceHorizon); } else //左对齐 { x = columnMax % 2 == 0 ? ((col - columnMax / 2) * spaceHorizon + spaceHorizon / 2):((col - columnMax / 2) * spaceHorizon); y = totalRow % 2 == 0 ? ((totalRow / 2 - row) * spaceVertical - spaceVertical / 2):((totalRow / 2 - row) * spaceVertical); } tran.localPosition = new Vector3(x + xPadding, y + yPadding); Text countText = tran.Find("text_count").GetComponent <Text>(); countText.text = string.Format(Localization.Get("common.x_count"), resData.count); icon.ShowRewardCount(true); } rewardIconPrefab.SetActive(false); }