Speed up dialogue text while pressing the continue button

Announcements, support questions, and discussion for the Dialogue System.
MilziPPG
Posts: 33
Joined: Sat Jun 15, 2019 5:25 am

Speed up dialogue text while pressing the continue button

Post by MilziPPG »

Hi everyone,

I'm not sure if this is possible with the current system or not, but is there a way to speed up dialogue text appearing while the player presses the continue button. The fast forward function in the typewriter seems to just complete the dialogue box.

From what I have read, to speed up text with the continue button, you have to modify the typewriter script. Am I correct in thinking this?

Thanks!
User avatar
Tony Li
Posts: 20764
Joined: Thu Jul 18, 2013 1:27 pm

Re: Speed up dialogue text while pressing the continue button

Post by Tony Li »

Hi,

Nothing is built in to speed up dialogue text, but you can copy StandardUIContinueButtonFastForward.cs to a new script and modify it to speed up the typewriter instead of skipping to the end. In fact, here it is:
StandardUIContinueButtonSpeedUp.cs

Code: Select all

namespace PixelCrushers.DialogueSystem
{
    public class StandardUIContinueButtonSpeedUp : StandardUIContinueButtonFastForward
    {

        public int fasterSpeed = 100;

        public override void OnFastForward()
        {
            if ((typewriterEffect != null) && typewriterEffect.isPlaying)
            {
                var completeText = DialogueManager.currentConversationState.subtitle.formattedText.text;
                var textUI = typewriterEffect.GetComponent<UnityEngine.UI.Text>();
                var textSoFar = Tools.StripRichTextCodes(textUI.text.Substring(0, textUI.text.IndexOf("<color=#00000000>")));
                var charsSoFar = textSoFar.Length;
                typewriterEffect.charactersPerSecond = fasterSpeed;
                typewriterEffect.StartTyping(completeText, charsSoFar);
            }
            else
            {
                base.OnFastForward();
            }
        }
    }
}
Add the script above to your project, and replace StandardUIContinueButtonFastForward with it on your continue button.

You'll also need to add a little script to your main dialogue UI GameObject to reset the speed back to its original value afterward:
ResetTypewriterSpeed.cs

Code: Select all

using UnityEngine;

namespace PixelCrushers.DialogueSystem
{
    public class ResetTypewriterSpeed : MonoBehaviour
    {
        public AbstractTypewriterEffect typewriterEffect;

        private float originalCharsPerSec;

        private void Start()
        {
            if (typewriterEffect != null) originalCharsPerSec = typewriterEffect.charactersPerSecond;
        }

        void OnConversationLine(Subtitle subtitle)
        {
            if (typewriterEffect != null) typewriterEffect.charactersPerSecond = originalCharsPerSec;
        }
    }
}
MilziPPG
Posts: 33
Joined: Sat Jun 15, 2019 5:25 am

Re: Speed up dialogue text while pressing the continue button

Post by MilziPPG »

