Force Unity to scroll to the bottom of a scroll rect

So, I’ve got a textbox (TextMeshPro) with a load of text in it. As the game progresses, I add more text to the textbox and I want the user to see the bottom of the text (the last bit added)…

/// <summary>
/// Add text to the panel at the bottom of the screen
/// </summary>
public void AddTextToBottomPanel(string a_text)
{
  m_bottomPanelText.text += a_text;
  StartCoroutine(PushToBottom());
}

IEnumerator PushToBottom()
{
  yield return new WaitForEndOfFrame();
  m_bottomScrollRect.verticalNormalizedPosition = 0;
  LayoutRebuilder.ForceRebuildLayoutImmediate((RectTransform)m_bottomScrollRect.transform);
}
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: