UGUI 实现无限滚动

               

因为在界面中大量使用了 Outline 以及界面复杂度较高,在邮件 等需要超长滚动列表的界面,就会十分卡顿。既然美术与策划同学不能妥协,那就来优化代码。

按照滚动区域的属性,展示给玩家看的只是一小块区域,比如列表中有100封邮件,其实只要显示个七八封就够了,剩下的,在向上滑动的时候,把第一排移动到最后一排,替换内容。

这样做能提高绘制效率,但是因为是在滑动的时候动画创建,所以会稍有卡顿,所以只适用于超长列表的情况使用,至于到底多长需要自己取舍。

转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn

下面是代码

/************************** * 文件名:InfinityGridLayoutGroup.cs; * 文件描述:无限滚动GridLayoutGroup,动态创建滚动Item; * 实现无限滚动,需要的最少的child数量。屏幕上能看到的+一行看不到的,比如我在屏幕上能看到 2 行,每一行 2 个。则这个值为 2行*2个 + 1 行* 2个 = 6个。 * 创建日期:2016/05/31; * Author:ThisisGame; * Page:https://github.com/ThisisGame/InfinityGridLayoutGroup ***************************/using UnityEngine;using System.Collections;using UnityEngine.UI;using System.Collections.Generic;[RequireComponent(typeof(GridLayoutGroup))][RequireComponent(typeof(ContentSizeFitter))]public class InfinityGridLayoutGroup : MonoBehaviour {    [SerializeField]    int minAmount = 0;//实现无限滚动,需要的最少的child数量。屏幕上能看到的+一行看不到的,比如我在屏幕上能看到 2 行,每一行 2 个。则这个值为 2行*2个 + 1 行* 2个 = 6个。    RectTransform rectTransform;    GridLayoutGroup gridLayoutGroup;    ContentSizeFitter contentSizeFitter;    ScrollRect scrollRect;    List<RectTransform> children=new List<RectTransform>();    Vector2 startPosition;    int amount = 0;    public delegate void UpdateChildrenCallbackDelegate(int index, Transform trans);    public UpdateChildrenCallbackDelegate updateChildrenCallback = null;    int realIndex = -1;    int realIndexUp = -1; //从下往上;    bool hasInit = false;    Vector2 gridLayoutSize;    Vector2 gridLayoutPos;    Dictionary<Transform, Vector2> childsAnchoredPosition = new Dictionary<Transform, Vector2>();    Dictionary<Transform, int> childsSiblingIndex = new Dictionary<Transform, int>(); // Use this for initialization void Start ()    {        //StartCoroutine(InitChildren()); }    IEnumerator InitChildren()    {        yield return 0;        if (!hasInit)        {            //获取Grid的宽度;            rectTransform = GetComponent<RectTransform>();            gridLayoutGroup = GetComponent<GridLayoutGroup>();            gridLayoutGroup.enabled = false;            contentSizeFitter = GetComponent<ContentSizeFitter>();            contentSizeFitter.enabled = false;            gridLayoutPos = rectTransform.anchoredPosition;            gridLayoutSize = rectTransform.sizeDelta;                        //注册ScrollRect滚动回调;            scrollRect = transform.parent.GetComponent<ScrollRect>();            scrollRect.onValueChanged.AddListener((data) => { ScrollCallback(data); });            //获取所有child anchoredPosition 以及 SiblingIndex;            for (int index = 0; index < transform.childCount; index++)            {                Transform child=transform.GetChild(index);                RectTransform childRectTrans= child.GetComponent<RectTransform>(); 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值