Skip to content

Commit c9ad6b7

Browse files
committed
Fixed log scroll view not snapping to bottom intuitively.
1 parent c84be0c commit c9ad6b7

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- Changed DevConsole.InvokeCoroutine method to return the Coroutine instance.
1818
- Fixed Unity logs from other threads not showing in the developer console log.
1919
- Fixed generic or nullable parameter types not having their type displayed nicely.
20+
- Fixed log scroll view not snapping to bottom intuitively.
2021

2122
## [0.2.1-alpha] - 2021-08-09
2223
- Added documentation.

Runtime/DevConsoleMono.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ internal sealed class DevConsoleMono : MonoBehaviour
155155
private readonly TextGenerator _textGenerator = new TextGenerator();
156156
private int _vertexCount = 0;
157157
private int _initLogTextSize = 0;
158+
private bool _pretendScrollAtBottom = false;
158159

159160
#endregion
160161

@@ -382,6 +383,7 @@ internal void ClearConsole()
382383
ClearLogFields();
383384
_vertexCount = 0;
384385
StoredLogText = ClearLogText;
386+
_pretendScrollAtBottom = true;
385387
}
386388

387389
internal void ResetConsole()
@@ -397,7 +399,7 @@ internal void SubmitInput()
397399
{
398400
if (!string.IsNullOrWhiteSpace(InputText) && RunCommand(InputText))
399401
{
400-
ScrollToBottomAtEndOfFrame();
402+
ScrollTo(0.0f);
401403
}
402404

403405
InputText = string.Empty;
@@ -887,9 +889,12 @@ private void LateUpdate()
887889
// Process the stored logs, displaying them to the console
888890
if (StoredLogText != string.Empty)
889891
{
892+
bool scrollToBottom = _logScrollView.verticalNormalizedPosition < 0f || Mathf.Approximately(_logScrollView.verticalNormalizedPosition, 0f);
893+
float scrollTo = scrollToBottom || _pretendScrollAtBottom ? 0.0f : _logScrollView.verticalNormalizedPosition;
890894
string logText = string.Copy(StoredLogText);
891895
StoredLogText = string.Empty;
892896
ProcessLogText(logText);
897+
ScrollTo(scrollTo);
893898
}
894899

895900
// Check if the developer console toggle key was pressed
@@ -2635,17 +2640,21 @@ private void RebuildLayout()
26352640
LayoutRebuilder.ForceRebuildLayoutImmediate(_logContentTransform);
26362641
}
26372642

2638-
private void ScrollToBottomAtEndOfFrame()
2643+
private void ScrollTo(float vertical)
26392644
{
2640-
IEnumerator ScrollToBottomCoroutine()
2645+
IEnumerator ScrollTo()
26412646
{
26422647
yield return new WaitForEndOfFrame();
2643-
_logScrollView.verticalNormalizedPosition = 0f;
2644-
_logScrollView.CalculateLayoutInputVertical();
2648+
_logScrollView.verticalNormalizedPosition = vertical;
2649+
LayoutRebuilder.ForceRebuildLayoutImmediate((RectTransform)_logScrollView.transform);
2650+
if (_pretendScrollAtBottom && (_logScrollView.verticalNormalizedPosition < 0f || Mathf.Approximately(_logScrollView.verticalNormalizedPosition, 0f)))
2651+
{
2652+
_pretendScrollAtBottom = false;
2653+
}
26452654
}
26462655

26472656
// Start the coroutine that snaps the scroll view at the end of the frame
2648-
StartCoroutine(ScrollToBottomCoroutine());
2657+
StartCoroutine(ScrollTo());
26492658
}
26502659

26512660
#endregion

0 commit comments

Comments
 (0)