Skip to content

Commit be7b0a5

Browse files
committed
Scroll view snaps to bottom when a command is entered.
1 parent 3ee4242 commit be7b0a5

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

Resources/Prefabs/FAB_DevConsole.Instance.prefab

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ MonoBehaviour:
399399
_suggestionText: {fileID: 8332604281068276197}
400400
_logFieldPrefab: {fileID: 5715229556425181486}
401401
_logContentTransform: {fileID: 8873818845564592851}
402+
_logScrollView: {fileID: 5535905114958307578}
402403
_dynamicTransform: {fileID: 3759786000585080057}
403404
_resizeButtonImage: {fileID: 2549797857176525141}
404405
_resizeButtonHoverColour: {r: 0.8018868, g: 0.8018868, b: 0.8018868, a: 1}
@@ -1089,7 +1090,7 @@ RectTransform:
10891090
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
10901091
m_AnchorMin: {x: 0, y: 0}
10911092
m_AnchorMax: {x: 0, y: 0}
1092-
m_AnchoredPosition: {x: 421.41385, y: 0}
1093+
m_AnchoredPosition: {x: 0, y: 0}
10931094
m_SizeDelta: {x: 822.8277, y: 0}
10941095
m_Pivot: {x: 0.5, y: 0.5}
10951096
--- !u!222 &7401132262407597909

Runtime/DevConsoleMono.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using UnityEngine.SceneManagement;
1919
using UnityEngine.UI;
2020
using System.Runtime.CompilerServices;
21+
using System.Collections;
2122
#if INPUT_SYSTEM_INSTALLED
2223
using UnityEngine.InputSystem;
2324
using UnityEngine.InputSystem.UI;
@@ -86,6 +87,7 @@ internal sealed class DevConsoleMono : MonoBehaviour
8687
[Header("Logs")]
8788
[SerializeField] private GameObject _logFieldPrefab = null;
8889
[SerializeField] private RectTransform _logContentTransform = null;
90+
[SerializeField] private ScrollRect _logScrollView = null;
8991

9092
[Header("Window")]
9193
[SerializeField] private RectTransform _dynamicTransform = null;
@@ -282,9 +284,9 @@ internal void ClearConsole()
282284

283285
internal void SubmitInput()
284286
{
285-
if (!string.IsNullOrWhiteSpace(InputText))
287+
if (!string.IsNullOrWhiteSpace(InputText) && RunCommand(InputText))
286288
{
287-
RunCommand(InputText);
289+
ScrollToBottom();
288290
}
289291

290292
InputText = string.Empty;
@@ -1709,13 +1711,24 @@ private void RefreshLogFieldsSize()
17091711
RebuildLayout();
17101712
}
17111713

1712-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
17131714
private void RebuildLayout()
17141715
{
17151716
// Forcefully rebuild the layout, otherwise transforms are positioned incorrectly
17161717
LayoutRebuilder.ForceRebuildLayoutImmediate(_logContentTransform);
17171718
}
17181719

1720+
private void ScrollToBottom()
1721+
{
1722+
IEnumerator ScrollToBottomCoroutine()
1723+
{
1724+
yield return new WaitForEndOfFrame();
1725+
_logScrollView.verticalNormalizedPosition = 0f;
1726+
}
1727+
1728+
// Start the coroutine that snaps the scroll view at the end of the frame
1729+
StartCoroutine(ScrollToBottomCoroutine());
1730+
}
1731+
17191732
#endregion
17201733

17211734
#region Physical input methods

0 commit comments

Comments
 (0)