首页 > 其他 > 详细

ScrollView优化

时间:2015-09-17 21:34:57      阅读:177      评论:0      收藏:0      [点我收藏+]
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class OptimizeScrollView
{
    static List<int> ids = new List<int>();
    static int _startIndex;
    static int _endIndex;
    /*  优化ScrollView以外的item对象,隐藏之 更新list
     *
     *  
     *  constraint  是否是强制更新
     */
    public static void UpdateChange(UIScrollView uiscrollView, float startY, bool constraint = false)
    {
        UIGrid grid = uiscrollView.transform.FindChild("Grid").GetComponent<UIGrid>();
        float panelHeight = uiscrollView.GetComponent<UIPanel>().GetViewSize().y;
        Vector3 vec3 = uiscrollView.transform.localPosition;
        float sub = vec3.y - startY;

        ids.Clear();
        
        for (int i = 0; i < grid.transform.childCount; i++)
        {
            GameObject obj = grid.transform.GetChild(i).gameObject;
            Vector3 pos = obj.transform.localPosition;
            float start = Mathf.Abs(pos.y);
            float end = start + grid.cellHeight;
            if (start >= sub && end <= sub + panelHeight)
            {
                ids.Add(i);
            }
        }

        if (ids.Count > 0)
        {
            int id = ids[0];
            int startIndex = id;
            if (id > 0)
            {
                startIndex = id - 1;
            }

            id = ids[ids.Count - 1];
            int endIndex = id;
            if (id + 1 < grid.transform.childCount)
            {
                endIndex = id + 1;
            }
            if (constraint == false)
            {
                if (_startIndex == startIndex && _endIndex == endIndex)
                {
                    return;
                }
            }

            _startIndex = startIndex;
            _endIndex = endIndex;
            
            for (int i = 0; i < grid.transform.childCount; i++)
            {
                GameObject go = grid.transform.GetChild(i).gameObject;
                if (i == 0 || i == grid.transform.childCount - 1)
                {
                    if (!go.activeSelf)
                    {
                        go.SetActive(true);
                    }
                }
                else
                {
                    if (i >= startIndex && i <= endIndex)
                    {
                        if (!go.activeSelf)
                        {
                            go.SetActive(true);
                        }
                    }
                    else
                    {
                        if (go.activeSelf)
                        {
                            go.SetActive(false);
                        }
                    }
                }
            }
        }
    }
}

 

ScrollView优化

原文:http://www.cnblogs.com/jiangjieqim/p/4817470.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!