Advertisement
COBO90

Testing Scroll Bug

Feb 6th, 2020
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEditor;
  3. using UnityEngine;
  4.  
  5. // This class must be placed in an Editor Folder
  6. public class TestScrollBugWindow : EditorWindow
  7. {
  8.     List<string> m_list;
  9.     Vector2 m_scrollPosition;
  10.  
  11.     const int ITEMS_COUNT = 1000;
  12.     readonly float ITEM_HEIGHT = EditorGUIUtility.singleLineHeight;
  13.  
  14.     [MenuItem("Window/Test Scroll Bug")]
  15.     internal static void Open()
  16.     {
  17.         TestScrollBugWindow window = GetWindow<TestScrollBugWindow>();
  18.         window.titleContent = new GUIContent("TestScrollBug");
  19.         window.Show();
  20.         window.Focus();
  21.     }
  22.  
  23.     void OnEnable()
  24.     {
  25.         m_list = new List<string>(ITEMS_COUNT);
  26.         for (int i = 0; i < ITEMS_COUNT; i++)
  27.             m_list.Add(i.ToString());
  28.     }
  29.  
  30.     void OnGUI()
  31.     {
  32.         Rect positionRect = new Rect(0, 0, position.width, position.height);
  33.  
  34.         // calculate view rect
  35.         Rect viewRect = positionRect;
  36.         viewRect.height = 16 * m_list.Count;
  37.         viewRect.width -= 16; // rest the vertical scroll bar width to not to show the
  38.  
  39.         m_scrollPosition = GUI.BeginScrollView(positionRect, m_scrollPosition, viewRect, false, true);
  40.         {
  41.             float positionY = 0; // used to calculate the y position of each key
  42.             for (int i = 0; i < m_list.Count; i++)
  43.             {
  44.                 Rect rect = new Rect(0, positionY, viewRect.width, EditorGUIUtility.singleLineHeight);
  45.                 positionY += ITEM_HEIGHT;
  46.  
  47.                 if (ElementRectOverlapsWithScrollViewRect(rect, positionRect))
  48.                 {
  49.                     // DelayedTextField copy the text
  50.                     m_list[i] = EditorGUI.DelayedTextField(rect, m_list[i]);
  51.  
  52.                     // TextField do not copy the text
  53.                     // m_list[i] = EditorGUI.TextField(rect, m_list[i]);
  54.  
  55.                     // log to the console the names of the drawn elements to see if are equal to the names visible in the window
  56.                     // Debug.Log("drawn: " + m_list[i]);
  57.                 }
  58.             }
  59.         }
  60.         GUI.EndScrollView();
  61.     }
  62.  
  63.     /// <summary> Calculate the global rect for the element and returns true if it overlaps with the scrollPositionRect. </summary>
  64.     bool ElementRectOverlapsWithScrollViewRect(Rect elementRect, Rect scrollPositionRect)
  65.     {
  66.         Rect globalRect = elementRect;
  67.         globalRect.y -= m_scrollPosition.y; // rest the scroll position
  68.  
  69.         return scrollPositionRect.Overlaps(globalRect);
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement