测试环境


操作系统:Windows8.1

开发工具:Unity5.5.2


1、问题描述,在实际开发过程中经常会使用ScrollRect实现滚动列表,当初次加载数据比较多的情形时,Unity3D会出现比较严重的卡顿,降低帧率,其原因主要为 a、集中式的申请ItemRenderer对象大量堆内存,b、帧Draw Call增加。

2、解决方案,主要逻辑根据Viewport即Mask Visual区域计算当前上下文显示ItemRenderer个数,同时滚动的时候会动态计算scrollLineIndex行数,来重新计算每一个ItemRenderer渲染的位置,从而复用ItemRenderer。

   如图所示:当前数据已经有31个,但是ItemRenderer的实例只有21个,即当前满屏情况下最大的显示个数。

3、完成代码

UIWrapItem 用来作为数据、ItemRenderer prefab的 具体关联类。

using UnityEngine;

/// <summary>
/// Wrapper Item for user data index.
/// </summary>
public class UIWrapItem : MonoBehaviour { /// <summary>
/// User data index
/// </summary>
private int dataIndex = ; /// <summary>
/// Item container
/// </summary>
private UIWrapContainer container = null; private void OnDestroy()
{
container = null;
} /// <summary>
/// Container setter
/// </summary>
public UIWrapContainer Container
{
set
{
container = value;
}
} /// <summary>
/// DataIndex getter & setter
/// </summary>
public int DataIndex
{
set
{
dataIndex = value; gameObject.name = dataIndex.ToString(); if (dataIndex >= )
{
transform.localPosition = container.GetLocalPositionByDataIndex(dataIndex); if (container.onInitializeItem != null)
{
container.onInitializeItem(gameObject, dataIndex);
}
}
} get { return dataIndex; }
}
}