So close! The reset doesn't seem to be working though. I've added it to every game object in my WRPG Template Standard Dialogue UI (I'm using that as a base)
dialogue ui.PNG
dialogue ui.PNG (26.44 KiB) Viewed 2488 times
I also tried putting in on the Dialogue Manager and the Canvas child GameObject but I can't link the Typewriter Effect that the script asks for
User avatar
Tony Li
Posts: 20764
Joined: Thu Jul 18, 2013 1:27 pm

Re: Speed up dialogue text while pressing the continue button

Post by Tony Li »

Hi,

If you're using the WRPG template, you're probably accumulating text, which adds another twist. You'll need to use these versions:
ResetTypewriterSpeed.cs

Code: Select all

using UnityEngine;

namespace PixelCrushers.DialogueSystem
{
    public class ResetTypewriterSpeed : MonoBehaviour
    {
        public AbstractTypewriterEffect typewriterEffect;
        public StandardUIContinueButtonSpeedUp continueButtonSpeedUp;

        private float originalCharsPerSec;
        private float originalFullPause;
        private float originalQuarterPause;

        private void Start()
        {
            if (typewriterEffect != null)
            {
                originalCharsPerSec = typewriterEffect.charactersPerSecond;
                originalFullPause = typewriterEffect.fullPauseDuration;
                originalQuarterPause = typewriterEffect.quarterPauseDuration;

            }
        }

        void OnConversationLine(Subtitle subtitle)
        {
            if (typewriterEffect != null)
            {
                typewriterEffect.charactersPerSecond = originalCharsPerSec;
                typewriterEffect.fullPauseDuration = originalFullPause;
                typewriterEffect.quarterPauseDuration = originalQuarterPause;
            }
        }

        void OnConversationStart(Transform actor)
        {
            if (continueButtonSpeedUp != null) continueButtonSpeedUp.completeText = string.Empty;
        }

    }
}
StandardUIContinueButtonSpeedUp.cs

Code: Select all

using UnityEngine;

namespace PixelCrushers.DialogueSystem
{
    public class StandardUIContinueButtonSpeedUp : StandardUIContinueButtonFastForward
    {

        public int fasterSpeed = 100;
        public float fasterFullPause = 0.5f;
        public float fasterQuarterPause = 0.1f;

        public string completeText;
        private Subtitle lastSubtitleAdded = null;

        private bool accumulateText { get { return dialogueUI.conversationUIElements.defaultNPCSubtitlePanel.accumulateText; } }

        private void AddText(Subtitle subtitle)
        {
            if (accumulateText)
            {
                if (!string.IsNullOrEmpty(completeText)) completeText += "\n";
                completeText += subtitle.formattedText.text;
            }
            else
            {
                completeText = subtitle.formattedText.text;
            }
            lastSubtitleAdded = subtitle;
        }

        public override void OnFastForward()
        {
            var subtitle = DialogueManager.currentConversationState.subtitle;
            if ((typewriterEffect != null) && typewriterEffect.isPlaying && (typewriterEffect.charactersPerSecond != fasterSpeed))
            {
                AddText(subtitle);
                var textUI = typewriterEffect.GetComponent<UnityEngine.UI.Text>();
                var typewriterEndPos = textUI.text.IndexOf("<color=#00000000>");
                var textSoFar = (typewriterEndPos >= 0) ? Tools.StripRichTextCodes(textUI.text.Substring(0, typewriterEndPos)) : completeText;
                textSoFar = textSoFar.Replace("\\.", "");
                var charsSoFar = textSoFar.Length;
                typewriterEffect.charactersPerSecond = fasterSpeed;
                typewriterEffect.fullPauseDuration = fasterFullPause;
                typewriterEffect.quarterPauseDuration = fasterQuarterPause;
                typewriterEffect.StartTyping(completeText, charsSoFar);
            }
            else
            {
                if (accumulateText && lastSubtitleAdded != subtitle)
                {
                    AddText(subtitle);
                }
                base.OnFastForward();
            }
        }
    }
}
And you'll need to assign the StandardUIContinueButtonSpeedUp component to the ResetTypewriterSpeed's Continue Button Speed Up field. The ResetTypewriterSpeed script should be on the WRPG Template Standard Dialogue UI.

Edit: Updated scripts to allow speedup for pause commands.
MilziPPG
Posts: 33
Joined: Sat Jun 15, 2019 5:25 am

Re: Speed up dialogue text while pressing the continue button

Post by MilziPPG »

That seems to have done it! Is it easy to change the script so that the text speed resets its speed when the player lets go of the interact button? I.e.

Interact button not held - normal speed
Interact button held - fast speed
Let go of interact button - return to normal speed?

Sorry for all of the questions!
User avatar
Tony Li
Posts: 20764
Joined: Thu Jul 18, 2013 1:27 pm

Re: Speed up dialogue text while pressing the continue button

Post by Tony Li »

I suppose you could change it to something like this: (warning: untested)
Spoiler

Code: Select all

using UnityEngine;

namespace PixelCrushers.DialogueSystem
{
    public class StandardUIContinueButtonSpeedUp : StandardUIContinueButtonFastForward
    {

        public string completeText;
        private Subtitle lastSubtitleAdded = null;

        private bool accumulateText { get { return dialogueUI.conversationUIElements.defaultNPCSubtitlePanel.accumulateText; } }

        private void AddText(Subtitle subtitle)
        {
            if (lastSubtitleAdded == subtitle) return;
            if (accumulateText)
            {
                if (!string.IsNullOrEmpty(completeText)) completeText += "\n";
                completeText += subtitle.formattedText.text;
            }
            else
            {
                completeText = subtitle.formattedText.text;
            }
            lastSubtitleAdded = subtitle;
        }

        public override void OnFastForward()
        {
            if (typewriterEffect != nul) && typewriterEffect.isPlaying) return; && (typewriterEffect.charactersPerSecond != fasterSpeed))
            {
                // Do nothing. Handle in PointerDown and PointerUp events.
            }
            else
            {
                AddText(DialogueManager.currentConversationState.subtitle);
                base.OnFastForward();
            }
        }
        
        public void SetTypewriterSpeed(int charsPerSec)
        {
            AddText(DialogueManager.currentConversationState.subtitle);
            var textUI = typewriterEffect.GetComponent<UnityEngine.UI.Text>();
            var typewriterEndPos = textUI.text.IndexOf("<color=#00000000>");
            var textSoFar = (typewriterEndPos >= 0) ? Tools.StripRichTextCodes(textUI.text.Substring(0, typewriterEndPos)) : completeText;
            var charsSoFar = textSoFar.Length;
            typewriterEffect.charactersPerSecond = charsPerSec;
            typewriterEffect.StartTyping(completeText, charsSoFar);
        }
    }
}
Then add an Event Trigger component to the button. Configure the PointerDown event to call SetTypewriterSpeed(X) and the PointerUp event to call SetTypewriterSpeed(Y) where X is the faster speed and Y is the original speed.
MilziPPG
Posts: 33
Joined: Sat Jun 15, 2019 5:25 am

