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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Script to make TextMeshPro text the same font size as another text's

Discussion in 'UGUI & TextMesh Pro' started by Romeno, Apr 26, 2021.

  1. Romeno

    Romeno

    Joined:
    Jun 24, 2013
    Posts:
    34
    Hey,

    I have a need to make some text font size the same as the other. I was looking into the TextMeshPro code and found out that there is EventManager and one can use it's events to track stuff. I wrote the script but I think that might be terribly inefficient. May be there is a better way? Here is the script:

    Code (CSharp):
    1.     [ExecuteAlways]
    2.     [RequireComponent(typeof(TMP_Text))]
    3.     public class SameTextSizeAs : MonoBehaviour
    4.     {
    5.         public TMP_Text sameAsText;
    6.         private TMP_Text thisText;
    7.  
    8.         private void OnEnable()
    9.         {
    10.             thisText = GetComponent<TMP_Text>();
    11.            
    12.             TMPro_EventManager.TEXT_CHANGED_EVENT.Add(UpdateFontSize);
    13.         }
    14.  
    15.         private void OnDisable()
    16.         {
    17.              TMPro_EventManager.TEXT_CHANGED_EVENT.Remove(UpdateFontSize);
    18.         }
    19.  
    20.         public void UpdateFontSize(UnityEngine.Object obj)
    21.         {
    22.             if (sameAsText != null && obj == sameAsText)
    23.             {
    24.                 thisText.fontSize = sameAsText.fontSize;
    25.                 thisText.ForceMeshUpdate();
    26.             }
    27.  
    28.             if (thisText == obj)
    29.             {
    30.                 thisText.enableAutoSizing = false;
    31.             }
    32.         }
    33.     }
     
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    Can you provide more information on your use case?

    How do your text objects end up with different sizes? Text Auto-size?
     
  3. Romeno

    Romeno

    Joined:
    Jun 24, 2013
    Posts:
    34


    I want all the buttons to have the same font size as one particular button which has AutoSize option enabled. In the example above I wanted to make all buttons have the same font size as Description button because it is the longest word and will determine the smallest size needed for all of them to fit. This is useful for different layouts when button size scales but you want all the texts to be of the same size.

    Also... I want to use the possibility to thank you Stephan_B for all the energy you invested into the development of the TextMeshPro package and the effort continuously put into it's support. It is truly amazing.
     
    Last edited: Apr 27, 2021
  4. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    Thank you for providing this additional information and for the kind comments.

    The following post which is related to this topic contains some of my thoughts on this and hopefully will prove useful.