当前位置:网站首页>Scrollrect rewrite recycle
Scrollrect rewrite recycle
2022-06-22 07:36:00 【Liam666】
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CollectionView : ScrollRect
{
public Vector2 sizeOfCells;
public float spaceOfH;
public float spaceOfV;
public delegate CollectionViewCell CellOfIndex(CollectionView view, int index);
public delegate int NumberOfCells(CollectionView view);
public CellOfIndex delegateCellOfIndex = null;
public NumberOfCells delegateNumberOfCells = null;
private int countOfCells;
private int _curMin = -1;
private int _curMax = -1;
/// <summary>
/// Horizontal number
/// </summary>
private int numberOfH;
/// <summary>
/// Vertical number
/// </summary>
private int numberOfV;
private Vector2 sizeOfContent;
private GameObject _prefab;
/// <summary>
/// Displayed Cell
/// </summary>
private Dictionary<int, CollectionViewCell> dict = new Dictionary<int, CollectionViewCell>();
/// <summary>
/// Cell Reuse cache
/// </summary>
private Queue<GameObject> _poolQueue = new Queue<GameObject>();
protected override void Start()
{
base.Start();
this.onValueChanged.AddListener(OnMove);
}
/// <summary>
/// Register default
/// </summary>
/// <param name="prefab"></param>
public void register(GameObject prefab)
{
_prefab = prefab;
}
public void register(string assetBundleName, AssetBundleType type, string assetName)
{
_prefab = AssetBundleTool.instance.LoadResource<GameObject>(assetBundleName, type, assetName);
}
/// <summary>
///
/// </summary>
/// <param name="identify"></param>
/// <returns></returns>
public T ReuseDequeue<T>(string identify = "") where T : CollectionViewCell
{
GameObject obj;
if (_poolQueue.Count > 0)
{
obj = _poolQueue.Dequeue();
obj.SetActive(true);
}
else
obj = Instantiate(_prefab);
obj.transform.SetParent(this.content);
obj.transform.localScale = Vector3.one;
T cell = obj.GetComponent<T>();
if(cell == null)
cell = obj.AddComponent<T>();
return cell;
}
public T CellAtIndex<T>(int index) where T : CollectionViewCell
{
T cell = null;
if (dict.ContainsKey(index))
{
cell = dict[index] as T;
}
return cell;
}
/// <summary>
///
/// </summary>
public void reload(bool top = true)
{
Vector2 pos = top ? Vector2.zero : this.content.anchoredPosition;
if (delegateNumberOfCells != null)
countOfCells = delegateNumberOfCells(this);
float width = 0;
float height = 0;
if(vertical)
{
width = Mathf.Max(this.content.sizeDelta.x, 0f);
numberOfH = Mathf.FloorToInt(width / sizeOfCells.x);
numberOfV = Mathf.CeilToInt((float)countOfCells / (float)numberOfH);
height = numberOfV * sizeOfCells.y + (numberOfV - 1) * spaceOfV;
}
else
{
height = Mathf.Max(this.content.sizeDelta.y, 0f);
numberOfV = Mathf.FloorToInt(height / sizeOfCells.y);
numberOfH = Mathf.CeilToInt((float)countOfCells / (float)numberOfV);
width = numberOfH * sizeOfCells.x + (numberOfH - 1) * spaceOfH;
}
sizeOfContent = new Vector2(width, height);
this.content.sizeDelta = sizeOfContent;
this.content.anchoredPosition = pos;
int min, max;
Vector2 anchoredPos = indexOfBounder(this.content.anchoredPosition, out min, out max);
if(!(_curMin == -1 && _curMax == -1))
{
for (int i = _curMin; i <= _curMax; i++)
{
removeCell(i);
}
}
if(countOfCells != 0)
{
for (int i = min; i <= max; i++)
{
addCell(i);
}
}
_curMin = min;
_curMax = max;
}
protected void OnMove(Vector2 value)
{
if (horizontal && (value.x < 0 || value.x > 1)) return;
if (vertical && (value.y < 0 || value.y > 1)) return;
if (countOfCells == 0 || dict.Count == 0) return;
int min;
int max;
Vector2 anchoredPos = indexOfBounder(this.content.anchoredPosition, out min, out max);
//Debug.LogFormat("{0}-{1} {2}-{3}", _curMin, _curMax, min, max);
updateContent(min, max);
}
private Vector2 indexOfBounder(Vector2 anchoredPosition, out int min, out int max)
{
Vector2 vector = anchoredPosition;
vector = new Vector2(-vector.x, vector.y);
vector = new Vector2(Mathf.Min(vector.x, sizeOfContent.x), Mathf.Min(vector.y, sizeOfContent.y));
Vector2 anchoredPos = new Vector2(Mathf.Max(vector.x, 0), Mathf.Max(vector.y, 0));
if (vertical)
{
min = Mathf.FloorToInt(anchoredPos.y / (sizeOfCells.y + spaceOfV));
max = Mathf.CeilToInt((anchoredPos.y + this.viewport.rect.height) / (sizeOfCells.y + spaceOfV));
min = min * numberOfH;
max = Mathf.Min(max * numberOfH - 1, countOfCells - 1);
}
else
{
min = Mathf.FloorToInt(anchoredPos.x / (sizeOfCells.x + spaceOfH));
max = Mathf.CeilToInt((anchoredPos.x + this.viewport.rect.width) / (sizeOfCells.x + spaceOfH));
min = min * numberOfV;
max = Mathf.Min(max * numberOfV - 1, countOfCells - 1);
}
return anchoredPos;
}
private void addCell(int index)
{
CollectionViewCell cell = delegateCellOfIndex(this, index);
cell.show(this, index);
RectTransform t = cell.transform as RectTransform;
float x = 0;
float y = 0;
int indexX;
int indexY;
if (vertical)
{
indexX = index % numberOfH;
indexY = index / numberOfH;
}
else
{
indexX = index / numberOfV;
indexY = index % numberOfV;
}
x = indexX * sizeOfCells.x + spaceOfH * indexX;
y = -indexY * sizeOfCells.y - spaceOfV * indexY;
t.anchoredPosition3D = new Vector3(x, y, 0f);
dict[index] = cell;
}
private void removeCell(int index)
{
CollectionViewCell cell = dict[index];
cell.hide(this, index);
cell.gameObject.SetActive(false);
_poolQueue.Enqueue(cell.gameObject);
}
private void updateContent(int min, int max)
{
if (_curMin == min && _curMax == max) return;
if (_curMin < min)
{
//remove
for (int i = _curMin; i < min; i++)
{
removeCell(i);
}
}
if (max < _curMax)
{
//remove
for (int i = max + 1; i <= _curMax; i++)
{
removeCell(i);
}
}
if (min < _curMin)
{
//add
for (int i = min; i < _curMin; i++)
{
addCell(i);
}
}
if (_curMax < max)
{
//add
for (int i = _curMax + 1; i <= max; i++)
{
addCell(i);
}
}
_curMin = min;
_curMax = max;
}
}
边栏推荐
- Chromedriver all versions download
- Application and problem solving of robotframework
- Get through version - bargain activity
- Parameter curve notes of coursera self driving car Part4 motion planning
- Antd framework: click the link to reopen the browser page - Basic accumulation
- Docker command, docker installation sqlserver2019, docker installation MySQL (continuous update)
- Canoe learning notes (1) illustration of new project and channel configuration steps
- CONDA installation method of deepmd kit and the solution of slow speed
- How to upload Taobao tmall products with one click
- Flutter input field maxlines dynamic change
猜你喜欢

