Hello I am writing in a C# behavior script and I have a text box that I am trying to have it resize it self when the text content becomes larger than the text. I see people mention a contentsizefitter but I have no found one example of it being used anywhere programmatically, please help thanks.
Do you have access to the scene? Just add a content size fitter component to the object with text and mess around with the settings and editing the text amount.
The UI is confusing as hell.
For most cases (ie. you’ve got a bunch of text you want to fit), you’ll need some kind of layout group on the same GameObject as the ContentSizeFitter. So say a HorizontalLayoutGroup and a ContentSizeFitter.
In that case, the layout group positions the elements, and the fitter makes the container fit them.
It would be too easy if the content size fitter just made a rect contain all the elements inside of it as you’d expect.
I’m guessing you are trying to create a dynamic textbox, kind of like what you see in comics. There is nothing on it being used programatically because it is not something meant to be controlled programatically.
What you can do is have an image gameobject with a child text gameobject
Add a ContentSizeFitter component to the text gameobject. This should make the text resize itself based on the text content. The ContentSizeFitter “HorizontalFit” and “VerticalFit” variables should be set to “Preferred Size” for it to resize based on the text content.
Add a ContentSizeFitter to the image gameObject as well. The “HorizontalFit” and “VerticalFit” variables should also be set to “Preferred Size”.
Now, add a layoutGroup component to the image gameobject. there are several to choose from. Grid, Horizontal, and Vertical layoutGroups should work fine. The thing we need from the layoutGroup is a feature called “padding”, so the background image will always keep a margin around the text child gameObject.
With this setup, the size of the object is now controlled based on the length of the text. the only thing missing now is a maximum length for the text to check when it should move on to a new line. That is what you will control “programatically”. Using “\n” in a string will force all characters after it to move to a new line.