using UnityEngine;
using System.Collections.Generic;
/// <summary>
/// 这个类主要做了一件事,就是优化了,NGUI UIScrollView 在数据量很多都时候,
/// 创建过多都GameObject对象,造成资源浪费.
/// </summary>
public class LoopScrollView : MonoBehaviour
{
public enum ArrangeDirection
{
Left_to_Right,
Right_to_Left,
Up_to_Down,
Down_to_Up,
}
/// <summary>
/// items的排列方式
/// </summary>
public ArrangeDirection arrangeDirection = ArrangeDirection.Up_to_Down;
/// <summary>
/// 列表脚本
/// </summary>
public UIScrollView scrollView;
public GameObject itemParent;
/// <summary>
/// 列表单项模板
/// </summary>
public GameObject itemPrefab;
[HideInInspector]
public int maxPoolItem = 3;
/// <summary>
/// The items list.
/// </summary>
public List<LoopItemObject> itemsList;
/// <summary>
/// The datas list.
/// </summary>
public List<object> datasList;
/// <summary>
/// itemsList的第一个元素
/// </summary>
LoopItemObject firstItem;
/// <summary>
/// itemsList的最后一个元素
/// </summary>
LoopItemObject lastItem;
public delegate void DelegateHandler (LoopItemObject item, object data);
/// <summary>
/// 响应
/// </summary>
public DelegateHandler OnItemInit;
/// <summary>
/// 第一item的起始位置
/// </summary>
public Vector3 itemStartPos = Vector3.zero;
/// <summary>
/// 菜单项间隙
/// </summary>
public float gapDis = 0f;
// 对象池
// 再次优化,频繁的创建与销毁
Queue<LoopItemObject> itemLoop = new Queue<LoopItemObject> ();
void Awake ()
{
if (itemPrefab == null || scrollView == null || itemParent == null) {
Debug.LogError ("LoopScrollView.Awake() 有属性没有在inspector中赋值");
}
// 设置scrollview的movement
if (arrangeDirection == ArrangeDirection.Up_to_Down ||
arrangeDirection == ArrangeDirection.Down_to_Up) {
scrollView.movement = UIScrollView.Movement.Vertical;
} else {
scrollView.movement = UIScrollView.Movement.Horizontal;
}
scrollView.pane
LoopScrollView
最新推荐文章于 2023-11-15 16:06:21 发布