NGUI学习笔记(二):UIWrapCotent实现循环滚动(优化排行榜等效果)
2017-07-02 18:41
776 查看
通常我们使用NGUI的UIScrollView时,其内部挂载的子项并不会太多,但如果几百个甚至上千个,那么把它们统统实例化出来同时放到场景中便是很麻瓜的.
能不能让滚出ScrollView的Item重新被我们利用显示新的内容呢?
NGUI作者早就想到了这个问题.只需要重写UIWrapCotent.cs的UpdateItem函数就可以了.
先看一下目标效果(只用了6个Item哟):
![](https://img-blog.csdn.net/20170702184741105?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjYyNzYwOTc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
下面来搭建基础场景:
![](https://img-blog.csdn.net/20170702185121773?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjYyNzYwOTc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
新建一个脚本,把它挂到场景中的Container上:
注意这里的设定:
![](https://img-blog.csdn.net/20170702183635055?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjYyNzYwOTc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
CullContent作者的解释:
Whether the content will be automatically culled. Enabling this will improve performance in scroll views that contain a lot of items.
最后一处设定:
![](https://img-blog.csdn.net/20170702183831716?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjYyNzYwOTc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
注意设定的初始Value为0,并且Direction为Top To Bottom
能不能让滚出ScrollView的Item重新被我们利用显示新的内容呢?
NGUI作者早就想到了这个问题.只需要重写UIWrapCotent.cs的UpdateItem函数就可以了.
先看一下目标效果(只用了6个Item哟):
下面来搭建基础场景:
新建一个脚本,把它挂到场景中的Container上:
//---------------------------------------------- // NGUI: Next-Gen UI kit // Copyright © 2011-2015 Tasharen Entertainment //---------------------------------------------- using UnityEngine; using System.Collections.Generic; /// <summary> /// This script makes it possible for a scroll view to wrap its content, creating endless scroll views. /// Usage: simply attach this script underneath your scroll view where you would normally place a UIGrid: /// /// + Scroll View /// |- UIWrappedContent /// |-- Item 1 /// |-- Item 2 /// |-- Item 3 /// </summary> [AddComponentMenu("NGUI/Custom/Wrap Content")] public class UICustomWrapContent : UIWrapContent { protected override void Start () { base.Start (); } /// <summary> /// Want to update the content of items as they are scrolled? Override this function. /// </summary> protected override void UpdateItem (Transform item, int index) { if (onInitializeItem == null) { onInitializeItem += InitializeItem; } else { int realIndex = (mScroll.movement == UIScrollView.Movement.Vertical) ? Mathf.RoundToInt(item.localPosition.y / itemSize): Mathf.RoundToInt(item.localPosition.x / itemSize); onInitializeItem(item.gameObject, index, realIndex); } } void InitializeItem(GameObject go,int index,int realIndex) { //Debug.LogFormat ("go.name =={0} index =={1} realIndex == {2}",go.name,index,realIndex); realIndex = Mathf.Abs (realIndex); Transform child = transform.GetChild (index); UILabel rangeLbl = child.FindChild ("Range").GetComponent<UILabel> (); UILabel nameLbl = child.FindChild ("Name").GetComponent<UILabel> (); rangeLbl.text = (realIndex+1).ToString(); nameLbl.text = "社会你"+(index).ToString()+"哥"; } }
注意这里的设定:
CullContent作者的解释:
Whether the content will be automatically culled. Enabling this will improve performance in scroll views that contain a lot of items.
最后一处设定:
注意设定的初始Value为0,并且Direction为Top To Bottom
相关文章推荐
- JavaScript实现走马灯效果[无缝连接、循环滚动] 向左
- Scroll text - JS实现文字自动循环滚动效果
- jquery.blockUI.js实现上传滚动等待效果
- jquery实现的向上无缝循环滚动的新闻效果
- 利用Scrip实现走马灯效果[无缝连接、循环滚动]
- JavaScript实现走马灯效果[无缝连接、循环滚动] 向右
- 用DIV+javascript实现首尾相连循环滚动效果(兼容firefox)
- 用marquee和div+js实现首尾相连循环滚动效果
- jquery.blockUI.js上传滚动等待效果实现思路及代码
- android ViewPager实现循环滚动效果
- JavaScript实现走马灯效果[无缝连接、循环滚动] 向下
- div+js实现首尾相连循环滚动效果
- 分别用marquee和div+js实现首尾相连循环滚动效果,仅3行代码
- Android UI控件之Gallery实现循环效果总结
- JavaScript实现走马灯效果[无缝连接、循环滚动] 向上
- JavaScript实现走马灯效果,上 下 左 右无缝连接、循环滚动
- 分别用marquee和div+js实现首尾相连循环滚动效果
- js + div 实现无缝隙滚动效果,从右向左循环显示
- 利用Scrip实现走马灯效果[无缝连接、循环滚动]
- JavaScript实现走马灯效果[无缝连接、循环滚动]