因为在界面中大量使用了 Outline 以及界面复杂度较高,在邮件 等需要超长滚动列表的界面,就会十分卡顿。既然美术与策划同学不能妥协,那就来优化代码。
按照滚动区域的属性,展示给玩家看的只是一小块区域,比如列表中有100封邮件,其实只要显示个七八封就够了,剩下的,在向上滑动的时候,把第一排移动到最后一排,替换内容。
这样做能提高绘制效率,但是因为是在滑动的时候动画创建,所以会稍有卡顿,所以只适用于超长列表的情况使用,至于到底多长需要自己取舍。
转自http://blog.youkuaiyun.com/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>();