NGUI优化ScroolView中Grid子级太多导致的滑动问题
来源:互联网 发布:收银软件交流群 编辑:程序博客网 时间:2024/06/05 17:04
当ScrollView中Grid子级太多的时候,会导致卡顿的效果。通过重写MultiRowWrapContent的函数即可有效解决。只需要在Grid上绑定这一个脚本,填写Item的数量,以及单元格大小,滑动的时候就会自动调整Item位置,来实现完美的显示效果。
using UnityEngine;public class MultiRowWrapContent : UIWrapContent{ /// <summary> /// 根据数据设置大小 /// </summary> /// <param name="showCount">最多显示的大小</param> /// <param name="dataCount">数据长度</param> public void SetCurData(int showCount,int dataCount) { minIndex = 0; maxIndex = dataCount - 1; if (maxIndex < (showCount-1)) { maxIndex = showCount-1; } } public void mFWrapContent() { //itemSizeH = 180; mFirstTime = true; SortBasedOnScrollMovement(); WrapContent(); mFirstTime = false; } protected override void ResetChildPositions() { for (int i = 0, imax = mChildren.Count; i < imax; ++i) { Transform t = mChildren[i]; int x = i * itemSize; int y = 0; if (!mHorizontal) { x = (i % RowNum) * itemSize; y = -(i / RowNum) * itemSizeH; int index = int.Parse(t.gameObject.name.Substring(4,2));//item00 if (index % 4 == 0) Debug.Log(i+t.gameObject.name+": " + y); } t.localPosition = new Vector3(x, y, 0); UpdateItem(t, i); } } protected override void UpdateItem(Transform item, int index) { if (onInitializeItem != null) { int yIndex = Mathf.Abs(Mathf.RoundToInt(item.localPosition.y / itemSize)); int xIndex = Mathf.Abs(Mathf.RoundToInt(item.localPosition.x / itemSize)); int realIndex = RowNum * yIndex + xIndex; onInitializeItem(item.gameObject, index, realIndex); } } public override void SortBasedOnScrollMovement() { if (!CacheScrollView()) return; // Cache all children and place them in order mChildren.Clear(); for (int i = 0; i < mTrans.childCount; ++i) { Transform t = mTrans.GetChild(i); if (hideInactive && !t.gameObject.activeInHierarchy) continue; mChildren.Add(t); } ResetChildPositions(); } public override void WrapContent() { float extents = itemSize * (mChildren.Count / RowNum) * 0.5f; Vector3[] corners = mPanel.worldCorners; for (int i = 0; i < 4; ++i) { Vector3 v = corners[i]; v = mTrans.InverseTransformPoint(v); corners[i] = v; } Vector3 center = Vector3.Lerp(corners[0], corners[2], 0.5f); bool allWithinRange = true; float ext2 = extents * 2f; if (mHorizontal) { float min = corners[0].x - itemSize; float max = corners[2].x + itemSize; for (int i = 0, imax = mChildren.Count; i < imax; ++i) { Transform t = mChildren[i]; float distance = t.localPosition.x - center.x; if (distance < -extents) { Vector3 pos = t.localPosition; pos.x += ext2; distance = pos.x - center.x; int realIndex = Mathf.RoundToInt(pos.x / itemSize); if (minIndex == maxIndex || (minIndex <= realIndex && realIndex <= maxIndex)) { t.localPosition = pos; UpdateItem(t, i); } else allWithinRange = false; } else if (distance > extents) { Vector3 pos = t.localPosition; pos.x -= ext2; distance = pos.x - center.x; int realIndex = Mathf.RoundToInt(pos.x / itemSize); if (minIndex == maxIndex || (minIndex <= realIndex && realIndex <= maxIndex)) { t.localPosition = pos; UpdateItem(t, i); } else allWithinRange = false; } else if (mFirstTime) UpdateItem(t, i); if (cullContent) { distance += mPanel.clipOffset.x - mTrans.localPosition.x; if (!UICamera.IsPressed(t.gameObject)) NGUITools.SetActive(t.gameObject, (distance > min && distance < max), false); } } } else { extents = itemSizeH * (mChildren.Count / RowNum) * 0.5f; ext2 = extents * 2f; float min = corners[0].y - itemSizeH; float max = corners[2].y + itemSizeH; for (int i = 0, imax = mChildren.Count; i < imax; ++i) { Transform t = mChildren[i]; float distance = t.localPosition.y - center.y; if (distance < -extents) { Vector3 pos = t.localPosition; pos.y += ext2; distance = pos.y - center.y; int yIndex = -Mathf.RoundToInt(pos.y / itemSizeH); int xIndex = -Mathf.RoundToInt(pos.x / itemSize); int realIndex = RowNum * yIndex; if (minIndex == maxIndex || (minIndex <= realIndex && realIndex <= maxIndex)) { t.localPosition = pos; UpdateItem(t, i); } else allWithinRange = false; } else if (distance > extents) { Vector3 pos = t.localPosition; pos.y -= ext2; distance = pos.y - center.y; // int realIndex = Mathf.RoundToInt(pos.y / itemSize); // int yIndex = Mathf.Abs(Mathf.RoundToInt(pos.y / itemSize)); // int xIndex = Mathf.Abs(Mathf.RoundToInt(pos.x / itemSize)); int yIndex = -Mathf.RoundToInt(pos.y / itemSizeH); int xIndex = -Mathf.RoundToInt(pos.x / itemSize); int realIndex = RowNum * yIndex; if (minIndex == maxIndex || (minIndex <= realIndex && realIndex <= maxIndex)) { t.localPosition = pos; UpdateItem(t, i); } else allWithinRange = false; } else if (mFirstTime) UpdateItem(t, i); if (cullContent) { distance += mPanel.clipOffset.y - mTrans.localPosition.y; if (!UICamera.IsPressed(t.gameObject)) NGUITools.SetActive(t.gameObject, (distance > min && distance < max), false); } } } mScroll.restrictWithinPanel = !allWithinRange; mScroll.InvalidateBounds(); }}
阅读全文
0 0
- NGUI优化ScroolView中Grid子级太多导致的滑动问题
- 关于scroolView自己慢慢滑动的动画
- scroolView嵌套listview时listView只显示一条item的高,并且还能滑动问题
- 关于WPF的ComboBox中Items太多而导致加载过慢的问题
- 持续集成过程中,临时文件太多导致编译不通过问题的解决方案
- hive产生动态分区太多导致的问题
- label文字太多导致与控件换行的问题
- NGUI UIScrollView - 大量item子项的性能优化
- ngui 的scrollview grid 只有一个子对象时 不会弹回
- 滑动手势导致的子控件是小问题解决
- Android ScroolView不能撑满全屏的问题
- 解决scrollView中嵌套编辑框导致不能上下滑动的问题
- 在ScrollView中嵌套GridView,导致GridView不能滑动的问题
- 解决scrollView中嵌套编辑框导致不能上下滑动的问题
- Excel中一个单元格中写入太多的汉字,导致打印时无法全部显示怎么办?
- ScroolView的使用
- ScroolView的初步了解
- 抄送的收件人太多导致发送失败
- 2.14 unity特效渲染
- FTP,SFTP,FTPS区别
- 一帧之内删除所有子级
- (1) racecar零件购买
- 秒杀系统的架构之道
- NGUI优化ScroolView中Grid子级太多导致的滑动问题
- 现代OpenGL+Qt学习笔记之一:概述
- UVA10534[Wavio Sequence] 动态规划 LIS
- 网易笔试题20170909
- 【拜小白opencv】29-平滑处理2线性滤波之——均值滤波
- Express + Session 实现登录验证
- Ubuntu 16.04搭建LAMP开发环境
- 爬虫自学笔记(Python3.6.1)
- Comparator<String>接口,String类数组按字符串长度排序