Skip to content

Commit bc91f2d

Browse files
committed
Tried to fix scrolling not working intuitively, but Unity hates me.
1 parent 95f0219 commit bc91f2d

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

Runtime/DevConsoleMono.cs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ internal sealed class DevConsoleMono : MonoBehaviour
156156
private int _vertexCount = 0;
157157
private int _initLogTextSize = 0;
158158
private bool _pretendScrollAtBottom = false;
159+
private bool _scrollToBottomNextFrame = false;
159160

160161
#endregion
161162

@@ -399,7 +400,7 @@ internal void SubmitInput()
399400
{
400401
if (!string.IsNullOrWhiteSpace(InputText) && RunCommand(InputText))
401402
{
402-
ScrollTo(0.0f);
403+
_scrollToBottomNextFrame = true;
403404
}
404405

405406
InputText = string.Empty;
@@ -780,6 +781,19 @@ private void Update()
780781
return;
781782
}
782783

784+
// Scroll the view to the bottom
785+
if (_scrollToBottomNextFrame)
786+
{
787+
_logScrollView.verticalNormalizedPosition = 0.0f;
788+
LayoutRebuilder.ForceRebuildLayoutImmediate((RectTransform)_logScrollView.transform);
789+
//_logScrollView.verticalNormalizedPosition = 0.0f;
790+
if (_pretendScrollAtBottom && (_logScrollView.verticalNormalizedPosition < 0f || Mathf.Approximately(_logScrollView.verticalNormalizedPosition, 0f)))
791+
{
792+
_pretendScrollAtBottom = false;
793+
}
794+
_scrollToBottomNextFrame = false;
795+
}
796+
783797
// Check if the resolution has changed and the window should be rebuilt / reset
784798
if (_screenSize.x != Screen.width || _screenSize.y != Screen.height)
785799
{
@@ -889,12 +903,16 @@ private void LateUpdate()
889903
// Process the stored logs, displaying them to the console
890904
if (StoredLogText != string.Empty)
891905
{
892-
bool scrollToBottom = _logScrollView.verticalNormalizedPosition < 0f || Mathf.Approximately(_logScrollView.verticalNormalizedPosition, 0f);
893-
float scrollTo = scrollToBottom || _pretendScrollAtBottom ? 0.0f : _logScrollView.verticalNormalizedPosition;
906+
// Check if should scroll to the bottom (not working - vertical changes between Update() and LateUpdate() - why???!!!)
907+
if (_pretendScrollAtBottom || _logScrollView.verticalNormalizedPosition < 0f || Mathf.Approximately(_logScrollView.verticalNormalizedPosition, 0f))
908+
{
909+
_scrollToBottomNextFrame = true;
910+
}
911+
894912
string logText = string.Copy(StoredLogText);
895913
StoredLogText = string.Empty;
896914
ProcessLogText(logText);
897-
ScrollTo(scrollTo);
915+
RebuildLayout();
898916
}
899917

900918
// Check if the developer console toggle key was pressed
@@ -2590,9 +2608,6 @@ private void ProcessLogText(in string logText)
25902608
_logFields.Last().text += logText;
25912609
_vertexCount += vertexCountStored;
25922610
}
2593-
2594-
// Refresh the UI, so that the text re-positions nicely
2595-
RebuildLayout();
25962611
}
25972612

25982613
private int GetVertexCount(string text)
@@ -2645,23 +2660,6 @@ private void RebuildLayout()
26452660
LayoutRebuilder.ForceRebuildLayoutImmediate(_logContentTransform);
26462661
}
26472662

2648-
private void ScrollTo(float vertical)
2649-
{
2650-
IEnumerator ScrollTo()
2651-
{
2652-
yield return new WaitForEndOfFrame();
2653-
_logScrollView.verticalNormalizedPosition = vertical;
2654-
LayoutRebuilder.ForceRebuildLayoutImmediate((RectTransform)_logScrollView.transform);
2655-
if (_pretendScrollAtBottom && (_logScrollView.verticalNormalizedPosition < 0f || Mathf.Approximately(_logScrollView.verticalNormalizedPosition, 0f)))
2656-
{
2657-
_pretendScrollAtBottom = false;
2658-
}
2659-
}
2660-
2661-
// Start the coroutine that snaps the scroll view at the end of the frame
2662-
StartCoroutine(ScrollTo());
2663-
}
2664-
26652663
#endregion
26662664

26672665
#region Physical input methods

0 commit comments

Comments
 (0)