12 第1页 | 共2 页下一页
返回列表 发新帖
查看: 5909|回复: 10
打印 上一主题 下一主题

[其他] NGUI无限滑动(转载)

[复制链接]

9903

主题

126

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
53464
精华
316

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2014-9-11 01:34:42 |只看该作者 |倒序浏览

最近由于工作需要,就开始研究NGUI滑动。刚开始参考NGUI自带的循环滑动,利用隐藏和显示,提高GPU的渲染,但是效果依旧不是很理想。在同事提醒下,添加缓存列表,不断刷新当前裁剪区域里的数据,最总完成了需求。在网上也参考了很多资料,今天恰好闲下来,就拿出来大家分享下,哈哈。代码附上:
主要分为三部分:


1.重写UIScrollView和UICustomDragScrollView两个脚本

2.核心脚本NGUIDynamicScrollBase ,计算滑动和播放位移动画

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;

  4. /// <summary>
  5. /// 扩展NGUIScroll滑动类,需要继承该类进行开发
  6. /// </summary>
  7. public abstract  class NGUIDynamicScrollBase : MonoBehaviour
  8. {

  9.     //每个列表项数据初始化
  10.     abstract protected void ResetItemData(GameObject go, int index);
  11.     public UIPanel panel;
  12.     public UIGrid grid;
  13.     //目标prefab
  14.     public GameObject prefab;
  15.     //宽度
  16.     public int cellHeight = 60;
  17.     //高度
  18.     public int cellWidth = 700;
  19.     //裁剪区的高度
  20.     private float m_height;
  21.     //裁剪区的宽度
  22.     private int m_maxLine;
  23.     //当前滑动的列表
  24.     protected GameObject[] m_cellList;
  25.     //当前需要滑动的列表总数
  26.     private int m_dataListCount;
  27.     //自定义滑动
  28.     private UICustomScrollView mDrag;
  29.     //最后一次的滑动位置
  30.     private float lastY = -1;
  31.     private Vector3 defaultVec;

  32.     // Use this for initialization
  33.     protected void BaseOnEnable()
  34.     {
  35.         GetConfiguration();
  36.     }

  37.     // Update is called once per frame
  38.     protected void BaseUpdate()
  39.     {
  40.         if (panel.transform.localPosition.y != lastY)
  41.         {
  42.             Validate();
  43.             lastY = panel.transform.localPosition.y;
  44.         }
  45.     }
  46.     //设置当前列表中的
  47.     protected int DataListCount
  48.     {
  49.         get { return m_dataListCount; }

  50.         set{
  51.             m_dataListCount = value;
  52.             
  53.             AddItem(m_dataListCount);
  54.             PlayMoveAnimation(m_cellList);
  55.         }
  56.     }



  57.     #region private Functions

  58.     //初始化配置数据
  59.     private void GetConfiguration()
  60.     {
  61.         //物体默认位置
  62.         defaultVec = new Vector3(0, cellHeight, 0);
  63.         //裁剪区域的高度
  64.         m_height = panel.height;
  65.         //裁剪区域中最多显示的cellItem数量
  66.         m_maxLine = Mathf.CeilToInt(m_height / cellHeight) + 1;
  67.         //初始化CellList
  68.         m_cellList = new GameObject[m_maxLine];
  69.         //创建Item,默认为不可显示状态
  70.         CreateItem();
  71.     }
  72.     //创建Item
  73.     private void CreateItem()
  74.     {
  75.         for (int i = 0; i < m_maxLine; i++)
  76.         {
  77.             GameObject go = null;
  78.             go = (GameObject)Instantiate(prefab);
  79.             go.gameObject.SetActive(false);
  80.             go.GetComponent<UICustomDragScrollView>().scrollView = panel.GetComponent<UICustomScrollView>();
  81.             AddChild(grid.gameObject, go);
  82.             go.transform.localScale = Vector3.one;
  83.             go.transform.localPosition = new Vector3(cellWidth * AllScale.ResolutionScale, -i * cellHeight, 0);
  84.             m_cellList[i] = go;
  85.             go.gameObject.SetActive(false);
  86.         }
  87.     }


  88.     //验证当前区域中的需要显示的CellItem
  89.     private void Validate()
  90.     {
  91.         Vector3 position = panel.transform.localPosition;

  92.         float _ver = Mathf.Max(position.y, 0);

  93.         int startIndex = Mathf.FloorToInt(_ver / cellHeight);

  94.         int endIndex = Mathf.Min(DataListCount, startIndex + m_maxLine);

  95.         GameObject cell;
  96.         int index = 0;
  97.         for (int i = startIndex; i < startIndex + m_maxLine; i++)
  98.         {
  99.             cell = m_cellList[index];

  100.             if (i < endIndex)
  101.             {
  102.                 //开始渲染
  103.                 cell.gameObject.SetActive(true);
  104.                 //重新填充数据
  105.                 ResetItemData(cell, i);
  106.                 //改变位置
  107.                 cell.transform.localPosition = new Vector3(cell.transform.localPosition.x, i * -cellHeight, 0);
  108.                 cell.name = "Item_" + index;
  109.             }
  110.             else
  111.             {
  112.                 cell.transform.localPosition = defaultVec;
  113.               //  cell.gameObject.SetActive(false);
  114.             }

  115.             index++;
  116.         }
  117.     }

  118.     //重新计算包围合的大小
  119.     private void UpdateBounds(int count)
  120.     {
  121.         Vector3 vMin = new Vector3();
  122.         vMin.x = -grid.transform.localPosition.x;
  123.         vMin.y = grid.transform.localPosition.y - count * cellHeight;
  124.         vMin.z = grid.transform.localPosition.z;
  125.         Bounds b = new Bounds(vMin, Vector3.one);
  126.         b.Encapsulate(grid.transform.localPosition);
  127.         if (mDrag == null) mDrag = panel.GetComponent<UICustomScrollView>();
  128.         mDrag.bounds = b;
  129.         mDrag.UpdateScrollbars(true);
  130.         mDrag.RestrictWithinBounds(true);
  131.     }


  132.     //根据新的数量来重新绘制
  133.     private void AddItem(int count)
  134.     {
  135.         Validate();
  136.         UpdateBounds(count);
  137.     }
  138.    
  139.     //增加孩子节点
  140.     void AddChild(GameObject parent, GameObject go)
  141.     {
  142.         Transform t = go.transform;
  143.         t.parent = parent.transform;
  144.         t.localPosition = Vector3.zero;
  145.         t.localRotation = Quaternion.identity;
  146.         t.localScale = Vector3.one;
  147.         go.layer = parent.layer;
  148.     }

  149.     //播放开始加载的位移动画
  150.     void PlayMoveAnimation(GameObject[] list)
  151.     {
  152.         Vector3 to;
  153.         Vector3 from;
  154.         for (int i = 0; i < list.Length; i++)
  155.         {
  156.             from = list[i].transform.localPosition;
  157.             from = new Vector3(cellWidth*AllScale.ResolutionScale, from.y, 0);
  158.             to = new Vector3(0, from.y, 0);
  159.             list[i].transform.localPosition = from;
  160.             TweenPosition tp = TweenPosition.Begin(list[i], 0.8f, to);
  161.             tp.delay = 0.1f;
  162.             tp.from = from;
  163.             tp.to = to;
  164.             tp.duration = (i + 2) * 0.1f;
  165.             tp.method = UITweener.Method.EaseIn;
  166.         }
  167.     }
  168.     #endregion


  169.    
  170. }