UIWrapContainer作用为UIWrapItem的容器,提供基本的数据判定逻辑。

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic; /// <summary>
/// This script makes it possible for a scroll view to wrap its item, creating scroll views.
/// Usage: simply attach this script underneath your scroll view where you would normally place a container:
///
/// + Scroll View
/// |- UIWrapContainer
/// |-- Item 1
/// |-- Item 2
/// |-- Item 3
/// </summary>
[DisallowMultipleComponent]
public class UIWrapContainer : MonoBehaviour
{
public delegate void OnInitializeItem(GameObject go, int dataIndex); public OnInitializeItem onInitializeItem = null; public enum Arraygement
{
Horizontal,
Vertical
} #region public variables /// <summary>
/// Type of arragement
/// </summary>
public Arraygement arrangement = Arraygement.Horizontal; /// <summary>
/// Maximum item per line.
/// If the arrangement is horizontal, this denotes the number of columns.
/// If the arrangement is vertical, this stands for the number of rows.
/// </summary>
public int maxPerLine = ; /// <summary>
/// The width of each of the items.
/// </summary>
public float itemWidth = 100f; /// <summary>
/// The height of each of the items.
/// </summary>
public float itemHeight = 100f; /// <summary>
/// The Horizontal space of each of the items.
/// </summary>
public float itemHorizontalSpace = 0f; /// <summary>
/// The vertical space of each of the items.
/// </summary>
public float itemVerticalSpace = 0f; public ScrollRect scrollRect = null; public RectTransform viewport = null; public RectTransform container = null; public GameObject itemPrefab = null; #endregion #region private variables private int dataCount = ; private int maximumVisualVerticalItemCount = ; private int maximumVisualHorizontalItemCount = ; private int currentScrollLineIndex = -; private IList<UIWrapItem> activeItems = null; private Queue<UIWrapItem> deactiveItems = null; #endregion void Awake()
{
activeItems = new List<UIWrapItem>();
deactiveItems = new Queue<UIWrapItem>(); maximumVisualVerticalItemCount = Mathf.CeilToInt(viewport.rect.height / (itemHeight + itemVerticalSpace));
maximumVisualHorizontalItemCount = Mathf.CeilToInt(viewport.rect.width / (itemWidth + itemHorizontalSpace));
} void Start()
{ } public void Initialize(int dataCount)
{
if (scrollRect == null || container == null || itemPrefab == null)
{
Debug.LogError("Not attach scrollRect or container or itemPrefab instance here, please check.");
return;
} if (dataCount <= )
{
return;
} setDataCount(dataCount); scrollRect.onValueChanged.RemoveAllListeners();
scrollRect.onValueChanged.AddListener(OnValueChanged); deactiveItems.Clear();
activeItems.Clear(); UpdateItems();
} public void RemoveItem(int dataIndex)
{
if (dataIndex < || dataIndex >= dataCount)
{
return;
} bool isDestroy = activeItems.Count >= dataCount;
setDataCount(dataCount - ); for (int index = activeItems.Count - ; index >= ; index--)
{
UIWrapItem item = activeItems[index];
int itemDataIndex = item.DataIndex; if (itemDataIndex == dataIndex)
{
activeItems.Remove(item);
if (isDestroy)
{
GameObject.Destroy(item.gameObject);
}
else
{
item.DataIndex = -;
item.gameObject.SetActive(false);
deactiveItems.Enqueue(item);
}
} if (itemDataIndex > dataIndex)
{
item.DataIndex = itemDataIndex - ;
}
} UpdateItems(GetCurrentScrollLineIndex());
} public void AddItem(int dataIndex)
{
if (dataIndex < || dataIndex > dataCount)
{
return;
} bool isNew = false;
for (int index = activeItems.Count - ; index >= ; index--)
{
UIWrapItem item = activeItems[index];
if (item.DataIndex >= (dataCount - ))
{
isNew = true;
break;
}
} setDataCount(dataCount + ); if (isNew)
{
for (int index = , length = activeItems.Count; index < length; index++)
{
UIWrapItem item = activeItems[index];
int itemDataIndex = item.DataIndex;
if (itemDataIndex >= dataIndex)
{
item.DataIndex = itemDataIndex + ;
}
} UpdateItems(GetCurrentScrollLineIndex());
}
else
{
for (int index = , length = activeItems.Count; index < length; index++)
{
UIWrapItem item = activeItems[index];
int itemDataIndex = item.DataIndex;
if (itemDataIndex >= dataIndex)
{
item.DataIndex = itemDataIndex;
}
}
}
} public Vector3 GetLocalPositionByDataIndex(int dataIndex)
{
float x = 0f;
float y = 0f;
float z = 0f; switch (arrangement)
{
case Arraygement.Horizontal:
{
x = (dataIndex / maxPerLine) * (itemWidth + itemHorizontalSpace);
y = -(dataIndex % maxPerLine) * (itemHeight + itemVerticalSpace);
break;
}
case Arraygement.Vertical:
{
x = (dataIndex % maxPerLine) * (itemWidth + itemHorizontalSpace);
y = -(dataIndex / maxPerLine) * (itemHeight + itemVerticalSpace);
break;
}
} return new Vector3(x, y, z);
} private void setDataCount(int count)
{
if (count != dataCount)
{
dataCount = count;
UpdateContainerSize();
}
} private void OnValueChanged(Vector2 value)
{
switch (arrangement)
{
case Arraygement.Vertical:
{
float y = value.y;
if (y >= 1.0f || y <= 0.0f)
{
return;
}
break;
}
case Arraygement.Horizontal:
{
float x = value.x;
if (x <= 0.0f || x >= 1.0f)
{
return;
}
break;
}
} int scrollPerLineIndex = GetCurrentScrollLineIndex(); if (scrollPerLineIndex == currentScrollLineIndex) { return; } UpdateItems(scrollPerLineIndex);
} private void UpdateItems(int scrollLineIndex)
{
if (scrollLineIndex < )
{
return;
} currentScrollLineIndex = scrollLineIndex; int startDataIndex = currentScrollLineIndex * maxPerLine;
int endDataIndex = (currentScrollLineIndex + (arrangement == Arraygement.Vertical ? maximumVisualVerticalItemCount : maximumVisualHorizontalItemCount)) * maxPerLine; for (int index = activeItems.Count - ; index >= ; index--)
{
UIWrapItem item = activeItems[index];
int itemDataIndex = item.DataIndex;
if (itemDataIndex < startDataIndex || itemDataIndex >= endDataIndex)
{
item.DataIndex = -;
activeItems.Remove(item);
item.gameObject.SetActive(false);
deactiveItems.Enqueue(item);
}
} for (int dataIndex = startDataIndex; dataIndex < endDataIndex; dataIndex++)
{
if (dataIndex >= dataCount)
{
continue;
} if (Exists(dataIndex))
{
continue;
} CreateItem(dataIndex);
}
} private void CreateItem(int dataIndex)
{
UIWrapItem item = null;
if (deactiveItems.Count > )
{
item = deactiveItems.Dequeue();
item.gameObject.SetActive(true);
}
else
{
item = AddChild(itemPrefab, container).AddComponent<UIWrapItem>();
} item.Container = this;
item.DataIndex = dataIndex;
activeItems.Add(item);
} private bool Exists(int dataIndex)
{
if (activeItems == null || activeItems.Count <= )
{
return false;
} for (int index = , length = activeItems.Count; index < length; index++)
{
if (activeItems[index].DataIndex == dataIndex)
{
return true;
}
} return false;
} private GameObject AddChild(GameObject prefab, Transform parent)
{
if (prefab == null || parent == null)
{
Debug.LogError("Could not add child with null of prefab or parent.");
return null;
} GameObject go = GameObject.Instantiate(prefab) as GameObject;
go.layer = parent.gameObject.layer;
go.transform.SetParent(parent, false); return go;
} private void OnDestroy()
{
scrollRect = null;
container = null;
itemPrefab = null;
onInitializeItem = null; activeItems.Clear();
deactiveItems.Clear(); activeItems = null;
deactiveItems = null;
}
}

