2025-07-19 17:59:28 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
|
|
|
|
namespace CreatGame.UI
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// item提供者
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public delegate string LoopListItemProvider(int index);
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// item渲染函数
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public delegate void LoopListItemRenderer(GameObject item,int index);
|
2025-07-19 20:29:55 +08:00
|
|
|
|
[RequireComponent(typeof(UnityEngine.UI.LoopScrollRect))]
|
|
|
|
|
|
[DisallowMultipleComponent]
|
2025-07-19 17:59:28 +08:00
|
|
|
|
public class UILoopList : MonoBehaviour, LoopScrollPrefabSource, LoopScrollDataSource
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
///
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public LoopListItemRenderer ListItemRenderer;
|
2025-07-19 20:29:55 +08:00
|
|
|
|
|
2025-07-19 17:59:28 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
///
|
|
|
|
|
|
/// </summary>
|
2025-07-19 20:29:55 +08:00
|
|
|
|
private Stack<Transform> itemPool = new Stack<Transform>();
|
2025-07-19 17:59:28 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 预制件
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public GameObject itemPrefab;
|
|
|
|
|
|
public GameObject GetObject(int index)
|
|
|
|
|
|
{
|
2025-07-19 20:29:55 +08:00
|
|
|
|
if (itemPool.Count == 0)
|
2025-07-19 17:59:28 +08:00
|
|
|
|
{
|
2025-07-19 20:29:55 +08:00
|
|
|
|
return Instantiate(itemPrefab);
|
2025-07-19 17:59:28 +08:00
|
|
|
|
}
|
2025-07-19 20:29:55 +08:00
|
|
|
|
Transform candidate = itemPool.Pop();
|
|
|
|
|
|
candidate.gameObject.SetActive(true);
|
|
|
|
|
|
return candidate.gameObject;
|
2025-07-19 17:59:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void ReturnObject(Transform trans)
|
|
|
|
|
|
{
|
2025-07-19 20:29:55 +08:00
|
|
|
|
trans.gameObject.SetActive(false);
|
|
|
|
|
|
trans.SetParent(transform, false);
|
|
|
|
|
|
itemPool.Push(trans);;
|
2025-07-19 17:59:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void ProvideData(Transform transform, int idx)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ListItemRenderer == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError("ListItemRenderer is null");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
ListItemRenderer(transform.gameObject, idx);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
///
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public LoopScrollRect ScrollRect { get; private set; }
|
2025-07-19 20:29:55 +08:00
|
|
|
|
private void Awake()
|
2025-07-19 17:59:28 +08:00
|
|
|
|
{
|
|
|
|
|
|
ScrollRect = GetComponent<LoopScrollRect>();
|
|
|
|
|
|
ScrollRect.prefabSource = this;
|
|
|
|
|
|
ScrollRect.dataSource = this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int ItemCount
|
|
|
|
|
|
{
|
|
|
|
|
|
get => ScrollRect.totalCount;
|
2025-07-19 20:29:55 +08:00
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
ScrollRect.totalCount = value;
|
|
|
|
|
|
ScrollRect.RefreshCells();
|
|
|
|
|
|
}
|
2025-07-19 17:59:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-19 20:29:55 +08:00
|
|
|
|
public void ScrollToItem(int index)
|
2025-07-19 17:59:28 +08:00
|
|
|
|
{
|
2025-07-19 20:29:55 +08:00
|
|
|
|
ScrollRect?.ScrollToCell(index - 1,100.0f);
|
2025-07-19 17:59:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|