Namespace NinjaTools.FlexBuilder.ContentSizing
Classes
ContentSizeCalculatorBasic441
Simple (but not optimized - see below) implementation of IContentSizeCalculator that doesn't try to be too clever, but DOES support text-flowing in the way CSS 'suggests': i.e. Text is soft-wrapped at word boundaries. It also allows custom overrides by checking for the presence of IProvidesContentSize (not supported yet) subclasses attached to the GameObject of each FlexItem
Spec: https://www.w3.org/TR/css-sizing-3/#min-content-inline-size Spec: https://www.w3.org/TR/css-sizing-3/#max-content-inline-size
Known issues:
- Unity has some major bugs in the handling of variable size fonts, and rather than handle all possible variations, this currently uses the exact 'fontSize' on each Unity.UI.Text object - it makes NO ATTEMPT to pre-size-to-fit the Text (which would be my preference). "size to fit" is treated as a 'post layout cosmetic operation, fully managed by Unity' since Unity's APIs and their bugs make it very difficult to do that without a huge amount of custom code.
Future possible optimizations:
- for variable-size content (i.e. size-to-fit Text, and scalable-images): this re-calcs both width AND height whenever it needs either, because the first time its called per-layout (parent's main-sizing - could be x OR y first) it has to 'set' the variable-size part, and second time its called (parent's cross-sizing) it has to 're-use' the exact same variable-size part. I didn't want to have to add parameters and state and state-handling to the calling-classes to manage keeping that data for only the duration of one layout, but keeping and re-using it - so instead I just re-calc both values each time.
ContentSizeCalculatorBasic470
Simple (but not optimized - see below) implementation of IContentSizeCalculator that doesn't try to be too clever, but DOES support text-flowing in the way CSS 'suggests': i.e. Text is soft-wrapped at word boundaries. It also allows custom overrides by checking for the presence of IProvidesContentSize (not supported yet) subclasses attached to the GameObject of each FlexItem
Spec: https://www.w3.org/TR/css-sizing-3/#min-content-inline-size Spec: https://www.w3.org/TR/css-sizing-3/#max-content-inline-size
Updates:
- since 4.7.0, this added: better handling for Buttons with an image and no text
- since 4.7.0, this added: public APIs for you to re-use and/or customize the TextMeshPro sizing calcs
Known issues:
- Unity has some major bugs in the handling of variable size fonts, and rather than handle all possible variations, this currently uses the exact 'fontSize' on each Unity.UI.Text object - it makes NO ATTEMPT to pre-size-to-fit the Text (which would be my preference). "size to fit" is treated as a 'post layout cosmetic operation, fully managed by Unity' since Unity's APIs and their bugs make it very difficult to do that without a huge amount of custom code.
Future possible optimizations:
- for variable-size content (i.e. size-to-fit Text, and scalable-images): this re-calcs both width AND height whenever it needs either, because the first time its called per-layout (parent's main-sizing - could be x OR y first) it has to 'set' the variable-size part, and second time its called (parent's cross-sizing) it has to 're-use' the exact same variable-size part. I didn't want to have to add parameters and state and state-handling to the calling-classes to manage keeping that data for only the duration of one layout, but keeping and re-using it - so instead I just re-calc both values each time.
ContentSizeCalculatorBasic4x1
Simple (but not optimized - see below) implementation of IContentSizeCalculator that doesn't try to be too clever, but DOES support text-flowing in the way CSS 'suggests': i.e. Text is soft-wrapped at word boundaries. It also allows custom overrides by checking for the presence of IProvidesContentSize (not supported yet) subclasses attached to the GameObject of each FlexItem
Spec: https://www.w3.org/TR/css-sizing-3/#min-content-inline-size Spec: https://www.w3.org/TR/css-sizing-3/#max-content-inline-size
Known issues:
- Unity has some major bugs in the handling of variable size fonts, and rather than handle all possible variations, this currently uses the exact 'fontSize' on each Unity.UI.Text object - it makes NO ATTEMPT to pre-size-to-fit the Text (which would be my preference). "size to fit" is treated as a 'post layout cosmetic operation, fully managed by Unity' since Unity's APIs and their bugs make it very difficult to do that without a huge amount of custom code.
Future possible optimizations:
- for variable-size content (i.e. size-to-fit Text, and scalable-images): this re-calcs both width AND height whenever it needs either, because the first time its called per-layout (parent's main-sizing - could be x OR y first) it has to 'set' the variable-size part, and second time its called (parent's cross-sizing) it has to 're-use' the exact same variable-size part. I didn't want to have to add parameters and state and state-handling to the calling-classes to manage keeping that data for only the duration of one layout, but keeping and re-using it - so instead I just re-calc both values each time.
IContentSizeCalculator4x1
Layout Algorithms from 4.0.9 onwards delegate to an instance of this class to do all content-sizing, since it's complicated and independent of the layout algorithm (enabling you to mix-and-match content-size approaches with different flex-algorithms)
TODO: PERFORMANCE: instead of caching each individual MonoBehaviourAttachmentCalculation ... do a single compound call that has a large struct with null refs to all the possible attachments, precisely one of which is filled-in, and cache that -- then the subsequent calls don't need to hit the cache M times for N items, but instead 1 time for N items
TextContentSizing
Many core helper methods for auto-sizing Text/UI.Text/TextMeshPro when doing content-sizing calculations using IContentSizeCalculator4x1 for FlexBuilder.
Contains the crucial method LayoutSizeWithFontSize(Text, CSSContentLengthConstrainable, Nullable<Int32>, Nullable<Int32>, Nullable<Int32>, Boolean, Boolean, Boolean) which is where 99% of the hard work in auto-sizing text happens.
NB: this was originally private internal implementation, and the APIs in this class are subject to change without notice, but for a few advanced use-cases we had to expose this (e.g. for people doing advanced customization of content-sizing, so that they can re-use the existing complex text calculations instead of having to re-invent the wheel).
Structs
ContentSizeCalculation_Text
Custom ContentSizeCalculation that lets us do extra fine-grained caching of the sizes of Unity Text objects