Skip to content

Commit 567339d

Browse files
authored
Fix long member values messing up list scrolling in inspector (#100)
1 parent eb04af0 commit 567339d

File tree

4 files changed

+56
-10
lines changed

4 files changed

+56
-10
lines changed

RuntimeUnityEditor.Core/Windows/Inspector/Entries/Contents/CacheEntryBase.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,8 @@ public virtual bool CanEnterValue()
122122
}
123123
return _canEnter.Value;
124124
}
125+
126+
/// <inheritdoc />
127+
public int ItemHeight { get; set; } = Inspector.InspectorRecordInitialHeight;
125128
}
126129
}

RuntimeUnityEditor.Core/Windows/Inspector/Entries/Contents/ICacheEntry.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,9 @@ public interface ICacheEntry
5353
/// Inspector can further inspect the value returned by this member.
5454
/// </summary>
5555
bool CanEnterValue();
56+
/// <summary>
57+
/// Height of the item's row in the inspector's scroll list.
58+
/// </summary>
59+
int ItemHeight { get; set; }
5660
}
5761
}

RuntimeUnityEditor.Core/Windows/Inspector/Entries/Contents/MethodCacheEntry.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ namespace RuntimeUnityEditor.Core.Inspector.Entries
1111
/// </summary>
1212
public class MethodCacheEntry : ICacheEntry
1313
{
14-
/// <inheritdoc />
14+
/// <summary>
15+
/// Creates a new instance of MethodCacheEntry.
16+
/// </summary>
1517
public MethodCacheEntry(object instance, MethodInfo methodInfo, Type owner)
1618
{
1719
Instance = instance;
@@ -110,5 +112,8 @@ internal static string GetParameterPreviewString(MethodBase methodInfo)
110112
/// Not supported for methods.
111113
/// </summary>
112114
public bool CanEnterValue() => false;
115+
116+
/// <inheritdoc />
117+
public int ItemHeight { get; set; } = Inspector.InspectorRecordInitialHeight;
113118
}
114119
}

RuntimeUnityEditor.Core/Windows/Inspector/Inspector.cs

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ namespace RuntimeUnityEditor.Core.Inspector
1313
/// </summary>
1414
public sealed partial class Inspector : Window<Inspector>
1515
{
16-
private const int InspectorRecordHeight = 25;
16+
internal const int InspectorRecordInitialHeight = 25;
1717
private readonly GUILayoutOption[] _inspectorTypeWidth = { GUILayout.Width(170), GUILayout.MaxWidth(170) };
1818
private readonly GUILayoutOption[] _inspectorNameWidth = { GUILayout.Width(240), GUILayout.MaxWidth(240) };
19-
private readonly GUILayoutOption _inspectorRecordHeight = GUILayout.Height(InspectorRecordHeight);
2019

2120
private readonly List<InspectorTab> _tabs = new List<InspectorTab>();
2221
private InspectorTab _currentTab;
@@ -81,7 +80,7 @@ private void DrawVariableNameEnterButton(ICacheEntry field)
8180
_alignedButtonStyle = GUI.skin.button.CreateCopy();
8281
_alignedButtonStyle.alignment = TextAnchor.MiddleLeft;
8382
_alignedButtonStyle.wordWrap = true;
84-
83+
8584
_alignedButtonStyleUnclickable = _alignedButtonStyle.CreateCopy();
8685
_alignedButtonStyleUnclickable.normal.background = null;
8786
_alignedButtonStyleUnclickable.onNormal.background = null;
@@ -408,19 +407,54 @@ private void DrawContentScrollView(InspectorTab tab)
408407
});
409408
var visibleFields = visibleFieldsQuery.ToList();
410409

411-
var firstIndex = (int)(currentItem.ScrollPosition.y / InspectorRecordHeight);
410+
var scrollPositionY = (int)currentItem.ScrollPosition.y;
411+
var scrollMaxVisibleY = scrollPositionY + ((int)WindowRect.height - 130); // TODO conservative value, properly measure at runtime for less overdraw
412+
413+
var topCombinedHeight = 0;
414+
var index = 0;
415+
416+
// Empty space at the top
417+
for (; index < visibleFields.Count; index++)
418+
{
419+
var newHeight = topCombinedHeight + visibleFields[index].ItemHeight;
420+
if (newHeight >= scrollPositionY) break;
421+
else topCombinedHeight = newHeight;
422+
}
412423

413-
GUILayout.Space(firstIndex * InspectorRecordHeight);
424+
if (topCombinedHeight > 0)
425+
GUILayout.Space(topCombinedHeight);
414426

415-
var currentVisibleCount = (int)(WindowRect.height / InspectorRecordHeight) - 4;
416-
for (var index = firstIndex; index < Mathf.Min(visibleFields.Count, firstIndex + currentVisibleCount); index++)
427+
// Actual entries
428+
for (; index < visibleFields.Count; index++)
417429
{
418430
var entry = visibleFields[index];
431+
419432
DrawSingleContentEntry(entry);
433+
434+
topCombinedHeight += entry.ItemHeight;
435+
436+
if (Event.current.type == EventType.Repaint)
437+
{
438+
var measured = (int)GUILayoutUtility.GetLastRect().height;
439+
entry.ItemHeight = Mathf.Max(entry.ItemHeight, measured);
440+
}
441+
442+
if (topCombinedHeight > scrollMaxVisibleY)
443+
{
444+
index++;
445+
break;
446+
}
420447
}
448+
449+
// Empty space at the bottom
450+
var bottomCombinedHeight = 0;
451+
for (; index < visibleFields.Count; index++)
452+
bottomCombinedHeight += visibleFields[index].ItemHeight;
453+
421454
try
422455
{
423-
GUILayout.Space(Mathf.FloorToInt(Mathf.Max(WindowRect.height / 2, (visibleFields.Count - firstIndex - currentVisibleCount) * InspectorRecordHeight)));
456+
var extraSpace = Mathf.FloorToInt(WindowRect.height / 2);
457+
GUILayout.Space(bottomCombinedHeight + extraSpace);
424458
// Fixes layout exploding when searching
425459
GUILayout.FlexibleSpace();
426460
}
@@ -436,7 +470,7 @@ private void DrawContentScrollView(InspectorTab tab)
436470

437471
private void DrawSingleContentEntry(ICacheEntry entry)
438472
{
439-
GUILayout.BeginHorizontal(_inspectorRecordHeight);
473+
GUILayout.BeginHorizontal(GUILayout.Height(entry.ItemHeight));
440474
{
441475
try
442476
{

0 commit comments

Comments
 (0)