CustomSV.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public interface IItemBase<T>
  5. {
  6. void InitInfo(T info);
  7. }
  8. /// <summary>
  9. ///
  10. /// </summary>
  11. /// <typeparam name="T">数据源</typeparam>
  12. /// <typeparam name="K">格子类</typeparam>
  13. public class CustomSV<T, K> where K : IItemBase<T>
  14. {
  15. private RectTransform content;
  16. private int viewHeight;
  17. private Dictionary<int, GameObject> currItemDic = new Dictionary<int, GameObject>();
  18. private List<T> itemList;
  19. private int preMinIndex = -1;
  20. private int preMaxIndex = -1;
  21. private float itemW;
  22. private float itemH;
  23. private int col;
  24. public CustomSV(RectTransform content,int h,List<T> itemList,float itemW,float itemH)
  25. {
  26. this.content = content;
  27. this.viewHeight = h;
  28. this.itemList = itemList;
  29. this.itemW = itemW;
  30. this.itemH = itemH;
  31. }
  32. private void InitContentHeigth()
  33. {
  34. content.sizeDelta = new Vector2(col * itemW, Mathf.CeilToInt(itemList.Count / col) * itemH);
  35. }
  36. public void UpdateConten()
  37. {
  38. int minIndex = (int)(content.anchoredPosition3D.y / itemH) * col;
  39. int maxIndex = (int)((content.anchoredPosition3D.y + viewHeight) / itemH) * col + (col - 1);
  40. }
  41. private int IndexLimit(int index)
  42. {
  43. return Mathf.Clamp(index, 0, itemList.Count - 1);
  44. }
  45. }