Files
aibis-dream/Assets/Plugins/LoopScrollRect/Samples/Demo/Scripts/InitOnStart.cs
T
2025-09-16 17:13:21 +08:00

51 lines
1.5 KiB
C#

using UnityEngine;
using System.Collections.Generic;
using AibisDream.UI;
using UnityEngine.UI;
namespace Demo
{
[RequireComponent(typeof(LoopScrollRect))]
[DisallowMultipleComponent]
public class InitOnStart : MonoBehaviour, ILoopScrollPrefabSource, ILoopScrollDataSource
{
public GameObject item;
public int totalCount = -1;
// Implement your own Cache Pool here. The following is just for example.
Stack<Transform> pool = new Stack<Transform>();
public GameObject GetObject(int index)
{
if (pool.Count == 0)
{
return Instantiate(item);
}
Transform candidate = pool.Pop();
candidate.gameObject.SetActive(true);
return candidate.gameObject;
}
public void ReturnObject(Transform trans)
{
// Use `DestroyImmediate` here if you don't need Pool
trans.SendMessage("ScrollCellReturn", SendMessageOptions.DontRequireReceiver);
trans.gameObject.SetActive(false);
trans.SetParent(transform, false);
pool.Push(trans);
}
public void ProvideData(Transform transform, int idx)
{
transform.SendMessage("ScrollCellIndex", idx);
}
void Start()
{
var ls = GetComponent<LoopScrollRect>();
ls.prefabSource = this;
ls.dataSource = this;
ls.totalCount = totalCount;
ls.RefillCells();
}
}
}