Re: Speed up dialogue text while pressing the continue button

Post by MilziPPG »

Thanks Tony! I'll be the guinea pig and test it out.

I'll report back soon, thank you again!
MilziPPG
Posts: 33
Joined: Sat Jun 15, 2019 5:25 am

Re: Speed up dialogue text while pressing the continue button

Post by MilziPPG »

Sorry for the massively late reply on this one. The first method works and I think I'll stick with it for the minute (I've grown to like it).

I do have a follow on question that has popped up now. If I try and use time control codes (e.g. \, and \.) in my dialogue, the time codes will only appear when I have pressed space once to make the text go faster. When it goes at its normal speed, the time codes do not appear. Do I have to add the component Unity UI Ignore Pause Codes (and if I do have to add it, where would I do so) or is this a slightly more complicated issue. I have attached a video below so you can see what happens when the text is slow (the first dialogue in the video) and when the text is fast (the second dialogue in the video).



Thanks!
User avatar
Tony Li
Posts: 20764
Joined: Thu Jul 18, 2013 1:27 pm

Re: Speed up dialogue text while pressing the continue button

Post by Tony Li »

By "appear" do you mean that the time codes take effect (i.e., pause temporarily) or that the actual code text is visible in the subtitle (e.g., "my friend looks like\. death") or something else?
MilziPPG
Posts: 33
Joined: Sat Jun 15, 2019 5:25 am

Re: Speed up dialogue text while pressing the continue button

Post by MilziPPG »

So, what happens when you speed up the text, the text starts to appear and when the time control code is supposed to pause the text, it does pause the text, but all the text disappears (apart from the letter or punctuation mark you are pausing and the time code itself) for a brief moment while it waits for the time code function to finish.

Also, despite the text being sped up, the time code still pauses for the same amount of time as when it is slow. So for example, my slow text speed is currently 20 characters per second, and my fast speed text is 40 characters per second. If \. is the same as 1 second of pause time when my text speed is slow at 20 characters per second, is there a way so that at fast speed, \. is actually 0.5 seconds?

Thank you!
Post Reply