Unity UI组件ScrollRect实现无限滚动条

这篇文章主要为大家详细介绍了Unity UI组件ScrollRect实现无限滚动条,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

在游戏开发中经常遇到滚动显示的数据,特别是商店商品 排行榜 .......等数据很多,每一条数据去加载一个UI来显示显然对内存浪费很大,这种情况处理一般就是用几个显示条可滚动循环显示无限数据条。本篇介绍实现过程和大体思路以及可重用的滑动脚本InfinityGridLayoutGroup和MarketLayoutGroup数据管理刷新脚本。MarketElement类要看具体项目中具体数据结构来设计;仅供参考。

一 .总体流程

建一个循环滑动脚本 InfinityGridLayoutGroup类并且有删除刷新功能;

一个数据管理刷新脚本如:MarketLayoutGroup(商店市场数据刷新管理器),继承InfinityGridLayoutGroup类 并且在滑动   的时候给每条数据对象赋值显示;

一个单数据对象MarketElement;

一个UI 用于显示滑动;

1 .InfinityGridLayoutGroup类:

 using UnityEngine; using System.Collections; using UnityEngine.UI; using System.Collections.Generic; [RequireComponent(typeof(GridLayoutGroup))] [RequireComponent(typeof(ContentSizeFitter))] public class InfinityGridLayoutGroup : MonoBehaviour { public int minAmount = 0;//实现无限滚动,需要的最少的child数量。屏幕上能看到的+一行看不到的,比如我在屏幕上能看到 2 行,每一行 2 个。则这个值为 2行*2个 + 1 行* 2个 = 6个。 public bool changePanel = false;//切换面板 public bool up = false; RectTransform rectTransform; GridLayoutGroup gridLayoutGroup; ContentSizeFitter contentSizeFitter; ScrollRect scrollRect; List children = new List(); Vector2 startPosition; public int amount = 0; public delegate void UpdateChildrenCallbackDelegate(int index, Transform trans); public UpdateChildrenCallbackDelegate updateChildrenCallback = null; public int realIndex = -1; int realIndexUp = -1; //从下往上; #region // 改动 Vector2 gridLayoutSizeLast; Vector2 gridLayoutPosLast; Vector2 currentPos; #endregion public bool hasInit = false; Vector2 gridLayoutSize; Vector2 gridLayoutPos; Dictionary childsAnchoredPosition = new Dictionary(); Dictionary childsSiblingIndex = new Dictionary(); // Use this for initialization void Start() { StartCoroutine(InitChildren()); } IEnumerator InitChildren() { yield return 0; // Debug.Log("hasInit" + hasInit); minAmount = transform.childCount; if (!hasInit) { //childsAnchoredPosition.Clear(); //获取Grid的宽度; rectTransform = GetComponent(); gridLayoutGroup = GetComponent(); gridLayoutGroup.enabled = false; contentSizeFitter = GetComponent(); contentSizeFitter.enabled = false; gridLayoutSizeLast = rectTransform.sizeDelta; gridLayoutPos = rectTransform.anchoredPosition; gridLayoutSize = rectTransform.sizeDelta; // Debug.Log("children---" + children.Count + "realIndex---" + realIndex); //注册ScrollRect滚动回调; scrollRect = transform.parent.GetComponent(); scrollRect.onValueChanged.AddListener((data) => { ScrollCallback(data); }); //获取所有child anchoredPosition 以及 SiblingIndex; for (int index = 0; index (); childsAnchoredPosition.Add(child, childRectTrans.anchoredPosition); childsSiblingIndex.Add(child, child.GetSiblingIndex()); } //Debug.Log("children---" + children.Count + "realIndex---" + realIndex); } else { //Debug.Log("gridLayoutPosLast--" + gridLayoutSizeLast.y); rectTransform.anchoredPosition = new Vector2(gridLayoutPos.x, Vector2.zero.y); // Debug.Log("current--" + currentPos.y); rectTransform.sizeDelta = gridLayoutSize; gridLayoutSizeLast = rectTransform.sizeDelta; // Debug.Log("rectTransform.sizeDelta--" + rectTransform.sizeDelta.y); children.Clear(); realIndex = -1; realIndexUp = -1; //children重新设置上下顺序; foreach (var info in childsSiblingIndex) { info.Key.SetSiblingIndex(info.Value); } //children重新设置anchoredPosition; for (int index = 0; index (); if (childsAnchoredPosition.ContainsKey(child)) { childRectTrans.anchoredPosition = childsAnchoredPosition[child]; } else { Debug.LogError("childsAnchoredPosition no contain " + child.name); } } } //获取所有child; for (int index = 0; index ()); //初始化前面几个; UpdateChildrenCallback(children.Count - 1, transform.GetChild(index)); } startPosition = rectTransform.anchoredPosition; // Debug.Log("children---"+ children.Count+ "realIndex---"+ realIndex); realIndex = children.Count - 1; ShowElement(realIndex); //Debug.Log( scrollRect.transform.TransformPoint(Vector3.zero)); // Debug.Log(transform.TransformPoint(children[0].localPosition)); hasInit = true; //如果需要显示的个数小于设定的个数; for (int index = 0; index  0) { rectTransform.sizeDelta -= new Vector2(0, (gridLayoutGroup.cellSize.y

以上就是Unity UI组件ScrollRect实现无限滚动条的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程