NGUI 无限背包实现

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


//思路:View的位置是Grid的兩倍
//根据这个框来创建item
public class ScoreMoveTestTwo : MonoBehaviour
{
    private UIPanel uIPanel;
    private UIScrollView iScrollView;
    public Transform grid;//Grid 
    public GameObject prefab;//格子的原型
    UIGrid scoreGrid;
    private Vector3 vec3;
    private float vecoffer;    //初始的偏移量


    public List<GameObject> prefabsList = new List<GameObject>();
    public int ShowItem = 1;//展示的数据
    public int maxlimit = 20;
    private int CreateItem;
    private float itemHight = 0;
    private int uIndex = 0;
    private int bIndex = 0; 


    //外边框
    private Vector2 vectorsize;
    private float sizeY;


    /// <summary>
    /// 是否向下
    /// </summary>
    bool isDown = false;
    //下一帧拖拽的位置
    float offsetEndY;
    //这一帧拖拽的位置
    float offSetStartY;
    //上一帧的偏移量
    float DargYOffSet;
    float HightItem;
    float HightItemone;
    SpringPanel springPanel;
    float DragYoffsetOne;
    Vector2 vec;
    Vector2 vec2;
    Vector3 loca;
    Vector3 locamax;
    float lastmax;
    bool isStart;


    //创建item和view的宽高比


    public void Start()
    {
        //获取组件
        uIPanel = GetComponent<UIPanel>();
        iScrollView = GetComponent<UIScrollView>();
        scoreGrid = grid.GetComponent<UIGrid>();
        vec3 = iScrollView.transform.localPosition;
        //grid.localPosition = Vector3.zero;//设置Grid的位置






        //每一个item的大小
        //itemHight = scoreGrid.cellHeight;
        CreateItem = ShowItem + 2;


        //最后一个格子的数据
        bIndex = ShowItem + 1;
        //开始的时候获取偏移量的大小
        vecoffer = uIPanel.clipOffset.y;
        offsetEndY = uIPanel.clipOffset.y;
        DargYOffSet = uIPanel.clipOffset.y;








        //首先就是根据创建的宽度大小获取框的size;
        //然后获取Grid的分布的大小
        //获取每一个item的分布的大小
        //根据size来获取可以创建几个item然后哦谈过获取的item的值赋值到size上,改变size的大小
        //是输入显示多少,然后根据知道的外边框的大小
        Vector2 vecSize = uIPanel.GetViewSize();//创建的View的大小
        float itemPrefabSize = Mathf.FloorToInt(vecSize.y / ShowItem);
        scoreGrid.cellHeight = itemPrefabSize;
        prefab.gameObject.GetComponent<UISprite>().height = (int)scoreGrid.cellHeight;
        Vector3 Vec3Box = prefab.gameObject.GetComponent<BoxCollider>().size;
        Vec3Box.y = scoreGrid.cellHeight;
        prefab.gameObject.GetComponent<BoxCollider>().size = Vec3Box;


        itemHight = scoreGrid.cellHeight;  //设置宽度和碰撞体的大小
        HightItem = itemHight;
        print(prefab.gameObject.GetComponent<UISprite>().height);
        Debug.Log(prefab.gameObject.GetComponent<BoxCollider>().size);




        float itemUP = (uIPanel.GetViewSize().y - itemHight) / 2;//设置局部坐标
        Vector3 vecUp = grid.localPosition;
        vecUp.y = itemUP;
        grid.localPosition = vecUp;
        print("局部坐标" + grid.localPosition);






        //print("item的数量" + itemPrefabSize);
        Create();
    }
    //在这需要判断移动了多少格
    //根据多少格然后加载数据
    public void Update()
    {
        ItemExchange();
        LimitItem();
    }
    public static void GridReposition(UIGrid grid)
    {
        if (grid != null)
        {
            grid.Reposition();
        }
    } // grid到顶部  


    public static void ScrollResetPosition(UIScrollView scroll)
    {
        if (scroll != null)
        {
            scroll.ResetPosition();
        }
    }//scrollView回到顶部 


