Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Limit Max Width of Layout Component?

Discussion in 'UGUI & TextMesh Pro' started by Stephan-B, Apr 5, 2015.

  1. andbto

    andbto

    Joined:
    Feb 3, 2017
    Posts:
    1
    This worked nicely for me! Thanks for sharing, @bilalakil
     
    ChristmasGT and bilalakil like this.
  2. L0tan

    L0tan

    Joined:
    Jul 16, 2017
    Posts:
    75
    I know that setting max Widths on a Layout Component are no suppoused to work on every resolution but, in case you want to maintain the "ratio" of a predefined width in a predefined resolution, on the rest of resolutions, and for whatever reasons you can't use anchors (maybe you are restricted by parents or children behaviours), here is my modification on @bilalakil solution:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. [ExecuteAlways]
    5. [RequireComponent(typeof(ContentSizeFitter))]
    6. public class ContentSizeFitterMaxWidth : MonoBehaviour
    7. {
    8.     public float maxWidth;
    9.  
    10.     private float widthRatio;
    11.     private RectTransform _rtfm;
    12.     private ContentSizeFitter _fitter;
    13.     private ILayoutElement _layout;
    14.  
    15.     private void OnValidate() => OnEnable();
    16.  
    17.     private void OnEnable()
    18.     {
    19.         _rtfm = (RectTransform)transform;
    20.         _fitter = GetComponent<ContentSizeFitter>();
    21.         _layout = GetComponent<ILayoutElement>();
    22.  
    23.         widthRatio = maxWidth / Screen.width;
    24.     }
    25.  
    26.     private void Update()
    27.     {
    28.         maxWidth = widthRatio * Screen.width;
    29.  
    30.         _fitter.horizontalFit = _layout.preferredWidth > maxWidth
    31.             ? ContentSizeFitter.FitMode.Unconstrained
    32.             : ContentSizeFitter.FitMode.PreferredSize;
    33.  
    34.         if (_layout.preferredWidth > maxWidth)
    35.         {
    36.             _fitter.horizontalFit = ContentSizeFitter.FitMode.Unconstrained;
    37.             _rtfm.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, maxWidth);
    38.         }
    39.         else
    40.             _fitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
    41.     }
    42.  
    43. }
    44.  
     
  3. WavyRancheros

    WavyRancheros

    Joined:
    Feb 5, 2013
    Posts:
    165
    I am sorry to bump this. I need this, BUT instead of wrapping the text to a new line, I want to automatically shorten it with an Ellipsis. Doesn't work.
     
  4. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    515
    I can confirm that this super simple suggestion solved my problem without writing any custom code.

    **EDIT**

    I have spoken too early. It didn't work :(
     
    Last edited: Jun 29, 2022
  5. jpwhite-personal

    jpwhite-personal

    Joined:
    May 9, 2022
    Posts:
    1
    This worked for me. Thanks @bilalakil !
     
    dkydev and bilalakil like this.
  6. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    515
    Short story: This solution worked for me. I used @Ludiq's version because it supports both horizontal and vertical maximum limits.

    Long story:
    I came back to this problem (and this forum thread) again and again over the years while working on different projects. My goal was to find the perfect solution without writing a custom script and by simply trying to understand how Unity layout system works. Today, I think, I fully figured it out and noticed that it's not possible without writing a custom ContentSizeFitter. So, I gave up on my goal and started using @Ludiq's version.

    Thank you very much.
     
    MarcSpraragen likes this.
  7. ChristmasGT

    ChristmasGT

    Joined:
    Sep 21, 2011
    Posts:
    11
    Huge help, thank you so much!
     
    bilalakil likes this.
  8. mayberohit

    mayberohit

    Joined:
    Oct 2, 2018
    Posts:
    1
    This worked for me :
    If parent contains Horizontal or Vertical Layout Group, Enable 'Control Child Size' and Disable 'Child Force Expand', then add 'Layout Element' to child and set Preferred Height/Width.
     
    GainfulSage likes this.
  9. losingisfun

    losingisfun

    Joined:
    May 26, 2016
    Posts:
    35
    Thank you, you are a saint.

    For anyone using this on a newer version of unity, the Control Child Size need to be checked for Width and Height for both vertical layout groups.
     
  10. eky

    eky

    Joined:
    Sep 28, 2017
    Posts:
    6
    I found something working for me, I try to explain (it's really easy to reproduct):

    Make one gameObject :
    *stretch : (free = blue cross) don't know if it matter, anyway;
    *Anchors : Min X & Y = 0; Max X &Y = 1;
    *Pivot : X = 0; Y = 0,5;
    *TextMeshPro with Alignement Left;
    *Content Size Fitter : Horizontal = Preferred Size; Vertical = Unconstrained;
    *Horizontal Layout Group : Child Align = Upper Left; Control Child Size = Width; (Left = 13; Spacing = 4 but not really important)

    Then, make 3 gameObject child one the first:
    For All :
    *stretch : top left
    *Anchors : Min X = 0 & Y = 1; Max X = 0 & Y = 1;
    *Pivot : X = 0,5; Y = 0,5;
    *TextMeshPro with >For 1rst Go : (for the example write : 50) & Alignement Right;
    For 2nd Go : (for the example write : /) & Alignement Right;
    For 3rd Go : (for the example write : 50) & Alignement Left;

    That's it. Now just change the value on the textMeshpro of the 1rst Gameobject(of child), just add some 0000 and see the natural extension that you want.
     
  11. sinedsem

    sinedsem

    Joined:
    May 7, 2020
    Posts:
    6
    Can you please share an example of your project? I did like you say and it doesn't work for me.
     
  12. AdamandEveStudios

    AdamandEveStudios

    Joined:
    Apr 19, 2022
    Posts:
    15
    Why is it so hard to do something so simple. This is an incredible waste of energy and time. Have tried everything in this thread and nothing has worked