/// <summary> /// 重新生成Grid /// </summary> public void RefreshGrid() { if (mDataProviders == null) { throw new Exception("dataProviders 为空!请先使用SetDataProvider "); } //获得蒙版尺寸 mMaskSize = mScrollRect.GetComponent <RectTransform>().sizeDelta; //通过蒙版尺寸和格子尺寸计算需要的渲染器个数 mRendererCount = ColumnCount * (Mathf.CeilToInt(mMaskSize.y / GetBlockSizeY()) + 1); CreatRects(mDataProviders.Count); mList_items.ForEach(x => Destroy(x.gameObject)); mList_items.Clear(); for (int i = 0; i < mRendererCount; ++i) { GameObject child = Instantiate(RenderGO); child.transform.SetParent(transform); child.transform.localRotation = Quaternion.identity; child.transform.localScale = Vector3.one; child.layer = gameObject.layer; (child.transform as RectTransform).sizeDelta = CellSize * CellSizeScale; YuoLoopListItem <T> dfItem = child.GetComponent <YuoLoopListItem <T> >(); if (dfItem == null) { throw new Exception("Render must extend DynamicInfinityItem"); } mList_items.Add(dfItem); mList_items[i].DRect = mDict_dRect[i]; //child.SetActive(false); UpdateChildTransformPos(dfItem, i); } SetListRenderSize(mDataProviders.Count); }
public void SetItemAction(YuoLoopListItem <T> .OnSelect OnSelect, YuoLoopListItem <T> .OnUpdateData OnUpdate) { foreach (var item in mList_items) { item.OnSelectHandler = OnSelect; item.OnUpdateDataHandler = OnUpdate; } }
/// <summary> /// 清理可复用渲染格 /// 不需要public /// </summary> private void ClearAllListRenderDr() { if (mList_items != null) { int len = mList_items.Count; for (int i = 0; i < len; ++i) { YuoLoopListItem <T> item = mList_items[i]; item.DRect = null; } } }
/// <summary> /// 获得待渲染的渲染器 /// </summary> /// <returns></returns> private YuoLoopListItem <T> GetWaitItem() { int len = mList_items.Count; for (int i = 0; i < len; ++i) { YuoLoopListItem <T> item = mList_items[i]; if (item.DRect == null) { return(item); } } throw new Exception("Error"); }
/// <summary> /// 更新各个渲染格子的位置 /// </summary> /// <param name="child"></param> /// <param name="index"></param> private void UpdateChildTransformPos(YuoLoopListItem <T> child, int index) { int row = index / ColumnCount; int column = index % ColumnCount; UpdateChildTransformPosTemp = Vector2.zero; //UpdateChildTransformPosTemp.x = column * GetBlockSizeX() + CellSize.x * CellSizeScale.x / 2; //UpdateChildTransformPosTemp.y = mDict_dRect[index].mRect.y; UpdateChildTransformPosTemp = child.DRect.mRect.position; UpdateChildTransformPosTemp.x = column * GetBlockSizeX() + CellSize.x * CellSizeScale.x / 2; UpdateChildTransformPosTemp.y -= nowStartPos; //UpdateChildTransformPosTemp.y = -CellSize.y * CellSizeScale.y - row * GetBlockSizeY(); (child.transform as RectTransform).anchoredPosition3D = UpdateChildTransformPosTemp; }
protected void UpdateRender() { mRectMask.y = -mMaskSize.y - mRectTransformContainer.anchoredPosition.y + nowStartPos; inOverlaps.Clear(); int Count = mDict_dRect.Count; Count.Clamp(mDataProviders.Count / ColumnCount.RClamp(1, ColumnCount)); int min = (int)(Count * (1 - mScrollRect.verticalNormalizedPosition) - ColumnCount * mRectMask.height / CellSize.y / CellSizeScale.y * 2); int max = (int)(Count * (1 - mScrollRect.verticalNormalizedPosition) + ColumnCount * mRectMask.height / CellSize.y / CellSizeScale.y * 2); min += MinStart; max += MinStart; //print("min:" + min + " max:" + max + "minStart:" + MinStart + " mDict_dRect.Count:" + Count); min.Clamp(MinStart, Count + MinStart - 1); max.Clamp(MinStart, Count + MinStart - 1); for (int i = min; i <= max; i++) { if (mDict_dRect[i].Overlaps(mRectMask)) { inOverlaps.Add(mDict_dRect[i].Index, mDict_dRect[i]); } } int len = mList_items.Count; for (int i = 0; i < len; ++i) { YuoLoopListItem <T> item = mList_items[i]; if (item.DRect != null && !inOverlaps.ContainsKey(item.DRect.Index)) { item.DRect = null; } } foreach (YuoLoopListRect dR in inOverlaps.Values) { if (GetDynmicItem(dR) == null) { YuoLoopListItem <T> item = GetWaitItem(); item.DRect = dR; UpdateChildTransformPos(item, dR.Index); if (mDataProviders != null && dR.Index < mDataProviders.Count) { item.SetData(mDataProviders[dR.Index]); } } } inOverlaps.Clear(); }
/// <summary> /// 通过动态格子获得动态渲染器 /// </summary> /// <param name="rect"></param> /// <returns></returns> private YuoLoopListItem <T> GetDynmicItem(YuoLoopListRect rect) { int len = mList_items.Count; for (int i = 0; i < len; ++i) { YuoLoopListItem <T> item = mList_items[i]; if (item.DRect == null) { continue; } if (rect.Index == item.DRect.Index) { return(item); } } return(null); }
/// <summary> /// 初始化渲染脚本 /// </summary> public virtual void InitRendererList(YuoLoopListItem <T> .OnSelect OnSelect = null, YuoLoopListItem <T> .OnUpdateData OnUpdate = null) { if (mHasInited) { return; } //获得蒙版尺寸 mMaskSize = mScrollRect.GetComponent <RectTransform>().sizeDelta; //通过蒙版尺寸和格子尺寸计算需要的渲染器个数 mRendererCount = ColumnCount * (Mathf.CeilToInt(mMaskSize.y / GetBlockSizeY()) + 1); //CreatRects(mRendererCount); mList_items = new List <YuoLoopListItem <T> >(); for (int i = 0; i < mRendererCount; ++i) { GameObject child = GameObject.Instantiate(RenderGO); child.name = $"{RenderGO.name}_{i}"; child.transform.SetParent(transform); child.transform.localRotation = Quaternion.identity; child.transform.localScale = Vector3.one; child.layer = gameObject.layer; (child.transform as RectTransform).sizeDelta = CellSize * CellSizeScale; YuoLoopListItem <T> dfItem = child.GetComponent <YuoLoopListItem <T> >(); if (dfItem == null) { throw new Exception("Render must extend DynamicInfinityItem"); } mList_items.Add(dfItem); mList_items[i].DRect = new YuoLoopListRect(1 * GetBlockSizeX(), -i * GetBlockSizeY() - CellSize.y * CellSizeScale.y, CellSize.x * CellSizeScale.x, CellSize.y * CellSizeScale.y, i); mList_items[i].DRect.Index = -1; mList_items[i].OnSelectHandler = OnSelect; mList_items[i].OnUpdateDataHandler = OnUpdate; child.SetActive(false); UpdateChildTransformPos(dfItem, i); } SetListRenderSize(mRendererCount.Clamp(mDataProviders.Count)); mHasInited = true; }