void UpdateItemPrefab() { ItemPrefabConfData tData = mLoopListView.GetItemPrefabConfData("ItemPrefab1"); GameObject prefabObj = tData.mItemPrefab; RectTransform rf = prefabObj.GetComponent <RectTransform>(); ListItem6 itemScript = prefabObj.GetComponent <ListItem6>(); float w = mLoopListView.ViewPortWidth; int count = itemScript.mItemList.Count; GameObject p0 = itemScript.mItemList[0].gameObject; RectTransform rf0 = p0.GetComponent <RectTransform>(); float w0 = rf0.rect.width; int c = Mathf.FloorToInt(w / w0); if (c == 0) { c = 1; } mItemCountPerRow = c; float padding = (w - w0 * c) / (c + 1); if (padding < 0) { padding = 0; } rf.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, w); if (c > count) { int dif = c - count; for (int i = 0; i < dif; ++i) { GameObject go = Object.Instantiate(p0, Vector3.zero, Quaternion.identity, rf); RectTransform trf = go.GetComponent <RectTransform>(); trf.localScale = Vector3.one; trf.localPosition = Vector3.zero; trf.rotation = Quaternion.identity; ListItem5 t = go.GetComponent <ListItem5>(); itemScript.mItemList.Add(t); } } else if (c < count) { int dif = count - c; for (int i = 0; i < dif; ++i) { ListItem5 go = itemScript.mItemList[itemScript.mItemList.Count - 1]; itemScript.mItemList.RemoveAt(itemScript.mItemList.Count - 1); Object.DestroyImmediate(go.gameObject); } } float curX = padding; for (int k = 0; k < itemScript.mItemList.Count; ++k) { GameObject obj = itemScript.mItemList[k].gameObject; obj.transform.localPosition = new Vector3(curX, 0, 0); curX = curX + w0 + padding; } mLoopListView.OnItemPrefabChanged("ItemPrefab1"); }