    public void ItemExchange()
    {
        if (uIPanel.clipOffset.y < vecoffer)//判断这个偏移量//说明动了//現在的偏移量和初始化的偏移量的区别//现在就需要判断当我动了的时候后面加载的也
        {
            //下拉
            if (prefabsList[0].transform.localPosition.y - uIPanel.clipOffset.y > HightItem + Mathf.Abs(vecoffer) + HightItem / 2)
            {
                if (Mathf.Abs(uIPanel.clipOffset.y - offsetEndY
                    ) >= HightItem)
                {
                    //print("開始");
                    //存储开始拖拽的位置
                    offSetStartY = uIPanel.clipOffset.y;//-50//现在的偏移量和上一帧的偏移量的差/
                    int offer = Mathf.FloorToInt((Mathf.Abs(uIPanel.clipOffset.y - DargYOffSet) / itemHight));
                    print("下拉多少次" + offer);
                    for (int i = 0; i < offer; i++)
                    {
                        if (offSetStartY - DargYOffSet <= 0 && offsetEndY > offSetStartY)//向下的偏移量的判断不对
                        {
                            isDown = true;
                            OnScrollValueChanged();
                            //  DargYOffSet = offSetStartY;
                        }


                        //赋值 把只有拖拽的时候才能进去
                    }
                    // offsetEndY = uIPanel.clipOffset.y;//


                }


                //DargYOffSet = offSetStartY;
            }
            //判断上边界 //现在的偏移量减去集合中最后一位的局部坐标(就是判断开始的位置和最后一个位置的距离)>=跟一个固定的值进行判断(每个item的高度*倒数第二个减去初始的偏移量-半个item的距离)
            if (uIPanel.clipOffset.y - prefabsList[prefabsList.Count - 1].transform.localPosition.y >= (itemHight * (prefabsList.Count - 2)) + vecoffer - itemHight / 2)
            {
                if (Mathf.Abs(uIPanel.clipOffset.y - offsetEndY) >= HightItem)
                {
                    //存储开始拖拽的位置
                    offSetStartY = uIPanel.clipOffset.y;//-50
                                                        //现在的偏移量和上一帧的偏移量的差/
                    int offer = Mathf.FloorToInt((Mathf.Abs(uIPanel.clipOffset.y - DargYOffSet) / itemHight));
                    print("下拉" + offer);
                    for (int i = 0; i <= offer + 1; i++)
                    {
                        if (offSetStartY - DargYOffSet > 0 /*&& offsetEndY>offSetStartY*/)//判断是否大于一个的高度
                        {
                            isDown = false;
                            OnScrollValueChanged();
                            //DargYOffSet = offSetStartY;
                        }
                    }


                }
                //DargYOffSet = offSetStartY;
            }
            //判断快速拉动的时候出现的数据不刷新//思路:判断上边界的偏移量  //当他小于超出了count-2的格子的距离//正数<固定的值   //判断超出了显示的边界
            if (uIPanel.clipOffset.y - prefabsList[prefabsList.Count - 1].transform.localPosition.y < vecoffer + (itemHight * (prefabsList.Count - 3) + itemHight / 2))
            {
                //存储开始拖拽的位置
                //offSetStartY = uIPanel.clipOffset.y;
                isDown = true;
                OnScrollValueChanged();
                // DargYOffSet = offSetStartY;
                // DragYoffsetOne = offSetStartY;
                print("这是上拉");


            }
            DargYOffSet = offSetStartY;
            offsetEndY = DargYOffSet;


        }
    }


    //限制偏移量的大小//不讓偏移量变小






    /// <summary>
    /// 对于NGUI无限滚动的第二种做法
    /// 难点:对于快速下拉时出现卡顿的现象,对于滚动的点的控制
    /// 思路:运用上一帧的偏移量和下一帧的偏移量除以宽度运用数学类求出几个item,然后一起记载,
    /// </summary>


    private GameObject go;
    /// <summary>
    /// 初始化创建格子
    /// </summary>
    public void Create()
    {
        for (int i = 0; i < CreateItem; i++)
        {
            go = Instantiate(prefab, grid) as GameObject;
            go.transform.Find("Label").GetComponent<UILabel>().text = "" + i;//格子上面显示的数字
            go.transform.localPosition = new Vector3(0, -itemHight * i, 0);
            go.name = i.ToString();
            prefabsList.Add(go);
        }
    }
    /// <summary>
    /// 创建后面的prefabs
    /// </summary>
    void OnScrollValueChanged()
    {
        if (isDown)
        {
            if (uIndex >= 0 && bIndex >= CreateItem - 1)
            {
                uIndex++;
                bIndex++;
                if (uIndex >= 0)
                {
                    if (bIndex < maxlimit)//限制
                    {
                        if (bIndex >= maxlimit - 1)//判断最大值
                        {
                            UpdateList(isDown);
                            iScrollView.restrictWithinPanel = true;
                        }
                        else
                        {
                            UpdateList(isDown);
                            iScrollView.restrictWithinPanel = false;
                        }
                    }
                }
                if (bIndex >= maxlimit)
                {
                    bIndex = maxlimit - 1;
                }
            }
            //向下滑动
        }
        else
        {
            //向下划都减
            if (uIndex > 0 && bIndex > CreateItem - 1)
            {
                uIndex--;
                bIndex--;
                if (bIndex >= maxlimit - 1)
                {


                    bIndex = maxlimit - 1;
                }
                if (uIndex >= maxlimit - CreateItem)//限定最小值的索引
                {
                    uIndex = maxlimit - CreateItem - 1;
                }
                if (uIndex >= 0)
                {
                    UpdateList(isDown);
                }
            }
        }
    }
    private int intx = 0;
    GameObject gob;
    GameObject gobj;
    int index;
    int indexone;
    //更新格子
    void UpdateList(bool isUp)
    {
        //向下滑动给最后位置加格子就行
        index = 0;
        if (!isUp)
        {
            //向下滑动
            index = prefabsList.Count - 1;//最后一个值的索引
            gob = prefabsList[index];
            gob.transform.Find("Label").GetComponentInChildren<UILabel>().text = uIndex.ToString();
            gob.name = uIndex.ToString();
            prefabsList.RemoveAt(index);//移除这个位置的格子
            prefabsList.Insert(0, gob);//把生成的格子插入集合的第一个位置
            gob.transform.localPosition = new Vector3(0, -uIndex * itemHight, 0);//重新生成位置
        }
        //向下滑动给最前位置加格子就行
        else
        {
            //向上滑动
            gob = prefabsList[index];
            gob.transform.Find("Label").GetComponentInChildren<UILabel>().text = bIndex.ToString();
            gob.name = bIndex.ToString();
            prefabsList.RemoveAt(index);
            prefabsList.Add(gob);
            gob.transform.localPosition = new Vector3(0, -bIndex * itemHight, 0);


        }
    }
    /// <summary>
    /// 限制
    /// </summary>
    public void LimitItem()
    {
        if (uIPanel.clipOffset.y > vecoffer)
        {
            //-.限制面板
            iScrollView.restrictWithinPanel = true;
        }
    }
    public void Close()
    {
        iScrollView.restrictWithinPanel = false;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41600074/article/details/80519076