unity-flex-ui icon indicating copy to clipboard operation
unity-flex-ui copied to clipboard

Add support for ContentSizeFitter

Open asger60 opened this issue 1 year ago • 3 comments
trafficstars

As the title says, I think it would be very useful if we could have support ContentSizeFitter.

asger60 avatar May 29 '24 10:05 asger60

+1, can't use this asset for this reason

Syjgin avatar Jul 04 '24 14:07 Syjgin

Found a workaround: you can calculate content size programmatically. For example, for vertical layout it will be look like this:

...
        private const float PaddingW = 25f;
        private const float PaddingH = 25f;
        [SerializeField] private RectTransform _content;
        [SerializeField] private RectTransform scrollViewTransform; // to get max scroll content width

...
        public void SetContent(IReadOnlyCollection<TItem> content)
        {
            CleanContent();
            float contentHeight = 0f;
            float maxContentWidth = scrollViewTransform.sizeDelta.x;
            float currentContentWidth = 0f;
            foreach (TItem itemViewModel in content)
            {
                ItemUIView<TItem> itemView = CreateItemView(itemViewModel);

                float widthAddition  = itemView.GetComponent<RectTransform>().sizeDelta.x + PaddingW;
                currentContentWidth += widthAddition;
                float remainWidth = maxContentWidth - currentContentWidth;
                if (remainWidth < widthAddition)
                {
                    contentHeight += itemView.GetComponent<RectTransform>().sizeDelta.y + PaddingH;
                    currentContentWidth = 0f;
                }
                
            }
            _content.sizeDelta = new Vector2(0f, contentHeight);
        }
...

Syjgin avatar Jul 05 '24 08:07 Syjgin

Hey @asger60, thanks for the report. I agree that this should be added, it's super useful for text and image to be laied out according to their content size.

In the Yoga part of things, I think we need to hook measure functions for this to work properly. Also, I think ContentSizeFitter will want to relayout the element after FlexLayout does, so we'd likely have to use ILayoutElement.preferedHeight/preferedWidth for measuring instead of using ContentSizeFitter at all.

gilzoide avatar Aug 11 '24 11:08 gilzoide

+1 for ContentSizeFitter or a way to sync width and height for Image/TMPText would be amazing. I think that's the only major missing feature in this package!

devcube9 avatar Dec 08 '24 05:12 devcube9

Hey folks! Just released version 1.2.0 with support for ILayoutElements / ContentSizeFitter. I tested Text, Image and TextMeshProUGUI and it seems to work fine. Please open new issues if you find any problems with the new version.

Cheers \o/

gilzoide avatar Dec 13 '24 09:12 gilzoide