Add Row of Column - 0일 경우 View사이즈에 딱 맞게 아이템을 생성해줍니다. 이렇게 되면 아이템 재사용이 제대로 동작하지 않으니, 1이나 2로 지정합니다.
Item Padding - 아이템 사이의 간격입니다. 알아서 다 맞춰주기 때문에 Grid 사용하지 않아도 됩니다.
Scrollbar - 스크롤바가 있을경우 지정해줍시다.
그리고 UIPanel의 offset과 Center 좌표는 0으로 두는게 좋습니다. 패널 위치를 옮기려면 게임오브젝트의 좌표를 움직이는게 좋습니다.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CListViewTest : MonoBehaviour
{
public CUIListView m_ListView;
private List m_ItemList = new List();
void Start ()
{
for(int i = 0; i < 500; ++i)
{
m_ItemList.Add(i);
}
m_ListView.OnUpdateItem = OnUpdateItem;
m_ListView.OnGetItemCount = OnGetItemCount;
m_ListView.Init();
}
public void OnUpdateItem(int index, GameObject go)
{
CListViewTestItem item = go.GetComponent<CListViewTestItem>();
item.m_Label.text = index.ToString();
}
public int OnGetItemCount()
{
return m_ItemList.Count;
}
}
public class CListViewTestItem : MonoBehaviour
{
public UILabel m_Label;
}
코드는 위와같이 작성합니다.
OnUpdateItem - 아이템이 갱신될 때 호출됩니다. 나머지 부분은 직접 구현하시면 됩니다.
OnGetItemCount - 리스트의 전체 개수가 필요할 때 호출됩니다.
추가로 Refresh와 Reset, SetFocus 함수가 있는데 필요에 따라서 호출하여 쓰시면 될 것 같습니다.
미구현 기능 및 버그
- 스크롤 바를 클릭해서 뷰를 이동하는 기능. 이 기능을 쓸일이 없고 작업하기도 힘들어서 구현하지 않았습니다.
- 아이템이 한페이지 이상 삭제되었을 경우에 Refresh를 호출하면 뷰 이동이 안되는 버그. (이런 경우에는 Reset을 쓰는 걸 추천).
2015. 4. 15 업데이트
아이템이 리스트에 딱 맞게 설정되어 있을 경우에 스크롤이 되는 버그가 있어서 수정하였습니다.
2015. 5. 26 업데이트
아이템의 크기가 뷰사이즈 보다 작을 경우 드래그가 불가능 하도록 스크롤뷰를 disable 시키는 기능 추가.
SetFocus(0); 을 호출 할 경우 0번째 아이템이 화면 중앙에 위치하는 버그 수정.
"Infinite Scrolling" for Unity3D Using NGUI's UIScrollView (1st attempt)
For latest update please check this post instead I've been using NGUI for UI related work on my Unity project for the past few weeks and it is a breath of fresh air when compared to the stock UnityGUI (OnGUI()).
Part of my project relate to display a relatively large amount of data dynamically (well not very large but in thousands) and instantiating a prefab for each data element didn't seem like a good idea to me :-)
So instead I decided to add some logic to do a scroll view with fixed pool of items that I reuse and position according to the direction of the scroll and get it fed with the correct data.
Although I am sure that there are existing solutions I decided to do my own.
The logic so far is built using UIGrid with fixed cell height for the moment (not well suited for UITable with different cell height) and the panel is using soft clip. the Scroll view is using momentum only (Spring physics breaks my current logic for some reason)
Initialization steps:
Pool size is calculated using the cell height against the panel height plus some buffer (currently I am using a buffer of 4 list items)
List items' pool is instantiated with corresponding data from the data list
A map is maintained to keep track of which data item are used in which list items
While Scrolling:
Any item that turns a visible from an invisible state will notify the main controller
We check the direction of the scroll based on which item is visible (e.g. if the next item is visible means that we are dragging upwards)
based on the direction we reuse the items at the top or the bottom of the list accordingly (e.g. if direction is up the top item moves to the bottom and get populated with data from the corresponding data element)
This is a first attempt and further posts will follow as the logic evolve. ==UPDATE== I am in the process of making this component available as open source. Meanwhile we've launched a free app on Android that uses it called Avatar Messenger ==UPDATE 2== The component is available as open source https://github.com/OrangeLabsUK/InfiniteList_NGUI