复制代码
3.将列表中的Prefab设置为向上对齐

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏1 支持支持0 反对反对0
回复

使用道具 举报

hyui    

1

主题

2

听众

6671

积分

高级设计师

Rank: 6Rank: 6

纳金币
2715
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2014-9-11 02:13:22 |只看该作者
Thanks for this script !
回复

使用道具 举报

0

主题

2

听众

4092

积分

中级设计师

Rank: 5Rank: 5

纳金币
530
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2014-9-11 07:07:58 |只看该作者
感谢分享!
回复

使用道具 举报

33

主题

1

听众

2680

积分

中级设计师

Rank: 5Rank: 5

纳金币
559
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2014-9-11 08:47:38 |只看该作者
感谢分享
回复

使用道具 举报

115

主题

3

听众

5676

积分

高级设计师

Rank: 6Rank: 6

纳金币
7268
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2014-9-11 09:45:34 |只看该作者

感谢分享!
回复

使用道具 举报

0

主题

1

听众

479

积分

设计实习生

Rank: 2

纳金币
282
精华
0

最佳新人

6#
发表于 2014-9-11 10:20:23 |只看该作者
感谢分享
回复

使用道具 举报

3

主题

2

听众

312

积分

设计实习生

Rank: 2

纳金币
360
精华
0

最佳新人

7#
发表于 2014-9-11 15:34:18 |只看该作者
Thanks for sharing !
回复

使用道具 举报

3

主题

2

听众

312

积分

设计实习生

Rank: 2

纳金币
360
精华
0

最佳新人

8#
发表于 2014-9-11 15:42:46 |只看该作者
Thanks for sharing !
回复

使用道具 举报

0

主题

1

听众

58

积分

设计初学者

Rank: 1

纳金币
4
精华
0

活跃会员 灌水之王

9#
发表于 2014-9-12 10:41:54 |只看该作者
感谢分享,非常不错!!!!
回复

使用道具 举报

13

主题

7

听众

3263

积分

中级设计师

Rank: 5Rank: 5

纳金币
216
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

10#
发表于 2014-9-12 18:41:32 |只看该作者
代码不全。。转自哪里?
回复

使用道具 举报

12 第1页 | 共2 页下一页
返回列表 发新帖
您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-23 20:22 , Processed in 0.091721 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部