NGUI学习笔记(二):UIWrapCotent实现循环滚动(优化排行榜等效果)
来源:互联网 发布:ios 去掉数组小括号 编辑:程序博客网 时间:2024/06/17 06:11
通常我们使用NGUI的UIScrollView时,其内部挂载的子项并不会太多,但如果几百个甚至上千个,那么把它们统统实例化出来同时放到场景中便是很麻瓜的.
能不能让滚出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
阅读全文
0 0
- NGUI学习笔记(二):UIWrapCotent实现循环滚动(优化排行榜等效果)
- NGUI 相片循环滚动效果
- NGUI 类皇室战争(CR)的滚动列表效果完整实现(二)
- Egret实现滚动排行榜
- [Unity3d]NGUI- Scroll View实现触摸滚动相册效果
- div+js实现首尾相连循环滚动效果
- android ViewPager实现循环滚动效果
- marquee实现首尾相连循环滚动效果
- CSS实现上下循环滚动效果
- jquery实现文字上下循环滚动效果
- NGUI Scroll View 循环滚动
- NGUI学习笔记(三):内置脚本快速实现标签页效果
- NGUI学习笔记(二)UISprite
- Unity插件NGUI学习笔记(二)
- javaScript实现全屏页面滚动效果(优化)
- NGUI实现翻牌效果
- NGUI实现背包效果
- 循环滚动得效果
- Linux CentOS查看版本的命令
- 干货集中营 API 文档
- 不固定参数求和,arguments
- C++ 继承的构造与析构
- 视图、序列、索引
- NGUI学习笔记(二):UIWrapCotent实现循环滚动(优化排行榜等效果)
- JVM 类装载原理分析-ClassLoader原理分析
- ubuntu/var/log/下各个日志文件
- spring中的BeanFactory与ApplicationContext的作用和区别?
- 数据库-锁
- POJ 2598 Match Throwing Game 笔记
- 问题
- 计算机的原码、反码和补码详解
- 在C++ 程序中调用被 C 编译器编译后的函数,为什么要加 extern “C”声明?