Runic Dialogue UI - scroll to the bottom (autoscroll?)

Announcements, support questions, and discussion for the Dialogue System.
Pyrros
Posts: 1
Joined: Thu Apr 04, 2024 1:44 pm

Re: Runic Dialogue UI - scroll to the bottom (autoscroll?)

Post by Pyrros »

Hi,

Thanks so much for you code as I was looking for something similar.
The title of the topic is a bit deceptive at it is more like "Show accumulated text only when scrolling".

I had to change a bit the code given by giltar and then Sapidus3 to have the exact effect I wanted (explanations below) :

Code: Select all

using PixelCrushers.DialogueSystem;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;


// I was trying to find a way to accumulate but show the accumulated text only when scrolling
// Like "Tyranny" from Obsidian would do

/// NEW INPUT SYSTEM


public class DialogueHistory : MonoBehaviour
{
    [SerializeField] private bool clearHistoryOnConversationStart = true;
    [SerializeField] private Scrollbar scrollbar;
    [SerializeField] private RectTransform scrollContent;  // addition for pivot change
    [SerializeField] private StandardUISubtitlePanel standardUISubtitlePanel;
    [SerializeField] private ConversationView selectedResponseEventArgs; // don't know what for

    [SerializeField] private List<Subtitle> history = new();
    public List<Subtitle> History => history;
    private bool isScrolling;


    void OnConversationStart()
    {
        if (clearHistoryOnConversationStart)
        {
            history.Clear();
        }
    }

    void OnConversationLine(Subtitle subtitle)
    {
        FinishShowingHistory();
        if (subtitle.speakerInfo.isPlayer)
        {
            subtitle.formattedText.text = "<color=#B7B7B7>" + subtitle.formattedText.text + "</color>";
        }

        history.Add(subtitle);
    }

    private void Update()
    {
        Vector2 scrollVector2 = Mouse.current.scroll.ReadValue();
        float scroll = scrollVector2.y;

        if (!isScrolling && scroll != 0f && history.Count > 1)
        {
            isScrolling = true;
            AddCompleteHistory();
        }
    }

    private void AddCompleteHistory()
    {
        standardUISubtitlePanel.accumulatedText = "";
        standardUISubtitlePanel.accumulateText = true;
        foreach (Subtitle subtitle in history)
        {
            if (subtitle.formattedText.text == "")
            {
                continue;
            }

            subtitle.formattedText.text = @"\^" + subtitle.formattedText.text;
            standardUISubtitlePanel.SetContent(subtitle);
        }

        scrollContent.pivot = new Vector2(0, 0);
    }

    private void FinishShowingHistory()
    {
        isScrolling = false;
        standardUISubtitlePanel.accumulateText = false;

        scrollContent.pivot = new Vector2(0, 1);
    }
}

Changelog from previous scripts :

- I changed the way the scroll is detected to be compatible with the New Input System.
- I added a bool to chose if you want to clear the history once you end a conversation.
- I added a color to all the player lines to differentiate from NPC.
- "subtitle.formattedText.text = @"\^" + subtitle.formattedText.text;" is important if you have "Chars Per Seconds" so the last line does not rewrite itself
- I could not show properly the history with the goToBottom, so instead I changed the pivot of the scrollContent so that the scrollbar appears naturally at the end of the history (most recent line).


Now its working like a charm. Thank you all !
User avatar
Tony Li
Posts: 20646
Joined: Thu Jul 18, 2013 1:27 pm

Re: Runic Dialogue UI - scroll to the bottom (autoscroll?)

Post by Tony Li »

Thanks for sharing your code!
User avatar
Tony Li
Posts: 20646
Joined: Thu Jul 18, 2013 1:27 pm

Re: Runic Dialogue UI - scroll to the bottom (autoscroll?)

Post by Tony Li »

[Note: I deleted a suspected spam post. If you have a follow-up question that I inadvertently deleted, please post it again.]
Post Reply