Taobao store backup one store upload to multiple stores precautions

Open source get through version - integral function

Matlab用深度学习循环神经网络RNN长短期记忆LSTM进行波形时间序列数据预测

Propeller framework v2.3 releases high reusable operator library Phi: Restructure development paradigm, reduce cost and increase efficiency

A training summary of Intranet penetration test

How to backup the treasures in the store and upload them to multiple stores
The solution of word document being locked and unable to edit

Antd framework: click the link to reopen the browser page - Basic accumulation

antd——a-upload-dragger拖拽上传组件——基础积累

Real MySQL interview questions (20) -- video data analysis practice
随机推荐
Impact of dead chain on websites
White paper on Web page quality of Baidu search engine guides website construction and optimization
ASP. Net core development experience
Speed planning generation of coursera self driving car Part4 motion planning
Wechat applet service provider sub merchant payment order interface
Find and replace the spaces, Nan and special symbols contained in a column of data in the dataframe
How to upload Taobao tmall products with one click
Tikz learning notes (IV) further exploration of circle and complex geometry
itertools 排列组合
antd 框架:点击链接重开浏览器页面——基础积累
Antd - a-upload-dragger drag upload component - Basic accumulation
Crmeb mall distribution function
FFMPEG坑
Canoe learning notes (6) diagram of logging block data recording module
Typescript & explain in, keyof, extensions, index signature, record and typeof in detail (updated from time to time)
How was the coffee supply chain leveled?
Solve syntaxerror: cannot use import statement outside a module
2 lines of text on the right of the left Avatar
飞桨框架v2.3发布高可复用算子库PHI:重构开发范式,降本增效
[standard version 4.3] marketing activities (group bargaining second kill) error reporting under orders