4、未完待续

实际在开中主要使用数组集合作为参数及动态的Add or Rmove操作,所以接下来将会迭代掉采用数组下标的方式提供数据的初始化,增加删除等操作。

最后实现仓促难免存在bug,请与指正。

UGUI ScrollRect 性能优化的更多相关文章

  1. UGUI batch 规则和性能优化

    UGUI batch 规则和性能优化 (基础) Unity 绘图性能优化 - Draw Call Batching : http://docs.unity3d.com/Manual/DrawCallB ...

  2. UGUI性能优化

    http://www.cnblogs.com/suoluo/p/5417152.html http://blog.csdn.net/uwa4d/article/details/54344423 htt ...

  3. [转] 擎天哥as3教程系列第二回——性能优化

    所谓性能优化主要是让游戏loading和运行的时候不卡. 一  优化fla导出的swf的体积? 1,  在flash中,舞台上的元件最多,生成的swf越大,库里面有连接名的元件越多,swf越大.当舞台 ...

  4. Unity技术支持团队性能优化经验分享

    https://mp.weixin.qq.com/s?__biz=MzU5MjQ1NTEwOA==&mid=2247490321&idx=1&sn=f9f34407ee5c5d ...

  5. U3D开发性能优化笔记(待增加版本.x)

    http://blog.csdn.net/kaitiren/article/details/45071997 此总结由自己经验及网上收集整理优化内容 包括: .代码方面: .函数使用方面: .ui注意 ...

  6. Unity 性能优化(力荐)

    开始之前先分享几款性能优化的插件: 1.SimpleLOD : 除了同样拥有Mesh Baker所具有的Mesh合并.Atlas烘焙等功能,它还能提供Mesh的简化,并对动态蒙皮网格进行了很好的支持. ...

  7. 01.SQLServer性能优化之----强大的文件组----分盘存储

    汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 文章内容皆自己的理解,如有不足之处欢迎指正~谢谢 前天有学弟问逆天:“逆天,有没有一种方 ...

  8. 03.SQLServer性能优化之---存储优化系列

    汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 概  述:http://www.cnblogs.com/dunitian/p/60413 ...

  9. Web性能优化:What? Why? How?

    为什么要提升web性能? Web性能黄金准则:只有10%~20%的最终用户响应时间花在了下载html文档上,其余的80%~90%时间花在了下载页面组件上. web性能对于用户体验有及其重要的影响,根据 ...

随机推荐

  1. String类的实现(4)写时拷贝浅析

    由于释放内存空间,开辟内存空间时花费时间,因此,在我们在不需要写,只是读的时候就可以不用新开辟内存空间,就用浅拷贝的方式创建对象,当我们需要写的时候才去新开辟内存空间.这种方法就是写时拷贝.这也是一种 ...

  2. Excel公式-求最低价网站名字

    p{ font-size: 15px; } .alexrootdiv>div{ background: #eeeeee; border: 1px solid #aaa; width: 99%; ...

  3. Hexo + Github Pages 搭建个人博客

    之前一直想搭建自己的博客,由于自己的懒惰拖到现在.好了,废话不多说!直接上干货! 安装Node.js 安装Git Github Pages配置 安装Hexo及主题设置 进阶篇-高级定制 其它实用功能 ...

  4. Python爬虫 Urllib库的高级用法

    1.设置Headers 有些网站不会同意程序直接用上面的方式进行访问,如果识别有问题,那么站点根本不会响应,所以为了完全模拟浏览器的工作,我们需要设置一些Headers 的属性. 首先,打开我们的浏览 ...

  5. ubuntu中文字符集格式转换

  6. 实现一个竖直的显示表头的表格(vue版本)

    今天遇到一个问题,实现这样一个竖直的显示表头的表格,如下图.默认显示两列. vue实现代码如下:   tableComponent.vue:   <template> <table ...

  7. Java常用的八种排序算法与代码实现

    1.直接插入排序 经常碰到这样一类排序问题:把新的数据插入到已经排好的数据列中. 将第一个数和第二个数排序,然后构成一个有序序列 将第三个数插入进去,构成一个新的有序序列. 对第四个数.第五个数--直 ...

  8. 第一个SignalR案例

    说明:开发的案例为Hub(集线器) 一.开发环境 VS2013  ,window10 二.步骤 打开vs创建一个新的解决方案,添加一个空的WebForm项目. 使用NuGet添加引用.命令:PM> ...

  9. Ubuntu搭建mysql,Navicat Premium连接

    保存编辑结果与退出vim编辑器 https://jingyan.baidu.com/article/495ba8410ff14d38b30ede01.html 首先,我们需要使用apt安装mysql, ...

  10. java设计模式--基础思想总结--父类引用操作对象

    看设计模式的相关书籍也有一段时间了,一开始其实是抱着作为java三大框架的基础知识储备来学习的,不过到后来,才发现,在设计模式的一些准则装饰下,java的面向对象威力才真正地体现出来,后面的将会陆续地 ...