Class 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.
Inherited Members
Namespace: NinjaTools.FlexBuilder.ContentSizing
Assembly: cs.temp.dll.dll
Syntax
public class ContentSizeCalculatorBasic470 : IContentSizeCalculator4x1
Fields
- (Boolean) debugTextCache
- (Boolean) suppressUnityBugWarning
There's a major text bug in all versions of Unity that Unity staff (as of spring 2022) have officially stated they will not fix. There's nothing we can do - their own support policy requires them to fix it, but they refuse to.
Maybe if enough people complain then Unity staff will take it seriously?
If you run into this warning/error a lot, you can disable the warning here - BUT your text layouts will now silently be incorrect.
Methods
- (ILayoutElement) _FindAttachedLayoutElement(FlexItem)
- (ILayoutElement) _FindChildLayoutElement(FlexItem)
- (IProvidesContentSize) _FindCustomSubclassOrNull(IContentSizeCalculator4x1.BehaviourLocation, FlexItem)
Checks the FlexItem for an attached MonoBehaviour, or a child GameObject with MonoBehaviour, that implements
the IProvidesContentSize interface, causing it to override/replace the default calculations in this class -
note that this is typically checked before / instead of checking for specific MonoBehaviour and ILayoutElement
subclasses
Parameters
IContentSizeCalculator4x1.BehaviourLocation | loc | |
FlexItem | mainObject |
Returns
IProvidesContentSize |
Exceptions
Exception |
- (Single) ContentSize(Text, FlexItem, Axis, CSSAvailableSpace, Boolean)
EXPERIMENTAL: this was a private internal call, but made public in 2024 so that users can piggyback on the high-performance, auto-cached, text-layout/sizing calculations performed internally by FlexBuilder (there is a LOT of code here that exists to fix bugs in core Unity that Unity staff have officially refused to fix themselves, even though the existence of this code shows they could have done so at any time in the last 8 years if they had chosen to).
This method is subject to change without notice, and may be replaced or deleted in future releases - if you are relying on it, please contact support@ninjatools.tech to make sure we know about your use case and have support for it.
Parameters
Text | text | |
FlexItem | item | |
Axis | axis | |
CSSAvailableSpace | containersInnerSize | |
Boolean | showDebugMessages |
Returns
Single |
Overrides
Exceptions
Exception |
- (Single) ContentSize_Text(Text, FlexItem, Axis, CSSAvailableSpace, Boolean)
If this is NOT named differently from
Parameters
Text | text | |
FlexItem | item | |
Axis | axis | |
CSSAvailableSpace | containersInnerSize | |
Boolean | showDebugMessages |
Returns
Single |
Exceptions
Exception |
- (Single) GetItemLengthInAxis(FlexItem, Axis, CSSAvailableSpace, Boolean, out Boolean, Boolean)
There is a specific order of searching here that makes sure we don't e.g. try to process a Button as Text (because we found the Button's child Text object by accident).
Priority order:
- User-provided overrides
- UI Elements that have an embedded Text
- UI Elements that have an embedded Image
- Raw Text
- Raw Image
- Everything else
Parameters
FlexItem | element | |
Axis | axis | |
CSSAvailableSpace | containersInnerSize | |
Boolean | scaleToFit | |
Boolean | wasSizeable | |
Boolean | showDebugMessages |
Returns
Single |
Overrides
- (Boolean) HasIntrinsicAspectRatio(FlexItem, out Single)
There is a specific order of searching here that makes sure we don't e.g. try to process a Button as Text (because we found the Button's child Text object by accident).
Priority order:
- User-provided overrides
- UI Elements that have an embedded Text
- UI Elements that have an embedded Image
- Raw Text
- Raw Image
- Everything else
Parameters
FlexItem | element | |
Single | ratio |
Returns
Boolean |
Overrides
- (ContentLength) MinimumIntrinsicContentSize(FlexItem, Axis, Boolean)
There is a specific order of searching here that makes sure we don't e.g. try to process a Button as Text (because we found the Button's child Text object by accident).
Priority order:
- User-provided overrides
- UI Elements that have an embedded Text
- UI Elements that have an embedded Image
- Raw Text
- Raw Image
- Everything else
Parameters
FlexItem | element | |
Axis | axis | |
Boolean | showDebugMessages |
Returns
ContentLength |
Overrides
- (void) PurgeCachesForItem(FlexItem)
TODO: this is a VERY messy implementation that needs substantial re-designing
Parameters
FlexItem | item |