Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ContentSizeFitter #24

Open
asger60 opened this issue May 29, 2024 · 3 comments
Open

Add support for ContentSizeFitter #24

asger60 opened this issue May 29, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@asger60
Copy link

asger60 commented May 29, 2024

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

@Syjgin
Copy link

Syjgin commented Jul 4, 2024

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

@Syjgin
Copy link

Syjgin commented Jul 5, 2024

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);
        }
...

@gilzoide
Copy link
Owner

gilzoide commented Aug 11, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants