Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Solved] Scrolling dynamically-sized text

Discussion in 'UGUI & TextMesh Pro' started by MV10, Apr 2, 2016.

  1. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    Yes, this thread again. My latest n00b adventures involve deciphering the GUI system. I'm doing a little test-harness for a dialogue experiment. This seems like a simple problem but I've been butting my head against it all morning. Every thread / video / blog post I can find on the topic seems to assume a statically-sized text object.

    The program periodically adds new lines of text to a Text object, and when the Text object vertically expands beyond the size of the masked area, I want the Scrollbar to automatically update to reflect how much content is off-screen (masked). Nothing oddball there, scrollbars have worked this way for decades -- except in Unity, apparently. Additionally I'm setting the Scrollbar value to the bottom with the intent of showing the newly-added content.

    The part which works is that the textbox grows and the masking clips it at the bottom.

    One problem is that the scrollbar seems to ignore the size of the text content. As the content exceeds the mask area, the scroll Handle stays full-sized; it does not indicate there is content off the bottom of the screen, nor does it respond to setting the value to scroll all the way down.

    Another stranger problem is that if I manually grab and drag the Text content (once it has grown beyond the mask bounds), the scroll Handle (which occupies 100% of the Sliding Area) starts to shrink proportionally with how far I drag the Text. Then for some reason it bounces back to the top.

    The hierarchy is like this, which I believe to be correct:
    1.jpg

    The various important-seeming non-default Inspector settings are:

    Canvas, Panel - at defaults

    ScrollRect Mask - Content is the Text object; Vertical Scrollbar is the Scrollbar object; Horizontal is unchecked

    Text - pivot anchored to the top of the parent (I believe this makes it expand downward as it resizes, maybe this is the problem?); stretch is horizontal only; Horizontal is Wrap; Vertical is Overflow; Align is left / top

    Scrollbar - Direction is Bottom To Top

    The test-code is dead simple:
    Code (csharp):
    1.     public void ButtonClick_Reset()
    2.     {
    3.         dialogue.text = "";
    4.         lineCountdown = 100;
    5.         nextTick = 0f;
    6.     }
    7.  
    8.     void Update()
    9.     {
    10.         if(lineCountdown > 0 && Time.time > nextTick)
    11.         {
    12.             dialogue.text += "this is line " + lineCountdown.ToString() + "\n";
    13.             dialogueScrollbar.value = 0f;
    14.             lineCountdown--;
    15.             nextTick = Time.time + 0.1f;
    16.         }
    17.     }
    So what am I missing?
     
  2. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    I removed the Content Size Fitter from the Panel and added it to the Text object, and changed Vertical Fit to "Preferred Size". It shows the "Parent has a type of layout group component" warning, but I read that can be ignored, so I tried it.

    Now the text grows and the scrollbar resizes itself as expected. I really don't understand why that change fixed anything.

    There is a new problem; when the text box is smaller than the mask, it is vertically centered in the masked area. The pivot anchors seem to be ignored (they are at the top of the mask).

    So close!

    So baffled!

    (As a side note, the Unity docs for these options are useless, it looks like somebody from Microsoft wrote them. So "Preferred Size" sets the content to the preferred size, eh? I never would have guessed!)
     
  3. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    Everybody likes pictures. The first one is before I click "Reset" to load up the text box. The second one is with the list partially full (not exceeding the masked area). The third one shows the scrollbar functioning properly once the list exceeds the masked boundaries.

    1.jpg

    2.jpg

    3.jpg
     
  4. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    By thrashing around trying random things, it turns out if I manually edit the Rect Transform's Pivot Y value to 1.0, then it works. In the Editor it still floats around in the middle, but at runtime it moves to the top where I want it. I have no idea why it resets it's position when I drag it -- seems like a bug -- but at least it's working now.
     
  5. mutant_nz

    mutant_nz

    Joined:
    Nov 16, 2015
    Posts:
    12
  6. nithinm

    nithinm

    Joined:
    Sep 17, 2015
    Posts:
    1
    BIG Thanks @MV10 , you saved me from hours of debugging and frustration :)
     
  7. Endahs

    Endahs

    Joined:
    Sep 16, 2014
    Posts:
    94
  8. MaxLohMusic

    MaxLohMusic

    Joined:
    Jan 17, 2022
    Posts:
    58
    Thanks. Adding a Content Size Fitter fixed everything in 2 seconds. No idea why it isn't in the default scroll view.