Want to expand the game, display text line by line?

I’ve just finished the 2D course and I’ve looped back to the Text 101 game and I really want to expand on the idea. What I really want to know is when displaying storyText from the scriptableobject is there a way to display the text line by line? Would I have to make distinct strings and access them one at a time? Can I make an array within the SO and use a combination of a for loop and coroutine to display one line, wait a few seconds, then display the next line?

I’ve tried doing some research on my own but I’m a bit stumped. I would really appreciate any guidance or just a push in the right direction. My problem is my text is so long it seems daunting to read when it just appears on the screen all at once so I was hoping to break it up a little.

Hi,

Welcome to our forum! :slight_smile:

Yes, it is possible to display the text line by line. You do not have to assign the string from the State object directly to the text variable. First of all, look for tutorials on the typewriter effect in Unity on Youtube. You can use the concept for your own idea. Instead of displaying character by character, you could display word by word or line by line.

The actual problem you need to solve is: What do you define as a line?

Once you defined that, you will have to develop a algorithm so you can split up the string from the State object according to your definition of a line.

By the way, if you just want to solve the problem with the long text, you could simply create more State objects and split the text up. You could tell the player to press “1” to continue or something like that. You are not obliged to provide more than one option.

https://community.gamedev.tv/t/auto-scrolling-text-solved/96410/7

Not sure if this will help you but I thought of it after reading your post, it shows one letter at a time so maybe a bit of tweaking and it could work for you.

Thank you, this was inspiring and I’m trying to adapt it to work for line by line. Appreciate you sharing!

Thanks for the quick reply Nina! I appreciate you sending me in the right direction with the “Typewriter” effect, I’ve been watching tutorials for hours now. There seems to be a function in TextMeshPro that will count the amount of lines in the text and I’m hoping I can use that for my purposes. I’ll update once I’ve got something working.

Ok figured it out. The textmeshpro countLine function didn’t seem to work so I started over. Then I found this video and from there I was able to come up with a solution.

public class TestDisplayer : MonoBehaviour
{
    //public TextAsset textFile;
    public Story story;
    public string[] textLines;
    public TextMeshProUGUI textComponent;
    float waitTime = 3f;

    private void Start()
    {
        TextImporter();
        textComponent.text = "";
        StartCoroutine(DisplayText());

    }

    //Converts storyText to an array, seperating entries by a carriage return '\n'
    private void TextImporter()
    {
        if(story != null)
        {
            textLines = (story.storyText.Split('\n'));
        }
    }

    //Display one line at a time after 3 seconds
    IEnumerator DisplayText()
    {
        for(int i=0; i < textLines.Length; i++)
        {
            textComponent.text += textLines[i] + "\n";
            yield return new WaitForSeconds(waitTime);

            if(i >= textLines.Length)
            {
                break;
            }
        }
    }

   
}

Next I want to make each line appear to Fade In when it is written on screen. I know I can do this with the animator and changing the alpha value on text. Thank you for those who pointed me in the right direction.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms