This example shares the specific code for unity to implement the unlimited list function for your reference. The specific content is as follows
public static class RectTransformExtensions { public static bool Overlaps(this RectTransform a, RectTransform b) { return ().Overlaps(()); } public static bool Overlaps(this RectTransform a, RectTransform b, bool allowInverse) { return ().Overlaps((), allowInverse); } public static Rect WorldRect(this RectTransform rectTransform) { Vector2 sizeDelta = ; float rectTransformWidth = * ; float rectTransformHeight = * ; Vector3 position = ; return new Rect( - rectTransformWidth * , - rectTransformHeight * , rectTransformWidth, rectTransformHeight); } /// <summary> /// /// </summary> /// <param name="rectTransform"></param> /// <param name="pos">position of world coordinates</param> /// <returns></returns> public static Rect WorldRect2(this RectTransform rectTransform, Vector3 pos) { Rect rect = new Rect(); Vector2 sizeDelta = ; float rectTransformWidth = * ; float rectTransformHeight = * ; Vector3 position = pos; = - rectTransformWidth * ; = - rectTransformHeight * ; = rectTransformWidth; = rectTransformHeight; return rect; } }
The above expansion method is to determine whether two Recttransform type objects intersect.
Then update the UI position in the ScrollRec slide callback method, the code is as follows
private void OnScrollRectValueChanged(Vector2 arg0) { Dictionary<int, DynamicRect> inOverlaps = new Dictionary<int, DynamicRect>(); mRectMask = Masked object(); //m_DynamicRectDic dictionary saves the location data that all your UI needs to place. //Judge which is visible and which is not visible in all UIs, save it foreach (DynamicRect dR in m_DynamicRectDic.Values) { = ; //Get the world coordinates of each position UI Rect Rect rect = m_LevelItemPrefabRT.WorldRect2(); if ((mRectMask)) { (, dR); } } //m_LevelItemList is to save the UI list after you instantiate. For example, your mask page displays up to 3 UIs. You need to instantiate 4 UIs, and then dynamically modify the display and hiding of gameobject. int len = m_LevelItemList.Count; for (int i = 0; i < len; ++i) { //LevelItem is a script mounted on the UI, used to update the display and data storage of the UI interface. LevelItem item = m_LevelItemList[i]; if ( != null && !()) { // When the DRect of the item is null, hide the object, otherwise the object will be displayed = null; } } //Judge which UIs can be reused, and then give new data and location foreach (DynamicRect dR in ) { if (GetDynmicItem(dR) == null) { LevelItem item = GetNullDynmicItem(); if (item == null) continue; = dR; //Update the location and display of the UI (calculate it yourself, each display is different) _UpdateChildTransformPos(, ); } } } /// <summary> /// Obtain a dynamic renderer through dynamic grid /// </summary> /// <param name="rect"></param> /// <returns></returns> private LevelItem GetDynmicItem(DynamicRect rect) { int len = m_LevelItemList.Count; for (int i = 0; i < len; ++i) { LevelItem item = m_LevelItemList[i]; if ( == null) continue; if ( == ) return item; } return null; } /// <summary> /// Get the renderer to be rendered /// </summary> /// <returns></returns> private LevelItem GetNullDynmicItem() { int len = m_LevelItemList.Count; for (int i = 0; i < len; ++i) { LevelItem item = m_LevelItemList[i]; if ( == null) return item; } return null; } public class DynamicRect { /// <summary> /// Local coordinates /// </summary> public Vector3 localPos; /// <summary> /// Grid index /// </summary> public int Index; public DynamicRect(int index, Vector3 localPos) { = index; = localPos; } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.