Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

TextMesh Pro Using linked overflow with pages?

Discussion in 'UGUI & TextMesh Pro' started by MartinIsla, Apr 10, 2020.

  1. MartinIsla

    MartinIsla

    Joined:
    Sep 18, 2013
    Posts:
    72
    Update:
    This was yet another XY Problem. I solved this by using Linked pages (so I only set left page text, overflow goes to right page) and set left page's firstVisibleCharacter to right page's firstOverflowCharacterIndex when I want to go to the next two pages.

    Have a nice day everyone, just like the one I'm having right now.


    Hello everyone!

    This is something I've been dealing with for quite some time now. I tried multiple solutions, all of them seem to work initially, but a new problem always appears after every couple of weeks, some case where they don't work, and I need to find a new approach. That's why I'm asking for help now.

    I have a book, you can click arrows to go to the next/previous two pages. The problem I want to solve is how do I fit the text (that is a single string and can contain <sprites> that take up multiple lines so they look like illustrations in the book) into as many pages as needed.

    I tried:
    - Using TextGenerator with TMPro component values to estimate how much text will fit in each page.
    - Using hardcoded values.
    - A very ugly, desperate thing where I actually assigned the text, rebuilt the layout and checked how much text was shown. This one worked amazingly, but it was also non-surprisingly extremely expensive.

    An ideal solution would be something like using Linked overflow (so the content on the left page continues on the right page) but combined with Page overflow, so I can go to next/previous pages after I finished reading the right page. For what I've seen, there's no built-in solution (of course there isn't, it's a pretty specific thing that would rarely be useful for something that's not this game), so I'm looking for a workaround. I could try having multiple linked texts so leftPageOne is linked to rightPageOne, then rightPageOne is linked to leftPageTwo and so on. This would work but also be disgusting (and not fail-proof, since I don't know how long books will be).

    Any idea is appreciated right now. Going crazy here!

    Thanks :)
     
    Last edited: Apr 10, 2020
    ClawtheWolf likes this.
  2. ClawtheWolf

    ClawtheWolf

    Joined:
    Apr 4, 2019
    Posts:
    2
    May I see the code you used? Because when I do it, I get an IndexOutOfRange Exception, a blank right page and the left page stays the way it was originally.
     
  3. MartinIsla

    MartinIsla

    Joined:
    Sep 18, 2013
    Posts:
    72
    I can't really share the entire code cause it's for work, but this is how I move to the next two pages:

    Code (CSharp):
    1. leftPage.contentText.firstVisibleCharacter = rightPage.contentText.firstOverflowCharacterIndex;
    Going to the previous pages was a bit tricky, I came up with a solution I'm really not proud of, but it works. Whenever I move to the next two pages, I save the index of the first character in a list

    Code (CSharp):
    1.    // Save current firstVisibleCharacter
    2.    if (_pagesFirstVisibleCharacters.Count - 1 < _currentPageIndex) {
    3.       _pagesFirstVisibleCharacters.Add(leftPage.contentText.firstVisibleCharacter);
    4.    }
    So when I want to go back, I set the firstVisibleCharacter of the text using the value saved in that list

    Code (CSharp):
    1. leftPage.contentText.firstVisibleCharacter = _pagesFirstVisibleCharacters[_currentPageIndex];
     
    ClawtheWolf likes this.
  4. ClawtheWolf

    ClawtheWolf

    Joined:
    Apr 4, 2019
    Posts:
    2
    Thanks so much!! It was really helpful!
     
unityunity