using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System;
public enum Layout
{
Vertical,
Horizontal
}
/// <summary>
/// 无限Item列表
/// </summary>
public class UIDiffSizeListView : MonoBehaviour
{
public Layout layout;
public Vector2 spacing;
public GameObject itemPrefab;
private UIItemRender itemRender = null;
private UIPanel mPanel;
public UIScrollView mScrollView;
private bool initialized = false;
private GameObject content;
private Vector2 scrollRectSize;
private GameObject pressObj;
private bool isDraging = false;
private bool isInvalid;
public List<object> dataList = new List<object>();
private Dictionary<int, UIItemRender> dirCurItems = new Dictionary<int, UIItemRender>();
private Dictionary<int, UIItemRender> tmpDic2 = new Dictionary<int, UIItemRender>();
private Dictionary<int, Vector2> itemSizes = new Dictionary<int, Vector2>();
private Queue<UIItemRender> unUseItems = new Queue<UIItemRender>();
void Awake()
{
this.Init();
}
public void AddData(object obj)
{
this.dataList.Add(obj);
this.Invalidate();
}
public void UpdateDataAt(object obj, int inx)
{
if (inx >= 0 && inx <= this.dataList.Count)
{
this.dataList[inx] = obj;
this.itemSizes.Remove(inx);
this.Invalidate();
}
}
public void InsertData(object obj, int inx)
{
this.dataList.Insert(inx, obj);
Dictionary<int, Vector2> tmpDic = new Dictionary<int, Vector2>();
for(int i = 0; i < this.dataList.Count; ++i)
{
if (i < inx)
{
if(this.itemSizes.ContainsKey(i))
{
tmpDic[i] = this.itemSizes[i];
}
}
else if (i > inx)
{
if(this.itemSizes.ContainsKey(i - 1))
{
tmpDic[i] = this.itemSizes[i - 1];
}
}
}
this.itemSizes = null;
this.itemSizes = tmpDic;
this.Invalidate();
}
public void RemoveDataAt(int inx)
{
if (inx >= 0 && inx <= this.dataList.Count)
{
this.dataList.RemoveAt(inx);
Dictionary<int, Vector2> tmpDic = new Dictionary<int, Vector2>();
for(int i = 0; i < this.dataList.Count; ++i)
{
if (i < inx)
{
if(this.itemSizes.ContainsKey(i))
{
tmpDic[i] = this.itemSizes[i];
}
}
else if (i > inx)
{
if(this.itemSizes.ContainsKey(i + 1))
{
tmpDic[i] = this.itemSizes[i + 1];
}
}
}
this.itemSizes = null;
this.itemSizes = tmpDic;
this.Invalidate();
}
}
public void JumpTo(int inx)
{
if (inx >= 0 && inx <= this.dataList.Count)
{
float sizeSum = 0;
UIWidget widget = this.content.GetComponent<UIWidget>();
if (this.layout == Layout.Vertical)
{
for(int i = 0; i < inx; ++i)
{
Vector2 size = this.GetSizeByInx(i);
sizeSum = sizeSum + size.y;
}
this.mScrollView.MoveRelative(new Vector3(0,sizeSum,0));
}
else if (this.layout == Layout.Horizontal)
{
for(int i = 0; i < inx; ++i)
{
Vector2 size = this.GetSizeByInx(i);
sizeSum = sizeSum + size.x;
}
this.mScrollView.MoveRelative(new Vector3(sizeSum,0,0));
}
}
}
public void JumpToBottom()
{
UIWidget widget = this.content.GetComponent<UIWidget>();
if (this.layout == Layout.Vertical)
{
this.mScrollView.ResetPosition();
float showPos = widget.height - scrollRectSize.y;
if (showPos > 0)
this.mScrollView.MoveRelative(new Vector3(0,showPos,0));
}
else if (this.layout == Layout.Horizontal)
{
this.mScrollView.ResetPosition();
float showPos = widget.width - scrollRectSize.x;
if (showPos > 0)
this.mScrollView.MoveRelative(new Vector3(showPos,0,0));
}
}
public void JumpToTop()
{
UIWidget widget = this.content.GetComponent<UIWidget>();
if (this.layout == Layout.Vertical)
{
this.mScrollView.SetDragAmount(0,0,false);
}
else if (this.layout == Layout.Horizontal)
{
this.mScrollView.SetDragAmount(0,0,false);
}
}
public void Init()
{
if (initialized)
return;
initialized = true;
mPanel = NGUITools.FindInParents<UIPanel>(gameObject);
this.mScrollView = mPanel.GetComponent<UIScrollView>();
this.mPanel.onClipMove += OnUIPanelClipMove;
// record some sizes
scrollRectSize = mPanel.GetViewSize();
// add a scrollrect content
GameObject go = new GameObject ();
go.name = "content";
go.transform.SetParent(transform);
UIWidget uiwidget = go.AddComponent<UIWidget>();
uiwidget.pivot = UIWidget.Pivot.TopLeft;
uiwidget.SetDimensions((int)scrollRectSize.x,(int)scrollRectSize.y);
go.transform.localPosition = Vector3.zero;
go.transform.localScale = Vector3.one;
this.content = go;
//itemPrefab
GameObject item = GameObject.Instantiate(itemPrefab) as GameObject;
item.transform.SetParent(mPanel.transform.parent);
item.transform.localPosition = new Vector3(1500, 1500, 1);
itemRender = item.GetComponent<UIItemRender>();
if (itemRender == null)
itemRender = item.AddComponent<UIItemRender>();
this.mScrollView.onDragStarted += delegate ()
{
this.isDraging = true;
};
this.mScrollView.onDragFinished += delegate ()
{
this.isDraging = false;
this.pressObj = null;
};
}
// 全部清除重新计算每个的大小
public void ClearAll()
{
this.dataList.Clear();
this.itemSizes.Clear();
this.Invalidate();
}
public void ClearAllCurItems()
{
var enumerator = this.dirCurItems.GetEnumerator();
while (enumerator.MoveNext())
{
Destroy(enumerator.Current.Value.gameObject);
}
this.dirCurItems.Clear();
}
void LateUpdate()
{
UpdateNow();
}
public void UpdateNow()
{
if (this.isInvalid)
{
if (!this.gameObject.activeInHierarchy)
return;
this.ClearAllCurItems();
this.UpdateView();
this.UpdateScrollView();
this.isInvalid = false;
}
}
private void Invalidate()
{
this.isInvalid = true;
}
private void OnUIPanelClipMove(UIPanel panel)
{
this.UpdateView();
}
private void UpdateScrollView()
{
this.mScrollView.InvalidateBounds();
this.mScrollView.RestrictWithinBounds(true);
this.mScrollView.UpdateScrollbars(false);
}
private Vector2 GetSizeByInx(int inx)
{
if(!this.itemSizes.ContainsKey(inx))
{
itemRender.SetData(this.dataList[inx]);
UIWidget widget = itemRender.GetComponent<UIWidget>();
Vector2 size = Vector2.zero;
size.x = widget.width;
size.y = widget.height;
this.itemSizes.Add(inx, size);
}
return this.itemSizes[inx];
}
private Vector3 GetLocalPosByInx(int inx)
{
Vector3 pos = Vector3.zero;
for(int i = 0; i < inx; ++i)
{
Vector2 size = this.GetSizeByInx(i);
if (this.layout == Layout.Vertical)
{
pos.y = pos.y - size.y;
}
else if (this.layout == Layout.Horizontal)
{
pos.x = pos.x + size.x;
}
}
return pos;
}
private Vector2Int GetStartInxAndEndInx(float min,float max)
{
int startInx = -1;
int endInx = -1;
float AreaCellSizeCount = 0;
for(int i = 0; i < this.dataList.Count; ++i)
{
Vector2 size = this.GetSizeByInx(i);
if (this.layout == Layout.Vertical)
{
if (max > 0)
{
startInx = 0;
}
UIWidget widget = this.content.GetComponent<UIWidget>();
if (Math.Abs(min) > widget.height)
{
endInx = this.dataList.Count - 1;
}
AreaCellSizeCount = AreaCellSizeCount + size.y;
if (AreaCellSizeCount > Math.Abs(max) && startInx == -1)
{
startInx = i;
}
if (AreaCellSizeCount > Math.Abs(min) && endInx == -1)
{
endInx = i;
}
}
else if (this.layout == Layout.Horizontal)
{
AreaCellSizeCount = AreaCellSizeCount + size.x;
if (AreaCellSizeCount > Math.Abs(max) && startInx == -1)
{
startInx = i;
}
if (AreaCellSizeCount > Math.Abs(min) && endInx == -1)
{
endInx = i;
}
}
if (startInx != -1 && endInx != -1)
break;
}
return new Vector2Int(startInx,endInx);
}
private UIItemRender CreateItemRender()
{
UIItemRender item = null;
if (unUseItems.Count > 0)
{
item = unUseItems.Dequeue();
item.gameObject.SetActive(true);
return item;
}
GameObject obj = NGUITools.AddChild(content, itemPrefab);
item = obj.GetComponent<UIItemRender>();
if (item == null)
item = obj.AddComponent<UIItemRender>();
if (obj.GetComponent<UIDragScrollView>() != null)
UIEventListener.Get(obj).onPress += OnPressItem;
obj.SetActive(true);
return item;
}
private void OnPressItem(GameObject obj, bool isPress)
{
if (isPress)
{
pressObj = obj;
}
else
{
if (obj == pressObj)
pressObj = null;
}
}
private bool RecoverItemRender(UIItemRender item)
{
bool result = false;
if (item.gameObject == pressObj && isDraging)
{
pressObj = null;
result = true;
}
unUseItems.Enqueue(item);
item.SetSelected(false);
item.gameObject.SetActive(false);
return result;
}
private void UpdateSize()
{
Vector2 tempSize = Vector2.zero;
for(int i = 0; i < this.dataList.Count; ++i)
{
Vector2 size = this.GetSizeByInx(i);
if (this.layout == Layout.Vertical)
{
tempSize.x = size.x;
tempSize.y = tempSize.y + size.y;
}
else if (this.layout == Layout.Horizontal)
{
tempSize.x = tempSize.x + size.x;
tempSize.y = size.y;
}
}
UIWidget widget = this.content.GetComponent<UIWidget>();
widget.SetDimensions(Mathf.FloorToInt(tempSize.x), Mathf.FloorToInt(tempSize.y));
}
private void UpdateView()
{
this.Init();
this.UpdateSize();
Vector3[] corners = mPanel.worldCorners;
for (int i = 0; i < 4; ++i)
{
Vector3 v = corners[i];
v = this.content.transform.InverseTransformPoint(v);
corners[i] = v;
}
float min = 0;
float max = 0;
int tmpSizeH = 100;
int tmpSizeW = 100;
int maxInx = 0;
int startInx = 0;
int endInx = 0;
if (this.layout == Layout.Vertical)
{
min = corners[0].y;
max = corners[2].y;
max += tmpSizeH;
min -= tmpSizeH;
min = Mathf.Min(0, min);
max = Mathf.Min(0, max);
maxInx = Mathf.Max(0, this.dataList.Count - 1);
tmpSizeW = 0;
Vector2Int tempInx = this.GetStartInxAndEndInx(min,max);
startInx = tempInx.x;
endInx = tempInx.y;
startInx = Mathf.Min(startInx, maxInx);
endInx = Mathf.Min(endInx, maxInx);
}
else if (this.layout == Layout.Horizontal)
{
min = corners[0].x;
max = corners[2].x;
max += tmpSizeW;
min -= tmpSizeW;
min = Mathf.Max(0, min);
max = Mathf.Max(0, max);
maxInx = Mathf.Max(0, this.dataList.Count - 1);
tmpSizeH = 0;
Vector2Int tempInx = this.GetStartInxAndEndInx(min,max);
startInx = tempInx.x;
endInx = tempInx.y;
startInx = Mathf.Min(startInx, maxInx);
endInx = Mathf.Min(endInx, maxInx);
}
int count = this.dataList.Count;
Dictionary<int, UIItemRender>.Enumerator enumerator = this.dirCurItems.GetEnumerator();
bool recoverFocus = false;
while (enumerator.MoveNext())
{
int key = enumerator.Current.Key;
if (count > 0 && key >= startInx && key <= endInx)
{
this.tmpDic2[key] = enumerator.Current.Value;
}
else
{
bool tmp = this.RecoverItemRender(enumerator.Current.Value);
if (tmp)
recoverFocus = true;
}
}
this.dirCurItems.Clear();
Dictionary<int, UIItemRender> tmpDic = null;
tmpDic = dirCurItems;
dirCurItems = tmpDic2;
tmpDic2 = tmpDic;
if (count > 0 && startInx >= 0)
{
for (int i = startInx; i <= endInx; i++)
{
UIItemRender item = null;
Vector3 position = this.GetLocalPosByInx(i);
if (!this.dirCurItems.TryGetValue(i, out item))
{
item = this.CreateItemRender();
item.__index = i;
this.dirCurItems[i] = item;
item.SetData(this.dataList[i]);
}
item.gameObject.name = i.ToString();
item.transform.localPosition = position;
}
}
if (recoverFocus)
this.mScrollView.Press(false);
}
}
第二版
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System;
using LuaInterface;
// public enum Layout
// {
// Vertical,
// Horizontal
// }
/// <summary>
/// 无限Item列表
/// </summary>
public class UIDictListView : MonoBehaviour
{
public Layout layout;
public Vector2 spacing;
public GameObject itemPrefab;
private Vector3 panelDefaultPos;
private Vector2 panelDefaultClipOffset;
private UIPanel mPanel;
public UIScrollView mScrollView;
private UIItemRender itemRender = null;
private bool initialized = false;
private GameObject content;
private Vector2 scrollRectSize;
public bool bBackLoad = false;
public bool isCheackBackLoad = false;
public List<object> dataList = new List<object>();
private List<UIItemRender> lstItems = new List<UIItemRender>();
private GameObject pressObj;
private bool isDraging = false;
float svLastPos = 0; //记录scrollviet上一次的位置,用于判断scrollview的移动方向
void Awake()
{
this.Init();
}
public void Init()
{
if (initialized)
return;
initialized = true;
mPanel = NGUITools.FindInParents<UIPanel>(gameObject);
this.mScrollView = mPanel.GetComponent<UIScrollView>();
this.mPanel.onClipMove += OnUIPanelClipMove;
this.panelDefaultPos = mPanel.transform.localPosition;
this.panelDefaultClipOffset = mPanel.clipOffset;
// record some sizes
scrollRectSize = mPanel.GetViewSize();
// add a scrollrect content
GameObject go = new GameObject ();
go.name = "content";
go.transform.SetParent(transform);
UIWidget uiwidget = go.AddComponent<UIWidget>();
uiwidget.SetDimensions((int)scrollRectSize.x,(int)scrollRectSize.y);
go.transform.localPosition = Vector3.zero;
go.transform.localScale = Vector3.one;
this.content = go;
//itemPrefab
GameObject item = GameObject.Instantiate(itemPrefab) as GameObject;
item.transform.SetParent(mPanel.transform.parent);
item.transform.localPosition = new Vector3(1500, 1500, 1);
itemRender = item.GetComponent<UIItemRender>();
if (itemRender == null)
itemRender = item.AddComponent<UIItemRender>();
this.mScrollView.onDragStarted += delegate ()
{
this.isDraging = true;
};
this.mScrollView.onDragFinished += delegate ()
{
this.isDraging = false;
this.pressObj = null;
};
}
public void AddData(object obj)
{
this.dataList.Add(obj);
}
public void InsertData(object data,int inx)
{
this.dataList.Insert(inx, data);
}
public void ClearAll()
{
this.dataList.Clear();
}
public void UpdateNow()
{
this.Init();
this.AutoContentPivot();
this.UpdateView();
}
private void ClearAllItems()
{
var enumerator = this.lstItems.GetEnumerator();
while (enumerator.MoveNext())
{
Destroy(enumerator.Current.gameObject);
}
this.lstItems.Clear();
}
public bool CheckBackLoad(LuaTable table)
{
List<LuaTable> tableList = LuaTools.GetLuaTableToList<LuaTable>(table);
float containerH = 0;
for(int i = 0; i < tableList.Count; ++i)
{
itemRender.SetData(tableList[i]);
UIWidget itemWidget = itemRender.GetComponent<UIWidget>();
containerH = containerH + itemWidget.height;
// 初始布局,最多保证可隐藏的区域大于显示区域就可以了
if (containerH > (scrollRectSize.y))
{
return true;
}
}
return false;
}
public void AutoContentPivot()
{
this.ClearAllItems();
// 设置锚点
UIWidget contentWidget = this.content.GetComponent<UIWidget>();
contentWidget.SetDimensions((int)scrollRectSize.x,(int)scrollRectSize.y);
if(bBackLoad)
{
mPanel.transform.localPosition = this.panelDefaultPos - new Vector3(0,scrollRectSize.y - 1, 0);
mPanel.clipOffset = this.panelDefaultClipOffset + new Vector2(0,scrollRectSize.y - 1);
this.mScrollView.contentPivot = UIWidget.Pivot.BottomLeft;
contentWidget.pivot = UIWidget.Pivot.BottomLeft;
}
else
{
mPanel.transform.localPosition = this.panelDefaultPos;
mPanel.clipOffset = this.panelDefaultClipOffset;
this.mScrollView.contentPivot = UIWidget.Pivot.TopLeft;
contentWidget.pivot = UIWidget.Pivot.TopLeft;
}
this.content.transform.localPosition = Vector3.zero;
this.content.transform.localScale = Vector3.one;
}
private void OnUIPanelClipMove(UIPanel panel)
{
Vector3 delta = panelDefaultPos - panel.transform.localPosition;
float distance = delta.y;
float moveDis = this.mScrollView.transform.localPosition.y - svLastPos;
if (Mathf.Abs(moveDis) > 0.05 && lstItems.Count > 0)
{
bool isup = moveDis > 0;
if (isup)
{
if(bBackLoad)
{
float height = lstItems[lstItems.Count - 1].GetComponent<UIWidget>().height;
while (lstItems[lstItems.Count - 1].transform.localPosition.y - height > distance &&
lstItems[0].__index > 0)
{
UIItemRender itemRender = lstItems[lstItems.Count - 1];
int idx = lstItems[0].__index - 1;
itemRender.SetData(dataList[idx]);
itemRender.__index = idx;
itemRender.gameObject.name = idx.ToString();
lstItems.Insert(0, itemRender);
lstItems.RemoveAt(lstItems.Count - 1);
UIItemRender tempRender = lstItems[1];
float _height = tempRender.GetComponent<UIWidget>().height; //锚点左上角所以加itemRender height
itemRender.transform.localPosition = tempRender.transform.localPosition - new Vector3(0, _height, 0);
}
}
else
{
float height = lstItems[0].GetComponent<UIWidget>().height;
while (lstItems[0].transform.localPosition.y - height > distance &&
lstItems[lstItems.Count - 1].__index < dataList.Count - 1)
{
UIItemRender itemRender = lstItems[0];
int idx = lstItems[lstItems.Count - 1].__index + 1;
itemRender.SetData(dataList[idx]);
itemRender.__index = idx;
itemRender.gameObject.name = idx.ToString();
lstItems.Add(itemRender);
lstItems.RemoveAt(0);
UIItemRender tempRender = lstItems[lstItems.Count - 2];
float _height = tempRender.GetComponent<UIWidget>().height; //锚点左上角所以加tempRender height
itemRender.transform.localPosition = tempRender.transform.localPosition - new Vector3(0, _height, 0);
}
}
}
else
{
if(bBackLoad)
{
float height = lstItems[0].GetComponent<UIWidget>().height;
distance = distance - scrollRectSize.y;
while (distance > lstItems[0].transform.localPosition.y &&
lstItems[lstItems.Count - 1].__index < dataList.Count - 1)
{
UIItemRender itemRender = lstItems[0];
int idx = lstItems[lstItems.Count - 1].__index + 1;
itemRender.SetData(dataList[idx]);
itemRender.__index = idx;
itemRender.gameObject.name = idx.ToString();
lstItems.Add(itemRender);
lstItems.RemoveAt(0);
UIItemRender tempRender = lstItems[lstItems.Count - 2];
float _height = itemRender.GetComponent<UIWidget>().height; //锚点左上角所以加tempRender height
itemRender.transform.localPosition = tempRender.transform.localPosition + new Vector3(0, _height, 0);
}
}
else
{
float height = lstItems[lstItems.Count - 1].GetComponent<UIWidget>().height;
float temp = Mathf.Abs(distance) + scrollRectSize.y;
while (lstItems[lstItems.Count - 1].transform.localPosition.y < - temp &&
lstItems[0].__index > 0)
{
UIItemRender itemRender = lstItems[lstItems.Count - 1];
int idx = lstItems[0].__index - 1;
itemRender.SetData(dataList[idx]);
itemRender.__index = idx;
itemRender.gameObject.name = idx.ToString();
lstItems.Insert(0, itemRender);
lstItems.RemoveAt(lstItems.Count - 1);
UIItemRender tempRender = lstItems[1];
float _height = itemRender.GetComponent<UIWidget>().height; //锚点左上角所以加itemRender height
itemRender.transform.localPosition = tempRender.transform.localPosition + new Vector3(0, _height, 0);
}
}
}
svLastPos = this.mScrollView.transform.localPosition.y;
}
}
private UIItemRender loadOneItem()
{
GameObject item = GameObject.Instantiate(itemPrefab) as GameObject;
UIItemRender itemRender = item.GetComponent<UIItemRender>();
item.transform.SetParent(this.content.transform);
item.transform.localScale = Vector3.one;
lstItems.Add(itemRender);
return itemRender;
}
private void UpdateView()
{
svLastPos = 0;
this.ClearAllItems();
float containerH = 0;
Vector3 itemPosition = Vector3.zero;
for(int i = 0; i < dataList.Count; ++i)
{
UIItemRender itemRender = this.loadOneItem();
itemRender.SetData(dataList[i]);
itemRender.__index = i;
itemRender.gameObject.name = i.ToString();
UIWidget widget = itemRender.GetComponent<UIWidget>();
if(bBackLoad)
{
containerH = containerH + widget.height; // 锚点在左上所以位置不是从0开始
itemPosition.y = itemPosition.y + widget.height;
itemRender.transform.localPosition = new Vector3(0,itemPosition.y, 0);
}
else
{
itemRender.transform.localPosition = new Vector3(0,itemPosition.y, 0);
containerH = containerH + widget.height;
itemPosition.y = itemPosition.y - widget.height;
}
// 初始布局,最多保证可隐藏的区域大于显示区域就可以了
if (containerH > (scrollRectSize.y * 1.5f))
{
break;
}
}
}
}