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.

Other Text alignment

Discussion in 'UGUI & TextMesh Pro' started by GadOleg, Dec 20, 2022.

  1. GadOleg

    GadOleg

    Joined:
    Nov 13, 2019
    Posts:
    53
    Hello everyone
    I have a need to create a prefab for displaying text. The goal is to completely fill the block area depending on the filling of the fields, see the example in the figures below:
    2022-12-20_22-51-58.png

    2022-12-20_22-52-47.png

    I made a script for prefab in this way:
    Code (CSharp):
    1.     public class ResultBlock : MonoBehaviour
    2. {
    3. ...
    4.  
    5. public void TextShowResult(string value = "", string ES = "", string EI = "", string units = "")
    6.     {
    7.         if (value == "" && ES == "" && EI == "" && units == "") {
    8.             T_Value.text = value;
    9.             T_ES.text    = ES;
    10.             T_EI.text    = EI;
    11.             T_units.text = EI;
    12.             return;
    13.         }
    14.  
    15.         if (T_Value.material == null || T_units.material == null || T_ES.material == null || T_EI.material == null ) return;
    16.         this.Value = value;
    17.         this.ES = ES;
    18.         this.EI = EI;
    19.         this.units = units;
    20.  
    21.         T_Value.margin = Vector4.zero;
    22.         T_ES.margin = Vector4.zero;
    23.         T_EI.margin = Vector4.zero;
    24.         T_units.margin = Vector4.zero;
    25.  
    26.         string ESEI = "";
    27.         float prefV,  prefES, prefEI, prefU;        
    28.         float wALL, wV = 0f, wEIES = 0f, wU = 0f;        
    29.         float _fontSize = 1f;
    30.         bool UpES = true, UpEI = true;                        
    31.         bool FreeValueUnits = false;
    32.         bool OneLine = false;
    33.  
    34.         T_Value.gameObject.SetActive(true);
    35.         T_ES.gameObject.SetActive(true);
    36.         T_EI.gameObject.SetActive(true);
    37.         T_units.gameObject.SetActive(true);
    38.  
    39.         if (ES != "" || EI != "")              
    40.         {
    41.             float dd = ES.IndexOf("-");
    42.             if (dd < 0) dd = ES.IndexOf("+"); else UpES = false;
    43.  
    44.             float dd2 = EI.IndexOf("-");
    45.             if (dd2 < 0) dd2 = EI.IndexOf("+"); else UpEI = false;
    46.  
    47.  
    48.             if (ES == EI)
    49.             {
    50.                 if (UpES && UpEI) EI = "";
    51.                 else ES = "";
    52.             }
    53.             else
    54.             {
    55.                 if (UpES && !UpEI)
    56.                 {
    57.                     string buf = ES.Trim(new Char[] { '+', '-', ' ' });
    58.                     string buf2 = EI.Trim(new Char[] { '+', '-', ' ' });
    59.                     buf.Replace(".", ",");
    60.                     buf2.Replace(".", ",");
    61.                     if (buf == buf2)
    62.                     {
    63.                         ESEI = "±"+ES.Trim(new Char[] { '+', '-', ' ' });
    64.                         T_ES.text = "";
    65.                         ES = "";
    66.                         T_EI.text = "";
    67.                         EI = "";
    68.                         OneLine = true;
    69.                     }
    70.                 }
    71.             }
    72.         }
    73.         else OneLine = true;
    74.  
    75.         if (OneLine)
    76.         {
    77.             T_ES.gameObject.SetActive(false);
    78.             T_EI.gameObject.SetActive(false);
    79.             T_units.gameObject.SetActive(false);
    80.  
    81.             RT_Value.anchorMin = new Vector2(0f, 0f);
    82.             RT_Value.anchorMax = new Vector2(1f, 1f);
    83.             RT_Value.sizeDelta = Vector2.zero;
    84.             T_Value.text = value + ESEI + units;
    85.             T_Value.enableAutoSizing = true;
    86.  
    87.             switch (aligen)
    88.             {
    89.                 case Aligen.Center:
    90.                     T_Value.alignment = TextAlignmentOptions.Midline;
    91.                     break;
    92.                 case Aligen.Right:
    93.                     T_Value.alignment = TextAlignmentOptions.MidlineRight;
    94.                     break;
    95.                 default:
    96.                     T_Value.alignment = TextAlignmentOptions.MidlineLeft;
    97.                     break;
    98.             }
    99.             return;
    100.         }
    101.  
    102.         prefV    = UpdateSizeText(T_Value, value);
    103.         prefU    = UpdateSizeText(T_units, units);
    104.         prefES   = UpdateSizeText(T_ES, ES);
    105.         prefEI   = UpdateSizeText(T_EI, EI);
    106.  
    107.         if (prefES > prefEI) prefEI = prefES;
    108.         else prefES = prefEI;
    109.  
    110.         wALL = prefV + prefES + prefU; if (wALL == 0) return;
    111.         wV = prefV / wALL;
    112.         wEIES = prefES / wALL;
    113.  
    114.         if (prefV != 0f)
    115.         {
    116.             if (RT_Value == null) return;
    117.             RT_Value.anchorMin = new Vector2(0f, RT_Value.anchorMin.y);
    118.             RT_Value.anchorMax = new Vector2(wV, RT_Value.anchorMax.y);
    119.             RT_Value.sizeDelta = Vector2.zero;
    120.         }
    121.         else T_Value.gameObject.SetActive(false);
    122.  
    123.         if (prefES != 0f)
    124.         {
    125.             RT_ES.anchorMin = new Vector2(wV, RT_ES.anchorMin.y);
    126.             RT_ES.anchorMax = new Vector2(wV + wEIES, RT_ES.anchorMax.y);
    127.         }
    128.         else T_ES.gameObject.SetActive(false);
    129.  
    130.         if (prefEI != 0f)
    131.         {
    132.             RT_EI.anchorMin = new Vector2(wV, RT_EI.anchorMin.y);
    133.             RT_EI.anchorMax = new Vector2(wV + wEIES, RT_EI.anchorMax.y);
    134.         }
    135.         else T_EI.gameObject.SetActive(false);
    136.  
    137.         if (prefU != 0f)
    138.         {
    139.             if (prefES != 0f || prefEI != 0f)
    140.             RT_units.anchorMin = new Vector2(wV + wEIES, RT_units.anchorMin.y);
    141.             RT_units.anchorMax = new Vector2(1f, RT_units.anchorMax.y);
    142.         }
    143.         else T_units.gameObject.SetActive(false);
    144.  
    145.  
    146.         if (T_Value.gameObject.activeSelf) T_Value.ForceMeshUpdate();
    147.         if (T_ES.gameObject.activeSelf)    T_ES.ForceMeshUpdate();
    148.         if (T_EI.gameObject.activeSelf)    T_EI.ForceMeshUpdate();
    149.         if (T_units.gameObject.activeSelf) T_units.ForceMeshUpdate();
    150.  
    151.       //  return;
    152.  
    153.         if (T_Value.gameObject.activeSelf && T_units.gameObject.activeSelf )
    154.         {
    155.             _fontSize = T_Value.fontSize;
    156.             if (_fontSize < T_units.fontSize) _fontSize = T_units.fontSize;
    157.             FreeValueUnits = false;
    158.         }
    159.         else
    160.         {
    161.             if (!T_units.gameObject.activeSelf && !T_Value.gameObject.activeSelf)
    162.             {
    163.                 _fontSize = -1f;
    164.                 FreeValueUnits = true;
    165.             }
    166.             if (T_units.gameObject.activeSelf && !T_Value.gameObject.activeSelf)
    167.             {
    168.                 _fontSize = T_units.fontSize;
    169.             }
    170.             if (T_Value.gameObject.activeSelf && !T_units.gameObject.activeSelf)
    171.                 _fontSize = T_Value.fontSize;
    172.  
    173.             if (T_Value.gameObject.activeSelf && T_units.gameObject.activeSelf)
    174.             {
    175.                 _fontSize = T_Value.fontSize;
    176.                 if (_fontSize < T_units.fontSize) _fontSize = T_units.fontSize;
    177.                 FreeValueUnits = false;
    178.             }
    179.  
    180.  
    181.         }
    182.  
    183.         if (T_Value.gameObject.activeSelf) prefV = UpdateSizeText(T_Value, value, _fontSize, FreeValueUnits);
    184.         if (T_ES.gameObject.activeSelf) prefES = UpdateSizeText(T_ES, ES, _fontSize * 0.6f, FreeValueUnits); else prefES = 0f;
    185.         if (T_EI.gameObject.activeSelf) prefEI = UpdateSizeText(T_EI, EI, _fontSize * 0.6f, FreeValueUnits); else prefEI = 0f;
    186.         if (T_units.gameObject.activeSelf) prefU = UpdateSizeText(T_units, units, _fontSize, FreeValueUnits);
    187.  
    188.         if (prefES > prefEI) prefEI = prefES;
    189.         else prefES = prefEI;
    190.  
    191.         float wCent = 0f, wALL2;
    192.  
    193.         if (FreeValueUnits)
    194.         {
    195.             wV = 0f;
    196.             wEIES = 1f;
    197.             wU = 0f;
    198.         }
    199.         else
    200.         {
    201.            
    202.             wALL = getWidth(RT_panel); if (wALL == 0) return;
    203.             wV = prefV / wALL;
    204.             wEIES = prefES / wALL;
    205.             wU = prefU / wALL;
    206.             wALL2 = prefV + prefES + prefU;
    207.             switch (aligen)
    208.             {
    209.                 case Aligen.Left:
    210.                     wCent = 0f;
    211.                     break;
    212.                 case Aligen.Center:
    213.                     wCent = (1f - wALL2 / wALL) / 2f;
    214.                     break;
    215.                 case Aligen.Right:
    216.                     wCent = (1f - wALL2 / wALL);
    217.                     break;
    218.  
    219.             }
    220.             if (wCent < 0.1f) wCent = 0f;
    221.         }
    222.         //        if (wV < 0.37f && (wV + wEIES) > (wU)) wV = 0.37f;
    223.  
    224.  
    225.         if (T_Value.gameObject.activeSelf)
    226.         {
    227.             RT_Value.anchorMin = new Vector2(wCent, RT_Value.anchorMin.y);
    228.             RT_Value.anchorMax = new Vector2(wCent+wV, RT_Value.anchorMax.y);
    229.             RT_Value.sizeDelta = Vector2.zero;
    230.         }
    231.  
    232.         if (T_ES.gameObject.activeSelf)
    233.         {
    234.             RT_ES.anchorMin = new Vector2(wCent+wV, RT_ES.anchorMin.y);
    235.             RT_ES.anchorMax = new Vector2(wCent+wV + wEIES, RT_ES.anchorMax.y);
    236.             RT_ES.sizeDelta = Vector2.zero;
    237.         }
    238.  
    239.         if (T_EI.gameObject.activeSelf)
    240.         {
    241.             RT_EI.anchorMin = new Vector2(wCent+wV, RT_EI.anchorMin.y);
    242.             RT_EI.anchorMax = new Vector2(wCent+wV + wEIES, RT_EI.anchorMax.y);
    243.             RT_EI.sizeDelta = Vector2.zero;
    244.         }
    245.  
    246.         if (T_units.gameObject.activeSelf)
    247.         {
    248.             if (T_ES.gameObject.activeSelf | T_EI.gameObject.activeSelf)
    249.             RT_units.anchorMin = new Vector2(wCent+wV + wEIES + 0.01f, RT_units.anchorMin.y);
    250.             RT_units.anchorMax = new Vector2(1f, RT_units.anchorMax.y);
    251.             RT_units.sizeDelta = Vector2.zero;
    252.         }
    253.  
    254.         T_Value.margin = Vector4.zero;
    255.         T_ES.margin = Vector4.zero;
    256.         T_EI.margin = Vector4.zero;
    257.         T_units.margin = Vector4.zero;
    258.     }
    259. }
    Everything works almost as it should (there are small problems in the mathematics of calculation, but I will fix it over time). There is also a problem that my knowledge will not allow me to fix - this is too slow work (as it seems to me). When finding a large number of prefab instances (more than 20 pieces) with this script, it is slowly updated and sometimes text twitches are visible - I want to get rid of this, please help me with this..