using UnityEngine; namespace DebuggingEssentials { public class ScrollViewCullDrawOnce { public bool active = true; public bool recalc; private FastList elements1 = new FastList(); private FastList elements2 = new FastList(); private Vector2 scrollView; private bool eventLayout; private bool eventRepaint; private bool hasCalc; private bool calcHeights; private int startElementCull; private float spaceEnd; private float scrollViewStartHeight; private float scrollViewEndHeight; public void ResetAndRecalc() { recalc = true; scrollView.y = 0f; } public void BeginScrollView(float heightOffset = 0f) { Event current = Event.current; eventLayout = current.type == EventType.Layout; eventRepaint = current.type == EventType.Repaint; if (recalc && eventLayout) { hasCalc = false; recalc = false; elements1.FastClear(); elements2.FastClear(); } startElementCull = 0; calcHeights = !hasCalc && eventRepaint; spaceEnd = 0f; GUILayout.Space(0f); float y = GUILayoutUtility.GetLastRect().y; if (calcHeights) { scrollViewStartHeight = y; } scrollView = GUILayout.BeginScrollView(scrollView); if (!hasCalc || !active) { return; } int num = Helper.BinarySearch(elements2.items, elements2.Count, scrollView.y); float num2 = 0f; float num3 = elements2.items[num]; if (num3 < scrollView.y) { num2 = num3; } else if (num > 0) { num3 = elements2.items[num - 1]; if (num3 < scrollView.y) { num2 = num3; } } if (num2 > 0f) { GUILayout.Space(num2 + 4f); } } public void EndScrollView() { if (hasCalc && active) { GUILayout.Space((spaceEnd != 0f) ? (elements2.items[elements2.Count - 1] - spaceEnd) : 0f); } GUILayout.EndScrollView(); if (calcHeights) { hasCalc = true; } GUILayout.Space(0f); float y = GUILayoutUtility.GetLastRect().y; if (calcHeights) { scrollViewEndHeight = y; } } public bool StartCull() { if (!hasCalc || !active) { return true; } if (startElementCull >= elements1.items.Length) { return true; } float num = elements1.items[startElementCull]; if (elements2.items[startElementCull++] < scrollView.y && active) { return false; } if (num > scrollViewEndHeight - scrollViewStartHeight + scrollView.y && active) { if (spaceEnd == 0f) { spaceEnd = num; } return false; } return true; } public void EndCull() { if (calcHeights) { Rect lastRect = GUILayoutUtility.GetLastRect(); elements1.Add(lastRect.yMin); elements2.Add(lastRect.yMax); } } } }