Search Unity

TextMesh Pro Can Font Weight be exposed in the inspector?

Discussion in 'UGUI & TextMesh Pro' started by CDF, Jun 9, 2022.

  1. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,313
    I want to be able to set a textFields font weight in the inspector.
    I can see that TMP_Text has a "m_fontWeight" serialized property.

    But is not being drawn in default inspector. I have to switch to debug mode to set it.

    I also don't want to use the <font-weight> tag, as that's increasing the complexity of setting text on the text field, especially with localization.

    Currently I've got a custom component that sets the font weight, but ideally this is just exposed in the TMP inspector.

    Code (CSharp):
    1. /// <summary>
    2. /// Controls the <see cref="TMP_Text.fontWeight"/> value of TMP text fields at runtime and in edit mode.
    3. /// </summary>
    4. /// <author>Chris Foulston</author>
    5. [ExecuteAlways]
    6. [RequireComponent(typeof(TMP_Text))]
    7. [AddComponentMenu("UI/TextMeshPro - Font Weight")]
    8. public sealed class TMPFontWeight : MonoBehaviour {
    9.  
    10.     #region Properties
    11.  
    12.     public FontWeight FontWeight {
    13.  
    14.         get => fontWeight;
    15.         set {
    16.  
    17.             fontWeight = TextField.fontWeight = value;
    18.         }
    19.     }
    20.  
    21.     public TMP_Text TextField => gameObject.GetOrSetComponent(ref textField);
    22.  
    23.     #endregion
    24.  
    25.     #region Fields
    26.  
    27.     [SerializeField] private FontWeight fontWeight = FontWeight.Regular;
    28.  
    29.     [NonSerialized] private TMP_Text textField = default;
    30.  
    31.     #endregion
    32.     #region Unity Methods
    33.  
    34.     private void OnEnable() {
    35.  
    36.         TextField.fontWeight = fontWeight;
    37.     }
    38.  
    39.     private void OnValidate() {
    40.  
    41.         TextField.fontWeight = fontWeight;
    42.     }
    43.  
    44.     #endregion
    45. }
    Can m_fontWeight be exposed in the inspector at some point?