Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEditor;
- using UnityEngine;
- // This class must be placed in an Editor Folder
- public class TestScrollBugWindow : EditorWindow
- {
- List<string> m_list;
- Vector2 m_scrollPosition;
- const int ITEMS_COUNT = 1000;
- readonly float ITEM_HEIGHT = EditorGUIUtility.singleLineHeight;
- [MenuItem("Window/Test Scroll Bug")]
- internal static void Open()
- {
- TestScrollBugWindow window = GetWindow<TestScrollBugWindow>();
- window.titleContent = new GUIContent("TestScrollBug");
- window.Show();
- window.Focus();
- }
- void OnEnable()
- {
- m_list = new List<string>(ITEMS_COUNT);
- for (int i = 0; i < ITEMS_COUNT; i++)
- m_list.Add(i.ToString());
- }
- void OnGUI()
- {
- Rect positionRect = new Rect(0, 0, position.width, position.height);
- // calculate view rect
- Rect viewRect = positionRect;
- viewRect.height = 16 * m_list.Count;
- viewRect.width -= 16; // rest the vertical scroll bar width to not to show the
- m_scrollPosition = GUI.BeginScrollView(positionRect, m_scrollPosition, viewRect, false, true);
- {
- float positionY = 0; // used to calculate the y position of each key
- for (int i = 0; i < m_list.Count; i++)
- {
- Rect rect = new Rect(0, positionY, viewRect.width, EditorGUIUtility.singleLineHeight);
- positionY += ITEM_HEIGHT;
- if (ElementRectOverlapsWithScrollViewRect(rect, positionRect))
- {
- // DelayedTextField copy the text
- m_list[i] = EditorGUI.DelayedTextField(rect, m_list[i]);
- // TextField do not copy the text
- // m_list[i] = EditorGUI.TextField(rect, m_list[i]);
- // log to the console the names of the drawn elements to see if are equal to the names visible in the window
- // Debug.Log("drawn: " + m_list[i]);
- }
- }
- }
- GUI.EndScrollView();
- }
- /// <summary> Calculate the global rect for the element and returns true if it overlaps with the scrollPositionRect. </summary>
- bool ElementRectOverlapsWithScrollViewRect(Rect elementRect, Rect scrollPositionRect)
- {
- Rect globalRect = elementRect;
- globalRect.y -= m_scrollPosition.y; // rest the scroll position
- return scrollPositionRect.Overlaps(globalRect);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement