码迷,mamicode.com
首页 > 其他好文 > 详细

UIWrapContent

时间:2015-10-22 17:30:40      阅读:497      评论:0      收藏:0      [点我收藏+]

标签:

在新版本的NGUI 有个组件 UIWrapContent ,当我们的列表内容很多时,可以进行优化。它不是一次生成全部的child,而是生成固定数量的child,在滑动时循环替换child的显示内容。

封装了一个Helper组件简化代码量,UI的结构如下,UIWrapContent、UIPanel、UIScrollView,CUITabGrid(UIGrid) 绑定在同一个Gameobject上,Panel没有裁剪(可选)

技术分享

CUIWrapContentHelper代码如下:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// 对NGUI Wrap Content的封装
/// 
/// 首次要手动Refresh
/// </summary>
public class CUIWrapContentHelper
{
    public delegate void CUIWrapContentRenderDelegate(GameObject obj, int index);

    public CUIWrapContentRenderDelegate RenderFunc;

    private bool _hasRefresh = false;

    private UIWrapContent _wrapContent;
    private UIPanel _panel;
    private Vector2 _initPanelClipOffset;
    private Vector3 _initPanelLocalPos;
    /// <summary>
    /// 缓存起来上次渲染的对象对应索引
    /// </summary>
    private Dictionary<GameObject, int> CacheObjectIndex = new Dictionary<GameObject, int>();

    private CUIWrapContentHelper()
    {
        
    }
    private CUIWrapContentHelper(UIWrapContent nGuiWrapContent)
    {
        _wrapContent = nGuiWrapContent;
        _wrapContent.onInitializeItem = OnInitItem;
        _panel = _wrapContent.gameObject.GetComponent<UIPanel>();
        _initPanelClipOffset = _panel.clipOffset;
        _initPanelLocalPos = _panel.cachedTransform.localPosition;
    }

    public void ResetScroll()
    {
        _panel.clipOffset = _initPanelClipOffset;
        _panel.cachedTransform.localPosition = _initPanelLocalPos;

        // 重设组件~索引和位置
        var index = 0;
        foreach (var oChildTransform in _wrapContent.transform)
        {
            var childTransform = (Transform)oChildTransform;
            childTransform.SetLocalPositionY(-_wrapContent.itemSize*index);  // TODO: 方向、竖横
            CacheObjectIndex[childTransform.gameObject] = index;
            index++;
        }
    }
    private int _count;
    /// <summary>
    /// 设置多少项
    /// </summary>
    /// <param name="count"></param>
    private void SetCount(int count)
    {
        _count = count;
        _wrapContent.minIndex = -count + 1;
        _wrapContent.maxIndex = 0;
        //_wrapContent.SortAlphabetically();

        var scrollView = _panel.GetComponent<UIScrollView>();
        if (scrollView != null)
        {
            var canDrag = _count >= CUIHelper.GetActiveChilds(scrollView.transform).Count;
            if (count <= 1) canDrag = false;// 只有一个的时候,不能滑动
            scrollView.restrictWithinPanel = canDrag;
            scrollView.disableDragIfFits = !canDrag; 
        }

    }
    private void OnInitItem(GameObject go, int wrapindex, int realindex)
    {
        var index = Mathf.Abs(realindex);
        CacheObjectIndex[go] = index; // 取绝对值
        if (!CheckActive(go, index))
            return;

        if (!_hasRefresh)
        {
            return;
        }

        DoRender(go, index);
    }

    /// <summary>
    /// 检查是否应该隐藏起来
    /// </summary>
    /// <param name="go"></param>
    /// <param name="index"></param>
    /// <returns></returns>
    private bool CheckActive(GameObject go, int index)
    {
        if (index > (_count - 1))
        {
            go.SetActive(false);
            return false;
        }
        else
        {
            go.SetActive(true);
            return true;
        }
    }
    private void DoRender(GameObject go, int index)
    {
        if (RenderFunc == null)
        {
            CDebug.LogError("CUIWrapContent必须设置RenderFunc!");
            return;
        }
        RenderFunc(go, index);
    }

    public void Refresh(int count)
    {
        SetCount(count);
        foreach (var kv in CacheObjectIndex)
        {
            if (CheckActive(kv.Key, kv.Value))
                DoRender(kv.Key, kv.Value);
        }
        _hasRefresh = true;
    }

    public static CUIWrapContentHelper Create(UIWrapContent nGuiWrapContent)
    {
        return new CUIWrapContentHelper(nGuiWrapContent);
    }
}
使用方法如下
using System;
using System.Collections.Generic;
using Umeng;
using UnityEngine;
using System.Collections;

/// <summary>
/// 
/// 
/// </summary>
public class CUIFriendList : CUINavController
{
    private UIWrapContent WrapContent;

    private CUIWrapContentHelper WrapContentHelper;
    private List<CPartnerVo> CachePartnerVoList;
    
    //初始化
    public override void OnInit()
    {
        base.OnInit();
        WrapContent = GetControl<UIWrapContent>("ListPanel");
        WrapContentHelper = CUIWrapContentHelper.Create(WrapContent);
        WrapContentHelper.RenderFunc = OnRenderWrapContent;
        
    }

    //界面打开前播放动画
    public override void BeforeShowTween(object[] onOpenArgs, System.Action doNext)
    {
        //重设Scrollview的位置
        WrapContentHelper.ResetScroll();
        RefreshUI();

        //其它的逻辑


        base.BeforeShowTween(onOpenArgs, doNext);
    }

    //刷新列表
    private void RefreshUI()
    {
        var max = CachePartnerVoList.Count;//要渲染数据
        WrapContentHelper.Refresh(max);
    }


    private void OnRenderWrapContent(GameObject gameObj, int idx)
    {
        var partnerVo = CachePartnerVoList[idx];
        var trans = gameObj.transform;
        //执行具体的渲染逻辑
    }
}

UIWrapContent

标签:

原文地址:http://www.cnblogs.com/zhaoqingqing/p/4901393.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!