# Unity realizes unlimited list function

Time：2020-12-2

In this paper, we share the specific code of unity to realize the infinite 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 a.WorldRect().Overlaps(b.WorldRect());
}
public static bool Overlaps(this RectTransform a, RectTransform b, bool allowInverse)
{
return a.WorldRect().Overlaps(b.WorldRect(), allowInverse);
}

public static Rect WorldRect(this RectTransform rectTransform)
{
Vector2 sizeDelta = rectTransform.sizeDelta;
float rectTransformWidth = sizeDelta.x * rectTransform.lossyScale.x;
float rectTransformHeight = sizeDelta.y * rectTransform.lossyScale.y;

Vector3 position = rectTransform.position;

return new Rect(
position.x - rectTransformWidth * rectTransform.pivot.x,
position.y - rectTransformHeight * rectTransform.pivot.y,
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 = rectTransform.sizeDelta;
float rectTransformWidth = sizeDelta.x * rectTransform.lossyScale.x;
float rectTransformHeight = sizeDelta.y * rectTransform.lossyScale.y;

Vector3 position = pos;

rect.x = position.x - rectTransformWidth * rectTransform.pivot.x;
rect.y = position.y - rectTransformHeight * rectTransform.pivot.y;
rect.width = rectTransformWidth;
rect.height = rectTransformHeight;

return rect;
}
}``````

The above extension method is to judge whether two rectransform objects intersect.

Then, the UI position is updated in the sliding callback method of scrollrec, as shown in the following code

``````private void OnScrollRectValueChanged(Vector2 arg0)
{
Dictionary<int, DynamicRect> inOverlaps = new Dictionary<int, DynamicRect>();

//m_ Dynamicrectdic is a dictionary that stores all the location data you need to place in your UI,
//Determine which UI is visible and which is invisible, and save it
foreach (DynamicRect dR in m_DynamicRectDic.Values)
{
tmpTra.localPosition = dR.localPos;
//Get the world coordinates rect of UI for each location
Rect rect = m_LevelItemPrefabRT.WorldRect2(tmpTra.position);
{
}
}
//m_ Levelitemlist is to save the UI list after instantiation. For example, your mask page can display 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 to update the display and data storage of UI interface
LevelItem item = m_LevelItemList[i];
if (item.DRect != null && !inOverlaps.ContainsKey(item.DRect.Index))
{
//When the drect of item is null, the object is hidden, otherwise the object is displayed
item.DRect = null;
}
}
//Determine which UI can be reused, and then assign new data and location
foreach (DynamicRect dR in inOverlaps.Values)
{
if (GetDynmicItem(dR) == null)
{
LevelItem item = GetNullDynmicItem();

if (item == null) continue;

item.DRect = dR;
//Update the location and display of UI (calculated by yourself, each display is different)
_UpdateChildTransformPos(item.gameObject, dR.Index);
}
}
}

/// <summary>
///Obtaining dynamic renderers through dynamic lattices
/// </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 (item.DRect == null)
continue;
if (rect.Index == item.DRect.Index)
return item;
}
return null;
}
/// <summary>
///Get the render to render
/// </summary>
/// <returns></returns>
private LevelItem GetNullDynmicItem()
{
int len = m_LevelItemList.Count;
for (int i = 0; i < len; ++i)
{
LevelItem item = m_LevelItemList[i];
if (item.DRect == null)
return item;
}

return null;
}

public class DynamicRect
{
/// <summary>
///Local coordinates
/// </summary>
public Vector3 localPos;
/// <summary>
///Lattice index
/// </summary>
public int Index;

public DynamicRect(int index, Vector3 localPos)
{
this.Index = index;
this.localPos = localPos;
}
}``````