0
\$\begingroup\$

I'm trying to implement readable books into my game. Much like Morrowind and Skyrim. I have a system that sort of works, but it appears as though there is a bug that is duplicating some page data, and trimming other page data. I've tried to google search for a solution, but can't seem to find one. I'm thinking this is an issue with how the pages are turned, perhaps I am missing something along the path to accomplishing this. Here are two pictures showing off the problem:

Page 1 to 2

Page 3 to 4

And here is the text that it's supposed to be displaying:

The Legend of the Quick Brown Fox

There once was a quick brown fox, who nimbly jumped over a lazy dog.

The lazy dog was not amused, and tried to catch the fox. However, being that the dog was far too fat and lazy to move, the fox managed to escape the clutches of the dog.

But then, given time, the dog decided to make a change. It started eating healthy, started working out. Waiting for the day when the quick brown fox would return.

And on a cold day, that's exactly what happened. The quick brown fox came from beyond the forest, and locked eyes on the dog who was seemingly fast asleep.

Without hesitation, the quick brown fox took off across the field and lept over the dog, only to be immediately snatched out of the air and ripped to shreds by the dog.

And everyone lived happily ever after. The end.

As you can see from the images, there is some duplicated text on pages 2 and 3, and then some text is missing completely.

Here is the code I'm using to achieve this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using TMPro;


public class BookContents : MonoBehaviour
{
    [TextArea(10, 20)][SerializeField] private string content;
    [Space][SerializeField] private TMP_Text leftSide;
    [SerializeField] private TMP_Text rightSide;
    [Space][SerializeField] private TMP_Text leftPagination;
    [SerializeField] private TMP_Text rightPagination;

    private void OnValidate()
    {
        UpdatePagination();

        if (leftSide.text == content)
        {
            return;
        }

        SetupContent();
    }

    private void Awake()
    {
        SetupContent();
        UpdatePagination();
    }

    private void SetupContent()
    {
        leftSide.text = content;
        rightSide.text = content;
    }

    private void UpdatePagination()
    {
        leftPagination.text = leftSide.pageToDisplay.ToString();
        rightPagination.text = rightSide.pageToDisplay.ToString();
    }

    public void PreviousPage()
    {
        if (leftSide.pageToDisplay < 1)
        {
            leftSide.pageToDisplay = 1;
            return;
        }

        if (leftSide.pageToDisplay - 2 > 1)
        {
            leftSide.pageToDisplay -= 2;
        }
        else
        {
            leftSide.pageToDisplay = 1;
        }
        rightSide.pageToDisplay = leftSide.pageToDisplay + 1;

        UpdatePagination();
    }

    public void NextPage()
    {
        if (rightSide.pageToDisplay >= rightSide.textInfo.pageCount)
        {
            return;
        }

        if (leftSide.pageToDisplay >= leftSide.textInfo.pageCount - 1)
        {
            leftSide.pageToDisplay = leftSide.textInfo.pageCount - 1;
            rightSide.pageToDisplay = leftSide.pageToDisplay + 1;
        }
        else
        {
            leftSide.pageToDisplay += 2;
            
        }
        rightSide.pageToDisplay = leftSide.pageToDisplay + 1;

        UpdatePagination();
    }
}

Here is a picture of the game object setup:

enter image description here

Here is a picture of the TextMeshPro Text object:

enter image description here

I would post the object for the right page, but it's literally the exact same as the left, with the only difference being that it's marked as page number 2 instead of 1.

And finally, Here are the objects and data being assigned to the script.

enter image description here

Can anyone spot why some of the text is duplicated, and why some of it is missing? I have a feeling it's related to the way the page-turning works, but I can't seem to fix it.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

I figured it out. Naturally, this issue was derived from the fact that I had done something exceedingly stupid. Evidently the TextMeshPro Text objects were not the same size, which led to the discrepancy. After making both text fields the same size, everything works as intended.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .