/// <summary> /// 更新Item位置 /// </summary> protected override void UpdateItemPosition(LoopItem item, int index) { Vector3 pos = item.Transform.localPosition; pos.y = index * ItemLength; item.Transform.localPosition = pos; }
/// <summary> /// 生成Items /// </summary> protected void InstantiateItems(int count, Transform parent, bool isVertical) { if (string.IsNullOrEmpty(PrefabPath)) { return; } GameObject prefab = Resources.Load(PrefabPath) as GameObject; if (prefab == null) { return; } Vector3 hidePos = ScrollView.movement == UIScrollView.Movement.Vertical ? new Vector3(0, 5000, 0) : new Vector3(5000, 0, 0); for (int x = 0, length = count; x < length; x++) { GameObject go = Instantiate(prefab); go.transform.parent = parent; go.transform.localPosition = hidePos; go.transform.localScale = Vector3.one; LoopItem item = go.GetComponent <LoopItem>(); go.SetActive(false); if (item == null) { continue; } Items.Add(item); } }
public GameObject GetChildItem() { //查找有没有被回收的子节点 for (int i = 0; i < content.childCount; i++) { if (!content.GetChild(i).gameObject.activeSelf) { content.GetChild(i).gameObject.SetActive(true); return(content.GetChild(i).gameObject); } } //如果没有,创建一个 GameObject childItem = GameObject.Instantiate(childItemPrefab, content.transform); //设置数据 childItem.transform.localScale = Vector3.one; childItem.transform.localPosition = Vector3.zero; //设置锚点 childItem.GetComponent <RectTransform>().anchorMin = new Vector2(0, 1); childItem.GetComponent <RectTransform>().anchorMax = new Vector2(0, 1); //设置宽高 childItem.GetComponent <RectTransform>().sizeDelta = contentLayoutGroup.cellSize; LoopItem loopItem = childItem.AddComponent <LoopItem>(); loopItem.onAddHead += this.OnAddHead; loopItem.onRemoveHead += this.onRemoveHead; loopItem.onAddLast += this.onAddLast; loopItem.onRemoneLast += this.onRemoveLast; return(childItem); }
/// <summary> /// 更新Item資料與位置 /// </summary> protected virtual void UpdateItem(LoopItem item, int index) { item.IndexProperty = index; bool isNull = DataList == null || index < 0 || index >= DataList.Count; item.SetData(isNull ? null : DataList[index]); item.SetActive(!isNull); UpdateItemPosition(item, index); }
/// <summary> /// 將Item移至尾端 /// </summary> protected virtual void MoveItemToEnd(int needChangeID, int movement) { for (int x = 0, count = Items.Count; x < count; x++) { LoopItem item = Items[x]; if (item.IndexProperty > needChangeID) { continue; } UpdateItem(item, item.IndexProperty + movement); } }
/// <summary> /// 更新Item位置 /// </summary> protected abstract void UpdateItemPosition(LoopItem item, int index);