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); } } } } } } }
原文:http://www.cnblogs.com/jiangjieqim/p/4817470.html