Article directory
I. Introduction
Hi everyone, I'm new.
A small friend asked Unity
how to implement an irregular circular reuse list in the question area.
I found GitHub
that someone has already implemented a version today, and the effect is good. The
GitHub
address: https://github.com/aillieo/UnityDynamicScrollView
But this version has one BUG
, I Made a fix, as explained in the third part of this article.
Good wheels are used by everyone. Today I will introduce how to use this version. The
final effect of this article is as follows
2. How to use
1. Create Scroll View
UI
Create one on the node , Scroll View
as
follows
Remove Scroll View
the component of the node Scroll Rect
, as follows
Hang the ScrollView
component or ScrollViewEx
component in the code, I take the ScrollViewEx
component as an example,
Note: It
ScrollViewEx
inheritsScrollView
all the functions and has carried out targeted optimization. It willitem
perform pagination. Setting the appropriate page size can get better performance.
After hanging the component, the effect is as follows,
2. Set Scroll View parameters
2.1. Adjust the width and height
Let's adjust Scroll View
the width and height first.
2.2, delete the Scrollbar slider
We see that Scroll View
there are two Scrollbar
sliders, we don't want it, we
can directly delete the Scrollbar Horizontal
sum under the child node. After Scrollbar Vertical
deleting,
we can see that there was space left Viewport
before , but now there is no space , we need to adjust it to fill the whole , we Select and change both to , as followsScrollbar
Scrollbar
Viewport
Scroll View
Viewport
Right
Bottom
0
2.3. Set item template: Item Template
To display one by one in the list, you item
must first make a item
template. We Scroll View
create one under the child node Image
, rename it as item
follows,
adjust item
the width and height
as follows,
then select the Scroll View
node, set Item Template
it as just now item
, and fill Default Item Size
in item
the width and height as ,as follows
2.4. Set the object pool size: Pool Size
In order to prevent item
the repeated creation and destruction of the list, the object pool is used here, and we need to set the size of the object pool Pool Size
. If you keep stuffing objects into the object pool (just don’t take them out), after the object pool is full, you won’t continue to stuff objects into the object pool. We need to set a reasonable size of the object pool. It is recommended to be item
the largest size that can be displayed in the visible area of the list. Quantity 2倍
, here I estimate that the visible area 10
of the list will be displayed at most item
, then my object pool size is set to 20
,
2.5, set the list arrangement direction
The list provides a 4
kind of arrangement direction. You can see what it means when you look at the options. Here I take it Vertical
as an example.
2.6. Set the page size: Page Size
If you are using a ScrollView
component, there is no Page Size
such setting, only the ScrollViewEx
component has this setting.
Why set up pagination? ScrollView
A copy is maintained in List<ScrollItemWithRect>
, which is used to store item
the coordinates and sizes.
Suppose your list has a huge amount of item
data. You now have to insert a new one into the middle item
. At this time, you need to recalculate the huge amount item
of coordinates and sizes, which is very expensive. Performance, the solution is to set up paging, and only maintain one page at a time item
, which greatly improves performance.
It is recommended to set it to more than item
the maximum number that can be displayed in the visible area of the list 2倍
. Here it is set to 30
,
2.7. Other general settings
Set Content
and Viewport
, as follows
Check Horizontal
or according to the sliding direction Vertical
, here I only need to slide vertically, so only check Vertical
,
3. Add some elements to the item
Above our item
bare, add some elements to it, and then fine-tune the overall interface, as follows The
node structure is as follows
4. Write test code
Create a MyTest.cs
script as follows,
Note:
TestScript.cs
andTestLargeAmount.cs
are the test scripts provided by the original author.
First define a data structure
// MyTest.cs
public struct RankItemData
{
// 名次
public int rank;
// 名字
public string name;
}
declare an List
object to store the list data,
// MyTest.cs
List<RankItemData> testData = new List<RankItemData>();
Now let's write a method to construct some test data,
// MyTest.cs
private void Start()
{
// 构造测试数据
InitData();
}
private void InitData()
{
// 构建50000个排名数据
for (int i = 1; i <= 50000; ++i)
{
RankItemData data = new RankItemData();
data.rank = i;
data.name = "Name_" + i;
testData.Add(data);
}
}
Declare ScrollView
the member object and ScrollView
set the callback function, as follows
// MyTest.cs
using System.Collections.Generic;
using UnityEngine;
using AillieoUtils;
public class MyTest : MonoBehaviour
{
...
public ScrollView scrollView;
private void Start()
{
...
scrollView.SetUpdateFunc((index, rect) =>
{
// TODO 更新item的UI元素
});
scrollView.SetItemSizeFunc((index) =>
{
// TODO 返回item的尺寸
return Vector2.one;
});
scrollView.SetItemCountFunc(() =>
{
// TODO 返回数据列表item的总数
return 0;
});
}
}
Next, we implement the specific content of the callback one by one.
updated element item
,UI
// MyTest.cs
scrollView.SetUpdateFunc((index, rectTransform) =>
{
// 更新item的UI元素
RankItemData data = testData[index];
rectTransform.gameObject.SetActive(true);
rectTransform.Find("rankText").GetComponent<Text>().text = data.rank.ToString();
rectTransform.Find("nameText").GetComponent<Text>().text = data.name;
Button btn = rectTransform.Find("Button").GetComponent<Button>();
btn.onClick.RemoveAllListeners();
btn.onClick.AddListener(()=>{
Debug.Log(data.name);
});
});
The size of the return item
, I want the item
height of the top three to be higher, return Vector2(812, 180)
, the other return Vector2(812, 100)
,
// MyTest.cs
scrollView.SetItemSizeFunc((index) =>
{
// 返回item的尺寸
RankItemData data = testData[index];
if(data.rank <= 3)
{
return new Vector2(812, 180);
}
else
{
return new Vector2(812, 100);
}
});
returns item
the total number of data lists,
// MyTest.cs
scrollView.SetItemCountFunc(() =>
{
// 返回数据列表item的总数
return testData.Count;
});
5. Run the test
We MyTest.cs
hang the script Canvas
on it and assign Scroll View
members as follows,
then we item
hide it and
run it, the effect is as follows,
um, there is less space between item
and item
, let's transform it.
6, item directly increases the interval
item
Create one Image
and rename it to bg
, set Anchors
to stretch-strech
, set Top
and Bottom
bit 2
, so that there will be 2
a gap of units up and down,
we delete our item
own Image
components and re-run, the effect is as follows, to achieve our effect,
we can see, Although we have one list data 50000
, UI
only a few item
are being reused in cycles, which is especially suitable for the display of lists with many data items.
3. BUG repair
During the test, I found one BUG
. When using ScrollViewEx.cs
it, when turning pages, it will trigger frantically at the border, OnValueChanged
resulting in fast page turning.
I am Demo
making a fix and adding a damping to avoid crazy page turning.
4. Demo source code
Demo
I have uploaded the source code of this article GitCode
, and interested students can download and study by themselves.
Address: https://codechina.csdn.net/linxinfa/UnityDynamicScrollView
5. Finished
Well, let's write it here.
I'm new, https://blog.csdn.net/linxinfa , a developer who works
silently in a small company , I hope I can help more people who want to learn, and encourage each other~Unity
Unity