Text overflow to dynamically resize scroll panel rect. Can't get it working. help me please!

I’m writing a CYOA game. I’m trying to set it up to where I can just type the text out and the choices will automatically be placed under the text of the main story and the scrollable panel will resize itself to fit accordingly. I’ve tried everything. It just isnt working. I tried calculating the height of the text…

height = story.gameObject.GetComponent<Text>().preferredHeight;
choice.transform.position = new Vector2(xPosition, Screen.height - height);

this works but when the scrollable rect resizes (not very accurately) it changes the position. I added a content size fitter with a grid and layout groups but it doesn’t resize the scrollable panel? What am i doing wrong? How can i fix this? thank you!

    public void MakeOneButton(string newChoice)
    {   height = story.gameObject.GetComponent<Text>().preferredHeight;
        float xPosition = 0;
        GameObject choice = Instantiate(button);
        choice.transform.position = new Vector2(xPosition, Screen.height - height);
        //choice.transform.SetParent(panel.transform, false);
        choice.GetComponentInChildren<Text>().text = newChoice;
        //choice1BottomPosition = choice.GetComponent<Transform>().position.y;

        //Vector2 rectSize = new Vector2(0, height);
        //panel.GetComponent<RectTransform>().sizeDelta = rectSize;
        //panel.GetComponent<Rect>().size = new Vector2();

    }

thanks eses

Just based on your explanation, do you mean something like this?

  • RectTransforms added change size based on text length.

  • ScrollRect containing these images/buttons changes height based on items count / total height.

  • It’s pretty hard at least for me to guess what you mean by not exact and changing position… Image/animation would have helped.

1 Like