private void AddItemEvent(GameObject itemTemplate) { if (itemTemplate == null) { return; } int totalCount = 0; if (m_Multi) { Vector2 itemSize = (itemTemplate.transform as RectTransform).sizeDelta; totalCount = m_Axis == ScrollAxis.Horizontal ? m_HCount + 4 : m_VCount + 4; int childCount = m_Axis == ScrollAxis.Horizontal ? m_VCount : m_HCount; for (int i = 0; i < totalCount; i++) { MultiItemContainer container = new MultiItemContainer(this.ContainerList[i], i, m_ItemRefreshEvent); for (int j = 0; j < childCount; j++) { GameObject child = this.ItemList[i * childCount + j]; AdjustItemLayout(child, new Vector2() { x = m_Axis == ScrollAxis.Horizontal ? 0 : j * itemSize.x + j * m_Spacing.x, y = m_Axis == ScrollAxis.Horizontal ? (j * itemSize.y + j * m_Spacing.y) * -1 : 0 }); m_ItemInitEvent?.Invoke(i * childCount + j, child); container.AddItem(child); } m_Items.Add(container); } } else { totalCount = m_Axis == ScrollAxis.Horizontal ? (m_HCount + 4) * m_VCount : (m_VCount + 4) * m_HCount; for (int i = 0; i < totalCount; i++) { GameObject item = this.ItemList[i]; AdjustItemLayout(item, Vector2.zero); m_Items.Add(new SingleItemContainer(item, i, m_ItemRefreshEvent)); m_ItemInitEvent?.Invoke(i, item); } } }
IEnumerator AsynCreateAndRefresh(GameObject itemTemplate) { CleanAllItem(); if (itemTemplate == null) { yield break; } int totalCount = 0; if (m_Multi) { Vector2 itemSize = (itemTemplate.transform as RectTransform).sizeDelta; totalCount = m_Axis == ScrollAxis.Horizontal ? m_HCount + 4 : m_VCount + 4; int childCount = m_Axis == ScrollAxis.Horizontal ? m_VCount : m_HCount; //StartCoroutine(); for (int i = 0; i < totalCount; i++) { GameObject item = new GameObject("Container"); RectTransform rectTransform = item.AddComponent <RectTransform>(); rectTransform.SetParent(m_Content); rectTransform.localScale = Vector3.one; rectTransform.localRotation = Quaternion.identity; AdjustItemLayout(item, Vector2.zero); rectTransform.sizeDelta = new Vector2() { x = m_Axis == ScrollAxis.Horizontal ? itemSize.x : childCount * itemSize.x + (childCount - 1) * m_Spacing.x, y = m_Axis == ScrollAxis.Horizontal ? childCount * itemSize.y + (childCount - 1) * m_Spacing.y : itemSize.y }; MultiItemContainer container = new MultiItemContainer(item, i, m_ItemRefreshEvent); for (int j = 0; j < childCount; j++) { GameObject child = Instantiate(m_ItemTemplate, item.transform); AdjustItemLayout(child, new Vector2() { x = m_Axis == ScrollAxis.Horizontal ? 0 : j * itemSize.x + j * m_Spacing.x, y = m_Axis == ScrollAxis.Horizontal ? (j * itemSize.y + j * m_Spacing.y) * -1 : 0 }); m_ItemInitEvent(i * childCount + j, child); container.AddItem(child); yield return(0.033f); } m_Items.Add(container); } } else { totalCount = m_Axis == ScrollAxis.Horizontal ? (m_HCount + 4) * m_VCount : (m_VCount + 4) * m_HCount; for (int i = 0; i < totalCount; i++) { GameObject item = Instantiate(m_ItemTemplate, m_Content); AdjustItemLayout(item, Vector2.zero); m_Items.Add(new SingleItemContainer(item, i, m_ItemRefreshEvent)); m_ItemInitEvent?.Invoke(i, item); yield return(0.033f); } } AdjustContentAnchor(); if (m_ItemLoadBackEvent != null) { m_ItemLoadBackEvent(); } isLoad = true; }
private void BulidItem(GameObject itemTemplate) { if (itemTemplate == null) { return; } int totalCount = 0; if (m_Multi) { Vector2 itemSize = (itemTemplate.transform as RectTransform).sizeDelta; totalCount = m_Axis == ScrollAxis.Horizontal ? m_HCount + 4 : m_VCount + 4; int childCount = m_Axis == ScrollAxis.Horizontal ? m_VCount : m_HCount; for (int i = 0; i < totalCount; i++) { GameObject item = new GameObject("Container"); RectTransform rectTransform = item.AddComponent <RectTransform>(); rectTransform.SetParent(m_Content); rectTransform.localScale = Vector3.one; rectTransform.localRotation = Quaternion.identity; AdjustItemLayout(item, Vector2.zero); rectTransform.sizeDelta = new Vector2() { x = m_Axis == ScrollAxis.Horizontal ? itemSize.x : childCount * itemSize.x + (childCount - 1) * m_Spacing.x, y = m_Axis == ScrollAxis.Horizontal ? childCount * itemSize.y + (childCount - 1) * m_Spacing.y : itemSize.y }; MultiItemContainer container = new MultiItemContainer(item, i, m_ItemRefreshEvent); for (int j = 0; j < childCount; j++) { GameObject child = Instantiate(m_ItemTemplate, item.transform); AdjustItemLayout(child, new Vector2() { x = m_Axis == ScrollAxis.Horizontal ? 0 : j * itemSize.x + j * m_Spacing.x, y = m_Axis == ScrollAxis.Horizontal ? (j * itemSize.y + j * m_Spacing.y) * -1 : 0 }); if (m_ItemInitEvent != null) { m_ItemInitEvent.Invoke(i * childCount + j, child); } container.AddItem(child); } m_Items.Add(container); } } else { totalCount = m_Axis == ScrollAxis.Horizontal ? (m_HCount + 4) * m_VCount : (m_VCount + 4) * m_HCount; for (int i = 0; i < totalCount; i++) { GameObject item = Instantiate(m_ItemTemplate, m_Content); AdjustItemLayout(item, Vector2.zero); m_Items.Add(new SingleItemContainer(item, i, m_ItemRefreshEvent)); if (m_ItemInitEvent != null) { m_ItemInitEvent.Invoke(i, item); } } } }