private void InitTemplate() { for (int i = 0; i < m_CacheCount; i++) { ItemModelView itemModelView = InstantiateItem(i); itemModelView.SetContext(i, Collection.GetBaseItem(i)); } }
protected virtual ItemModelView InstantiateItem(int index) { GameObject itemObject = Instantiate(Template, transform); Transform itemTransform = itemObject.transform; itemTransform.localScale = Vector3.one; itemTransform.localPosition = Vector2.one * 5000; itemTransform.transform.SetSiblingIndex(index); ItemModelView modelView = itemObject.GetOrAddComponent <ItemModelView>(); modelView.TemplateName = Template.name; return(modelView); }
protected override void OnItemInsert(int index, ItemContext itemContext) { if (Template == null) { Log.Error("Template is invalid"); return; } ItemModelView itemModelView = InstantiateItem(index); m_ItemModelViews.Insert(index, itemModelView); UpdateContext(index); }
private ItemModelView InstantiateItem(int index) { GameObject itemObject = Instantiate(Template, transform); RectTransform rectTransform = itemObject.GetComponent <RectTransform>(); rectTransform.localScale = Vector3.one; rectTransform.anchorMax = rectTransform.anchorMin = Vector2.one / 2f; rectTransform.sizeDelta = m_ItemSize; SetItemPosition(rectTransform, index); rectTransform.transform.SetSiblingIndex(index); ItemModelView modelView = itemObject.GetOrAddComponent <ItemModelView>(); modelView.TemplateName = Template.name; m_ItemModelViews.AddLast(modelView); return(modelView); }
/// <summary> /// 更新Item的位置 /// </summary> /// <param name="itemModelView">ItemModelView</param> /// <returns>当水平滑动时,左边界返回1,右边界返回-1,未更新返回0,当垂直滑动时,上边界返回-1,下边界返回1,未更新返回0</returns> private int UpdatePosition(ItemModelView itemModelView) { Transform itemTrans = itemModelView.transform; float viewPos = m_Content.InverseTransformPoint(m_Viewport.position)[(int)m_Axis]; float itemPos = m_Content.InverseTransformPoint(itemTrans.position)[(int)m_Axis]; float distance = itemPos - viewPos; if (distance > m_Extents || distance < -m_Extents) { int sign = distance > m_Extents ? -1 : 1; float moveExtents = m_Extents - m_ItemSize[(int)m_Axis] * 0.5f; int index = itemModelView.Index; ItemContext itemContext = null; if (m_Axis == RectTransform.Axis.Horizontal) { index += sign * m_CacheCount; } else { index -= sign * m_CacheCount; } itemContext = Collection.GetBaseItem(index); if (itemContext == null) { return(0); } float value = moveExtents * 2 + m_Spacing; itemTrans.localPosition += sign * GetAxisVector3(value); itemModelView.ClearAllCache(); itemModelView.SetContext(index, itemContext); //Log.Debug("{0} : {1}", m_Axis, sign); return(sign); } return(0); }